Classic problems · core
Parking lot, library and board games
These are the most common starter problems. They test whether you can turn rules into small classes, keep lookups fast and make new rules cheap: a parking lot, a library, snake and ladder and a chess move checker.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- Parking lot classes: ParkingLot, Floor, Slot (id, vehicle type it accepts, occupant), Vehicle, Ticket (id, slot, entry time), and a ParkingService with park, unpark and freeSlots. The service never prints.
- Allocation is a SlotAllocationStrategy (lowest floor, nearest gate) injected into the service. Pricing is a PricingStrategy. Fast lookup: per floor and type a sorted set or heap of free slots, plus a map from ticket id to ticket. No scan of every slot.
- Two gates at once: the find-and-take step must be atomic. One lock around allocation, a lock per floor, or a compare-and-set on the slot.
- Library: separate Book (metadata) from BookCopy (id, borrower, due date). BorrowPolicy and FinePolicy are Strategies. Search uses an index per field. Reservations are a queue per book and returning a copy notifies the next member (Observer). Borrowing the last copy is atomic.
- Snake and ladder: Game, Board with a map from cell to BoardEntity (Snake, Ladder), Player, and a Dice interface. Turn order is a queue. Overshooting 100 means no move. Follow jumps in a loop for chained entries. Inject a fixed Dice to test.
- Chess: Board, Piece, Position, Move and composable MoveRule strategies (straight, diagonal, L-shape, one step). A queen is straight plus diagonal. Check path clear for sliding pieces but not for knights.
- Chess check detection: apply the move on a copy (or apply and undo) and see whether an enemy piece attacks the king. Undo uses a move history stack (Command pattern).
- Never put everything in main. Do not use Math.random inside the game loop.
The red pen
Common traps: mistake → fix
Mistake: One ParkingLot class with if/else on vehicle type is fine
Fix: new vehicle types then edit five places; use data and strategies.
Mistake: Book and BookCopy are the same
Fix: one book has many copies with different borrowers.
Mistake: One big switch on piece type for chess
Fix: compose rules instead.
Mistake: Snakes and ladders as two separate maps
Fix: use one BoardEntity abstraction.
Easy to hard
Questions interviewers ask, easy to hard
- Design a parking lot. What classes and which methods?
- Two gates issue tickets at once. How do you stop the same slot being given twice?
- Add electric cars that need a charging slot without changing the allocation code.
- How would you design a library that supports reservations and fines?
- How do you check for check in chess?
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.