Requirements
Functional Requirements
1. Single Elevator:
- Move between floors
- Open/close doors
- Carry passengers
2. Multiple Elevators:
- Coordinate between elevators
- Assign optimal elevator to request
3. Floor Requests:
- Inside elevator: select destination floor
- Outside elevator: call up/down
4. Scheduling:
- Optimize for waiting time
- Handle multiple requests efficiently
Non-Functional Requirements
1. Safety: Emergency stop, overload detection
2. Reliability: 99.99% uptime
3. Performance: <30s wait time
4. Capacity: Handle 1000+ requests/hour
Elevator States
┌────────────────────────────────────────┐
│ Elevator States │
├────────────────────────────────────────┤
│ │
│ IDLE → MOVING_UP → AT_FLOOR │
│ │ │ │
│ │ ▼ │
│ │ DOORS_OPEN │
│ │ │ │
│ │ ▼ │
│ │ DOORS_CLOSED │
│ │ │ │
│ │ ▼ │
│ └────────── MOVING_DOWN │
│ │
└────────────────────────────────────────┘
Scheduling Algorithm
SCAN (Elevator) Algorithm
The SCAN algorithm moves the elevator in one direction,
servicing all requests in that direction before reversing.
Floor 10: █ (elevator)
Floor 9: █ (request)
Floor 8:
Floor 7: █ (request)
Floor 6:
Floor 5:
Floor 4: █ (request)
Direction: DOWN
Order: 10 → 9 → 7 → 4 → (reverse) → ...
Elevator Controller
public class ElevatorController {
private final List<Elevator> elevators;
private final RequestQueue requestQueue;
public ElevatorController(int numElevators, int numFloors) {
this.elevators = new ArrayList<>();
for (int i = 0; i < numElevators; i++) {
elevators.add(new Elevator(i, numFloors));
}
this.requestQueue = new RequestQueue();
}
public void requestElevator(int floor, Direction direction) {
Elevator bestElevator = findBestElevator(floor, direction);
bestElevator.addRequest(floor);
}
private Elevator findBestElevator(int floor, Direction direction) {
// Find closest elevator moving toward the floor
return elevators.stream()
.filter(e -> e.isAvailable() ||
(e.getDirection() == direction &&
isMovingToward(e, floor)))
.min(Comparator.comparingInt(e -> Math.abs(e.getCurrentFloor() - floor)))
.orElse(elevators.get(0));
}
}
SCAN Implementation
public class ScanScheduler {
private final PriorityQueue<Integer> upRequests;
private final PriorityQueue<Integer> downRequests;
public void addRequest(int floor, Direction direction) {
if (direction == Direction.UP) {
upRequests.add(floor);
} else {
downRequests.add(floor);
}
}
public int getNextFloor(Elevator elevator) {
if (elevator.getDirection() == Direction.UP) {
if (!upRequests.isEmpty()) {
return upRequests.poll();
}
// Switch direction
elevator.setDirection(Direction.DOWN);
return downRequests.poll();
} else {
if (!downRequests.isEmpty()) {
return downRequests.poll();
}
elevator.setDirection(Direction.UP);
return upRequests.poll();
}
}
}
Request Assignment
public class RequestAssigner {
public Elevator assign(List<Elevator> elevators, int floor) {
return elevators.stream()
.min(Comparator.comparingInt(e -> {
int distance = Math.abs(e.getCurrentFloor() - floor);
int penalty = e.isIdle() ? 0 : 5;
return distance + penalty;
}))
.orElse(elevators.get(0));
}
}
State Management
Elevator Class
public class Elevator {
private final int id;
private int currentFloor;
private Direction direction;
private ElevatorState state;
private final List<Integer> requests;
private final Door door;
public Elevator(int id, int numFloors) {
this.id = id;
this.currentFloor = 1;
this.direction = Direction.IDLE;
this.state = new IdleState();
this.requests = new ArrayList<>();
this.door = new Door();
}
public void addRequest(int floor) {
requests.add(floor);
if (state instanceof IdleState) {
processNextRequest();
}
}
public void processNextRequest() {
if (requests.isEmpty()) {
setState(new IdleState());
return;
}
int nextFloor = getNearestRequest();
if (nextFloor > currentFloor) {
setState(new MovingUpState());
} else if (nextFloor < currentFloor) {
setState(new MovingDownState());
} else {
setState(new DoorsOpenState());
}
}
public void moveOneFloor() {
if (direction == Direction.UP) {
currentFloor++;
} else if (direction == Direction.DOWN) {
currentFloor--;
}
if (requests.contains(currentFloor)) {
requests.remove(Integer.valueOf(currentFloor));
setState(new DoorsOpenState());
}
}
}
Elevator States
public class IdleState implements ElevatorState {
public void process(Elevator elevator) {
if (!elevator.getRequests().isEmpty()) {
elevator.processNextRequest();
}
}
}
public class MovingUpState implements ElevatorState {
public void process(Elevator elevator) {
elevator.moveOneFloor();
}
}
public class DoorsOpenState implements ElevatorState {
public void process(Elevator elevator) {
elevator.getDoor().open();
// Wait for passengers
elevator.getDoor().close();
elevator.processNextRequest();
}
}
Display Panel
public class DisplayPanel {
private final int floor;
private final Direction direction;
private final ElevatorState state;
public void update(Elevator elevator) {
this.floor = elevator.getCurrentFloor();
this.direction = elevator.getDirection();
this.state = elevator.getState();
}
}
Follow-ups
Follow-up Questions
1. How to handle emergency stop?
→ EmergencyState
→ Override all other states
→ Alarm system
2. How to handle power failure?
→ Battery backup
→ Emergency lighting
→ Auto-leveling at nearest floor
3. How to optimize for peak hours?
→ Machine learning prediction
→ Pre-positioning elevators
→ Zoning (high-rise vs low-rise)
4. How to handle wheelchair accessibility?
→ Priority requests
→ Extended door opening time
→ Voice announcements
5. How to monitor elevator health?
→ Sensor data collection
→ Predictive maintenance
→ Remote diagnostics
Design Patterns Used
| Pattern | Usage |
|---|---|
| State | Elevator states |
| Strategy | Scheduling algorithm |
| Observer | Display updates |
| Factory | Elevator creation |
| Mediator | Elevator coordination |
Performance Metrics
- Average wait time: <30 seconds
- Average ride time: <60 seconds
- Door open time: 5-10 seconds
- Capacity: 8-15 people per elevator
- Speed: 1-2 floors/second
Practice Problems
Design a scalable Elevator Design system. Cover high-level architecture, data model, and API design.
Solution
// Complete system design:
// - Functional + Non-functional requirements
// - Capacity estimation
// - Data model (SQL/NoSQL choice)
// - API endpoints
// - Component architecture
// - Scaling strategy
// - Monitoring & reliabilityHow would you scale Elevator Design to handle 10x the current load? Identify bottlenecks and solutions.
Solution
// Scaling approach:
// 1. Load balancing
// 2. Database sharding/replication
// 3. Cache layer (Redis)
// 4. CDN for static assets
// 5. Async processing (queues)
// 6. Microservices decompositionAnalyze potential failure modes for Elevator Design and design mitigation strategies.
Solution
// Failure mitigation:
// 1. Redundancy (multi-AZ)
// 2. Circuit breakers
// 3. Retry with backoff
// 4. Dead letter queues
// 5. Health checks
// 6. Graceful degradationQuiz
1. What scheduling algorithm is commonly used for elevators?
2. How does the system choose which elevator to send?
3. What pattern models elevator behavior?
4. What happens when an elevator reaches a requested floor?
5. What is the benefit of SCAN over simple FIFO?
Flashcards
Question
What is SCAN scheduling?
Click to reveal answer
Answer
Elevator algorithm: moves in one direction servicing all requests, then reverses. Reduces total travel distance.
Question
How to assign elevator to request?
Click to reveal answer
Answer
Find nearest elevator that is idle or moving toward the request floor in the same direction.
Question
Elevator states?
Click to reveal answer
Answer
Idle, MovingUp, MovingDown, DoorsOpen, DoorsClosed. Each state has different behavior.
Question
What pattern handles elevator coordination?
Click to reveal answer
Answer
Mediator pattern coordinates multiple elevators. Strategy for scheduling algorithm. State for elevator behavior.
Question
Peak hour optimization?
Click to reveal answer
Answer
Pre-position elevators, zoning (high/low rise), ML prediction, priority for ground floor during rush hour.
Revision Notes
Key Takeaways
- 1.SCAN algorithm efficiently services elevator requests by direction
- 2.State pattern models elevator states and transitions
- 3.Elevator assignment considers proximity and direction
- 4.Multiple elevators coordinate through a controller/mediator
- 5.Peak hour optimization requires pre-positioning and prediction
Interview Tips
- •Explain SCAN algorithm with visual example
- •Show how multiple elevators coordinate
- •Discuss state transitions in the elevator
- •Mention optimization for peak hours
Cheat Sheet
Elevator Design - Cheat Sheet
SCAN Algorithm:
Move in one direction, service all requests, then reverse.
Reduces total travel distance.
Elevator States:
Idle → MovingUp → DoorsOpen → DoorsClosed → MovingDown
Request Flow:
- Passenger requests elevator
- System finds best elevator
- Elevator moves to floor
- Doors open/close
- Process next request
Assignment Logic:
Find nearest elevator that is:
- Idle, OR
- Moving toward request in same direction
Patterns:
State (elevator), Strategy (scheduling), Observer (displays), Mediator (coordination)