Classic problems · senior
Elevator, cab booking and food delivery
These problems have many actors acting at the same time. Use a dispatch Strategy to choose, a state machine for the lifecycle, and an atomic claim so one driver or lift is never given two jobs.
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
- Elevator: Building, ElevatorController (dispatcher), Elevator (floor, direction, state, stops), Request, Direction enum. Movement uses the SCAN or LOOK idea: keep going in one direction while stops are ahead, then reverse, with two sorted sets of stops.
- Dispatch is a DispatchStrategy: nearest lift moving toward the caller in the same direction, else nearest idle. Lift state is an enum or State pattern: IDLE, MOVING_UP, MOVING_DOWN, DOORS_OPEN, MAINTENANCE, EMERGENCY.
- Buttons add requests to a thread-safe queue, and each lift has its own loop. Stop sets are guarded by the lift lock. First-come-first-served makes the lift zigzag.
- Cab booking: Rider, Driver (AVAILABLE, OFFERED, ON_TRIP, OFFLINE), Trip, RideService, MatchingStrategy, PricingStrategy and a DriverLocationIndex using grid cells or geohash.
- Matching: candidates from the rider cell and neighbours, filtered by type and status, sorted by distance or ETA. Claim a driver atomically (compare-and-set AVAILABLE to OFFERED, or a lock per driver); if the claim fails, try the next. An offer timeout returns the driver to AVAILABLE.
- Trip states REQUESTED, DRIVER_ASSIGNED, STARTED, COMPLETED or CANCELLED with compare-and-set transitions. Pricing is a Strategy (base plus per km plus per minute) times a surge multiplier.
- Food delivery order: PLACED, ACCEPTED or REJECTED, PREPARING, READY, PICKED_UP, DELIVERED; CANCELLED only from early states. One transition(event) method validates every move, with status and version compare-and-set. Stock reservation is atomic per item.
- Duplicate events are no-ops. Notifications and refunds run as observers on each transition, not inside Order.
The red pen
Common traps: mistake → fix
Mistake: One lift class holds everything
Fix: split controller, lift and strategy.
Mistake: Scan all drivers for each request
Fix: use a spatial index.
Mistake: synchronized on the whole matching service
Fix: lock per driver or compare-and-set.
Mistake: Pricing inside Trip
Fix: use a PricingStrategy.
Easy to hard
Questions interviewers ask, easy to hard
- Design an elevator system. How do you decide which lift goes?
- Design cab booking. How do you avoid giving one driver to two riders?
- What happens when a driver does not respond to an offer?
- Design the lifecycle of a food order. Who may cancel and when?
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.