Design an Elevator System
What is the Elevator System Problem?
Section titled “What is the Elevator System Problem?”Design a smart elevator control system for a high-rise building. It needs to handle multiple elevator cars, different request strategies (SCAN, FCFS), and emergency states.
In this problem, you’ll design a central controller that dispatches elevators based on proximity, direction, and current load to reduce waiting time and optimize efficiency.
Problem Overview
Section titled “Problem Overview”Design a smart elevator control system for a building with multiple elevators and many floors. The system must efficiently move people while respecting capacity limits and safety protocols.
Core Requirements
Section titled “Core Requirements”Functional Requirements:
- Multi-Elevator Support: Handle a configurable number of elevators, all serving all floors.
- Capacity Management: Each car has a maximum weight/person limit that must never be exceeded.
- Request Handling: Support external floor requests and internal cabin floor selections.
- Smart Dispatching: Minimize wait times by selecting the best elevator based on direction, distance, and load.
- Batch Processing: Each car should handle multiple requests, stopping at floors along the way efficiently.
- State Tracking: Monitor and update current floor positions and movement states.
- Door Control: Properly manage transitions for door opening and closing during stops.
Non-Functional Requirements:
- Object-Oriented Design: Clear separation of roles (Controller, Car, Dispatcher, Button).
- Modular & Flexible: Easy to add new strategies (SCAN, FCFS) or maintenance modes.
- Thread Safety: Ensure state updates and requests are handled safely under concurrent access.
- Maintainability: Core logic should be easy to understand, test, and extend over time.
What’s Expected?
Section titled “What’s Expected?”1. System Architecture
Section titled “1. System Architecture”The system uses a central ElevatorController to manage the fleet of ElevatorCars.
2. Key Classes to Design
Section titled “2. Key Classes to Design”classDiagram
class ElevatorController {
-List~ElevatorCar~ cars
-DispatchStrategy strategy
+handleRequest(request)
-findBestCar(request)
}
class ElevatorCar {
-int currentFloor
-Direction direction
-State state
-InternalButtons buttons
+move()
+stop()
}
class Request {
-int floor
-Direction direction
-RequestType type
}
class DispatchStrategy {
<<interface>>
+selectCar(cars, request)
}
ElevatorController --> ElevatorCar
ElevatorController --> DispatchStrategy
ElevatorCar --> Request
System Flow
Section titled “System Flow”Dispatching Flow
Section titled “Dispatching Flow”Key Design Challenges
Section titled “Key Design Challenges”1. Optimizing Movement (The Elevator Algorithm)
Section titled “1. Optimizing Movement (The Elevator Algorithm)”Should an elevator stop for every floor in order (FCFS), or should it pick up everyone on the way (SCAN)?
Solution: Use the Strategy Pattern. Implement the SCAN algorithm (also known as the Elevator Algorithm), where the car continues in its current direction until all requests in that direction are fulfilled before reversing.
2. Assigning the “Best” Car
Section titled “2. Assigning the “Best” Car”Assigning the nearest car isn’t always best if that car is already full or going in the opposite direction.
Solution: The ElevatorController should calculate a “cost” for each car based on:
- Distance to the floor.
- Current direction vs. request direction.
- Number of pending stops.
- Current load.
3. Thread-Safe Requests
Section titled “3. Thread-Safe Requests”Multiple users on different floors might press buttons at the exact same millisecond.
Solution: Use thread-safe data structures like ConcurrentLinkedQueue for pending requests and ReentrantLock for updating the state of an individual elevator car.
What You’ll Learn
Section titled “What You’ll Learn”By solving this problem, you’ll master:
- ✅ Strategy Pattern - Making algorithms swappable and extensible.
- ✅ State Management - Coordinating complex, independent units.
- ✅ Concurrency - Building robust systems for high-contention environments.
- ✅ Resource Optimization - Balancing user wait time vs. energy efficiency.
View Complete Solution & Practice
Section titled “View Complete Solution & Practice”Ready to see the full implementation? Open the interactive playground to access:
- 🎯 Step-by-step guidance through the 8-step LLD approach
- 📊 Interactive UML builder to visualize your design
- 💻 Complete Code Solutions in Python, Java, C++, TypeScript, JavaScript, C#
- 🤖 AI-powered review of your design and code
Related Problems
Section titled “Related Problems”After mastering the Elevator System, try these similar problems:
- Traffic Signal System - Coordinating timing and states.
- Vending Machine - Another state-heavy machine design.
- Task Scheduler - Advanced request queuing and execution.