Part 6 of 6 · 2 min read
Exercises
Easy
Easy 1: Write the requirements (20 minutes)
Write seven numbered requirements for a booking system and three questions you would ask.
Done when: seven rules and three questions exist, and two features are marked out of scope.
Hint
Think about what happens between choosing and paying.
Easy 2: Run the booking (20 minutes)
Type the code and reproduce the first three rows of the run table.
Done when: you see "A2 is taken", 10 free seats, and a total of 20.
Hint
The fake clock has a plus method for minutes.
Easy 3: Price by hand (20 minutes)
With flat price 10 and an extra 5 from row C, price the seats A1, B4, C2 and D1.
Done when: the four prices are 10, 10, 15 and 15, and the sum is 50.
Hint
Rows C and later get the extra charge.
Medium
Medium 1: Best adjacent seats (45 minutes)
Write a method that finds a block of N adjacent free seats in one row, or says none exists.
Done when: tests show a found block, a case where seats are split by a taken seat, and a case with none.
Hint
Walk along a row and count a run of free seats.
Medium 2: Cancel and refund (45 minutes)
Add cancel(bookingId) that frees the seats and returns an amount using a rule: full refund more than two hours before the show, half otherwise.
Done when: two tests show the two refund amounts and the seats are free again.
Hint
Keep the show start time in the booking.
Medium 3: Lazy expiry (45 minutes)
Make hold treat seats of an expired hold as free, so no sweep is needed for correctness.
Done when: a test moves the clock and a second buyer gets the seats without a sweep.
Hint
In hold, release any expired hold that owns a requested seat before checking.
Hard
Hard 1: Conditional update store (90 minutes)
Write a SeatStore interface with tryHold(seats, holdId) that is one atomic conditional step. Write an in-memory version with ConcurrentHashMap, and no synchronized block.
Done when: a 100-caller race has one winner and the other 99 leave nothing held.
Hint
putIfAbsent is atomic, but a multi-seat step needs undo of seats already taken.
Hard 2: Payment step (90 minutes)
Add a payment gateway interface that may fail. If payment fails after the hold, the seats must be released and the buyer told.
Done when: a fake gateway that fails every third call leaves no seat stuck in the held state.
Hint
Release in a finally-style path when the gateway fails.
Hard 3: Load test (90 minutes)
Simulate 1,000 buyers on 5 shows, each trying 1 to 4 random seats, with 16 threads. Check that no seat has two owners and that sold plus free equals the total.
Done when: a check prints "no double sales" and the counts add up exactly.
Hint
Count each seat's final state at the end, and compare with the total.