Java LLD: Designing a High-Concurrency Elevator System
Designing an elevator system is a classic "Machine Coding" round favorite because it tests concurrency, state management, and algorithmic efficiency simultaneously. At companies like Apple or Amazon, interviewers aren't just looking for a working loop; they are looking for thread safety and optimal scheduling.
The Mistake Most Candidates Make
Using a simple Queue<Integer> : This leads to inefficient "ping-ponging" where the elevator moves from floor 1 to 10, then back to 2, then up to 9.
Over-locking: Using a global lock on the entire ElevatorSystem which kills performance in multi-elevator scenarios.
Ignoring Visibility: Forgetting that the ElevatorController needs the most recent currentFloor from the Elevator thread to make dispatching decisions.
The Right Approach
Core mental model: Treat each elevator as an autonomous actor using the SCAN Algorithm (Elevator Algorithm) to process requests in its current path before reversing.
Key entities:…