Skip to the page

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.

The 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

  1. Design a parking lot. What classes and which methods?
  2. Two gates issue tickets at once. How do you stop the same slot being given twice?
  3. Add electric cars that need a charging slot without changing the allocation code.
  4. How would you design a library that supports reservations and fines?
  5. How do you check for check in chess?

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview