Bird
0
0
LLDsystem_design~25 mins

Elevator, Floor, Request classes in LLD - System Design Exercise

Choose your learning style9 modes available
Design: Elevator Control System
Design the core classes and their interactions for elevator, floor, and request management. Exclude hardware control and detailed UI design.
Functional Requirements
FR1: Manage multiple elevators in a building
FR2: Handle requests from floors to go up or down
FR3: Handle requests from inside elevators to select destination floors
FR4: Optimize elevator movement to reduce wait and travel time
FR5: Track elevator current floor and direction
FR6: Support at least 10 floors and 4 elevators
Non-Functional Requirements
NFR1: Response time for request assignment should be under 200ms
NFR2: System should handle up to 100 concurrent requests
NFR3: Elevator state updates must be consistent and thread-safe
NFR4: Availability target: 99.9% uptime
Think Before You Design
Questions to Ask
❓ Question 1
❓ Question 2
❓ Question 3
❓ Question 4
❓ Question 5
Key Components
Elevator class to track state and handle movement
Floor class to represent each floor and its requests
Request class to represent a pickup or dropoff request
Scheduler or controller to assign requests to elevators
Design Patterns
Observer pattern for elevator state updates
Command pattern for requests
Singleton for central controller
State pattern for elevator movement states
Reference Architecture
  +----------------+       +----------------+       +----------------+
  |     Floor      |<----->|   Controller   |<----->|    Elevator    |
  +----------------+       +----------------+       +----------------+
         ^                         ^   ^                      ^
         |                         |   |                      |
  Floor requests             Assign requests           Elevator state
  (up/down buttons)          (pickup/dropoff)          (current floor,
                                                       direction, requests)
Components
Elevator
Class with attributes and methods
Represents an elevator with current floor, direction, and list of destination requests
Floor
Class with attributes
Represents a floor with up/down request buttons and pending requests
Request
Class
Represents a request either from a floor (pickup) or inside elevator (dropoff)
Controller
Singleton class
Receives requests and assigns them to elevators based on current state
Request Flow
1. User presses up/down button on a Floor, creating a pickup Request
2. Floor notifies Controller of the new Request
3. Controller evaluates all elevators and assigns the Request to the best Elevator
4. Elevator adds the Request to its destination queue
5. Elevator moves floor by floor, updating current floor and direction
6. When Elevator reaches a requested floor, it opens doors and removes the Request
7. Inside Elevator, user selects destination floor, creating a dropoff Request
8. Elevator adds dropoff Request to its queue
Database Schema
Entities: - Elevator(id, current_floor, direction, status, destination_queue) - Floor(number, up_button_pressed, down_button_pressed) - Request(id, type [pickup/dropoff], floor_number, direction [up/down/null], elevator_id [nullable], status) Relationships: - Elevator has many Requests - Floor can have multiple pickup Requests - Requests link to Elevator when assigned
Scaling Discussion
Bottlenecks
Controller becomes a bottleneck when handling many concurrent requests
Elevator state updates may cause race conditions if not synchronized
Request queue management can become complex with many elevators and floors
Solutions
Use concurrent data structures and locks to ensure thread safety
Distribute controller logic or shard by floors or elevator groups
Implement priority queues and efficient scheduling algorithms
Use event-driven architecture to decouple components
Interview Tips
Time: Spend 10 minutes clarifying requirements and constraints, 20 minutes designing classes and interactions, 10 minutes discussing scaling and optimizations, 5 minutes summarizing
Clearly define responsibilities of Elevator, Floor, and Request classes
Explain how requests flow through the system and get assigned
Discuss thread safety and state consistency
Mention how to optimize elevator scheduling
Address scaling challenges and solutions