Design Elevator Management System - Single Lift
What is the Elevator System - Single Lift Problem?
Section titled “What is the Elevator System - Single Lift Problem?”Design an elevator management system for a single elevator car in a building. The system should efficiently handle internal requests (from inside the elevator) and external requests (from floor buttons), manage direction-based request prioritization, handle state transitions (IDLE, MOVING_UP, MOVING_DOWN, STOPPED), and coordinate door operations.
In this problem, you’ll design a system that focuses on request queue management, direction optimization, and state transitions without the complexity of coordinating multiple elevators.
Problem Overview
Section titled “Problem Overview”Design a single elevator system that efficiently processes requests through intelligent queue management and direction-based prioritization.
Core Requirements
Section titled “Core Requirements”Functional Requirements:
- Single Elevator Car: Manage one elevator car that can access all floors in the building.
- Request Types: Handle both external requests (users pressing up/down buttons on floors) and internal requests (users selecting destination floors from inside the elevator).
- Request Queue: Maintain a queue of pending requests and process them efficiently based on current direction and floor position.
- Direction Optimization: When moving UP, prioritize requests above current floor going UP, and internal requests to floors above. When moving DOWN, prioritize requests below current floor going DOWN, and internal requests to floors below.
- State Management: Handle states (IDLE, MOVING_UP, MOVING_DOWN, STOPPED) with proper transitions.
- Door Operations: When elevator arrives at a requested floor, stop, open doors, wait briefly, close doors, and continue processing requests.
- Concurrent Requests: Handle multiple simultaneous requests from different users without conflicts.
- Idle Behavior: When IDLE, process the closest request (any direction).
Non-Functional Requirements:
- Modular Design: Each class should have well-defined roles following the Single Responsibility Principle.
- Extensibility: Easy to add new request prioritization strategies or elevator features without refactoring.
- Thread Safety: Request handling and elevator state updates must be thread-safe for concurrent requests.
- State Pattern: Use State Pattern to manage elevator states and transitions cleanly.
- Testability: Core elevator logic should be easy to test, understand, and maintain.
What’s Expected?
Section titled “What’s Expected?”1. System Architecture
Section titled “1. System Architecture”The system coordinates between users, the elevator system, and display components, focusing on single elevator management.
2. Key Classes to Design
Section titled “2. Key Classes to Design”classDiagram
class ElevatorSystem {
-Elevator elevator
-int totalFloors
+requestElevator(int, Direction) void
+selectFloor(int) void
}
class Elevator {
-int currentFloor
-Direction direction
-ElevatorState state
-List~Request~ requestQueue
+addRequest(Request) void
+move() void
+getNextRequest() Request
}
class ElevatorState {
<<interface>>
+handleRequest(Elevator, Request) void
+move(Elevator) void
}
class Request {
-int floor
-Direction direction
-RequestType type
}
ElevatorSystem --> Elevator
Elevator --> ElevatorState
Elevator --> Request
ElevatorState <|.. IdleState
ElevatorState <|.. MovingUpState
ElevatorState <|.. MovingDownState
ElevatorState <|.. StoppedState
System Flow
Section titled “System Flow”Request Processing Flow
Section titled “Request Processing Flow”Key Design Challenges
Section titled “Key Design Challenges”1. Direction-Based Request Prioritization
Section titled “1. Direction-Based Request Prioritization”When the elevator is moving UP, it should prioritize requests above the current floor going UP, and when moving DOWN, prioritize requests below going DOWN.
Solution: Implement direction-based filtering in getNextRequest(). Filter requests based on current direction and floor position. When moving UP, select closest request above current floor going UP or internal requests above. When moving DOWN, select closest request below current floor going DOWN or internal requests below.
2. State Transitions
Section titled “2. State Transitions”Elevator must transition between states (IDLE → MOVING_UP/DOWN → STOPPED → MOVING_UP/DOWN → IDLE) correctly based on requests and movement.
Solution: Use the State Pattern. Define ElevatorState interface with implementations (IdleState, MovingUpState, MovingDownState, StoppedState). Each state handles requests and movement differently, and manages transitions to appropriate next states.
3. Request Queue Management
Section titled “3. Request Queue Management”The system needs to efficiently prioritize requests while handling concurrent additions and removals.
Solution: Use thread-safe collections (synchronized lists or concurrent queues) for the request queue. Implement direction-based prioritization logic that filters and selects requests based on current state and direction. Consider using priority queues for more efficient selection.
4. Concurrent Request Handling
Section titled “4. Concurrent Request Handling”Multiple users can request elevators simultaneously from different floors, requiring thread-safe operations.
Solution: Implement thread-safe operations using synchronized methods, thread-safe collections, and fine-grained locking. Ensure request queue operations (addRequest(), getNextRequest()) and state updates are atomic.
What You’ll Learn
Section titled “What You’ll Learn”By solving this problem, you’ll master:
- ✅ State Management - Controlling object lifecycles with the State Pattern.
- ✅ Queue Management - Efficient request prioritization and processing.
- ✅ Direction Optimization - Intelligent request selection based on movement direction.
- ✅ Observer Pattern - Decoupled notifications to displays.
- ✅ Concurrency - Thread-safe operations for concurrent requests.
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 Elevator System - Single Lift, try these similar problems:
- Elevator System - Multiple Lifts - Multiple elevator coordination.
- Elevator System - Request Feasibility - Advanced feasibility checking.
- Task Management System - Queue management and prioritization.