Elevator System Design with 5-Stop Constraint

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2024-12-16

Problem Statement

Design a multi-floor elevator system for a building that has exactly 5 floors (0–4). The elevator must handle requests efficiently while ensuring safety and fairness.


Functional Requirements


1.Basic Operations
•Elevator starts at floor 0 by default.
•Users can make internal requests (selecting floor buttons inside the elevator).
•Users can make external requests (calling the elevator UP/DOWN from floors).
•Elevator should move one floor at a time and open/close doors at stops.
2.Constraints
•Only 5 floors exist (0–4).
•Elevator cannot go below 0 or above 4.
•Capacity limit: configurable (default: 8 people).
•Each request should be served in FIFO manner within direction strategy (like real elevators).
3.Scheduling
•Must decide next stop based on:
•Current direction (UP/DOWN).
•Pending requests.
•Cannot skip requests in current direction unless floor range ends.
4.Edge Cases
•Two users press the same floor button → only one stop is made.
•External UP/DOWN requests at top/bottom floors (invalid direction).
•Over-capacity (ignore new requests until space frees up).
•If idle (no requests), elevator waits at last floor.


Non-Functional Requirements


•Thread-safety: multiple users can press buttons at the same time.
•Realism: add door open/close delays and travel delays between floors.
•Extensibility: should allow changing to more than 5 floors in future.


Design Hints


•Classes / Entities
•Elevator → maintains current floor, direction, requests.
•Request → represents floor + type (internal/external).
•Controller → manages scheduling logic (like SCAN/LOOK).
•Door → handles open/close actions.
•Patterns
•Command Pattern → Encapsulate requests (internal/external).
•State Pattern → Elevator states (Idle, Moving, DoorOpen).
•Singleton → Central Controller.
•Data Structures
•Two PriorityQueues for requests (UP, DOWN).
•Or simple Set<Integer> for floor requests since it’s only 5 floors.


Example Flow


1.Elevator at floor 0 → user at floor 3 presses DOWN → request added.
2.Elevator moves from 0 → 3, opens door.
3.Inside elevator, user presses floor 1 → request added.
4.Elevator moves 3 → 1, opens door → request removed.


👉 This problem tests class design, concurrency, scheduling, edge-case handling, and extensibility with a bounded constraint of 5 stops.

Solution

Comments