Skip to content
Tutorials Center
My learning

Topics

Lesson 61 of 77 ยท DevOps, Tools and System Design

Low-Level Design: Parking Lot, Elevator and Cache

Learn the low-level design interview approach: model a parking lot, an elevator state machine and an LRU cache with clean classes, extensibility and runnable code.

High-level system design asks "which servers and databases?". Low-level design (LLD, also called object-oriented design) asks "which classes, which methods, and how do they collaborate?" A typical prompt is "design a parking lot" or "design an LRU cache". The interviewer wants to see that you can turn a fuzzy requirement into a small, readable model, protect its rules, and make it easy to extend without rewriting everything.

The examples are written in JavaScript and run in the browser console editor. They are single-process models without a database or user interface, which is exactly what an LLD interview expects: working core logic, not infrastructure. The ideas carry over to Java, C#, Python or any other object-oriented language. Principles used here (SOLID, strategy pattern) are explained in OOP, SOLID and design patterns.

An LLD answer, step by step

Clarify Rules, sizes, edge cases.
Nouns They become classes.
Verbs They become methods.
Extend Where might it change?
1 / 2

What approach should you follow for any LLD question?

  1. Clarify requirements. Who uses it? What are the core operations? What are the limits (capacity, vehicle types)? What is out of scope (payments, UI)? Write these down.
  2. Find the nouns and verbs. Nouns (vehicle, spot, ticket, lot) become classes or enums; verbs (park, leave, pay) become methods. Do not create a class that has no behavior and no rule to protect.
  3. Assign responsibility. Each class has one reason to change and guards its own invariants (a spot cannot hold two vehicles).
  4. Write the main flow first in code, then the edge cases (full lot, unknown ticket).
  5. Show extension points. Ask "what will the interviewer ask me to add next?" (new vehicle type, new pricing, a second floor) and make the answer a new class rather than an edit to an if chain.
  6. Test with a tiny scenario and talk through the output.

How would you design a parking lot?

Requirements to confirm: several floors or one level, spots of different sizes, vehicles of different sizes, a ticket on entry, a fee on exit. Core model:

  • Vehicle with a size (motorcycle, car, truck).
  • Spot with a size and an occupied flag. A vehicle fits in a spot that is at least as big.
  • ParkingLot finds a spot, issues a Ticket, and on exit frees the spot and charges.
  • A pricing strategy object so rates can change without touching the lot.

The lot asks for a spot through one method, which is where you can later add smarter rules (closest to the entrance, a reserved area for electric cars).

JS Parking lot with a pluggable pricing strategy

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Points to say aloud: the lot depends only on "something with a price(hours) method", so adding weekend pricing is a new class (open/closed principle). The size rule lives in Spot.fits, not scattered in the lot. In a real system concurrency matters: two cars must not get the same spot, so spot assignment needs a lock or a database constraint.

How would you design an elevator?

Elevators are a state machine problem. State plus rules decide what the elevator does next. Requirements to clarify: how many elevators, how many floors, are there hall calls (up/down buttons) and car calls (buttons inside), and what scheduling goal (shortest wait, least travel).

For a single elevator, a good simple policy is the SCAN idea (like a disk arm): keep moving in the current direction, stopping at requested floors on the way, and reverse only when nothing is left ahead. For several elevators add a dispatcher that assigns each hall call to the best car (nearest idle one, or one already moving toward the caller). That dispatcher is a strategy object, so you can change the policy later.

Model the states explicitly: IDLE, MOVING_UP, MOVING_DOWN (and, with doors, DOORS_OPEN). Invalid transitions (moving with the doors open) become impossible or throw errors instead of becoming subtle bugs.

JS A single-elevator state machine with SCAN scheduling

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The elevator serves floors 5 and 6 on the way up, then reverses and serves floor 1, rather than zig-zagging in request order. That is the whole value of the policy. When asked "what if there are 4 elevators?", answer: add an ElevatorController that owns the cars and a pluggable DispatchStrategy, and keep each Elevator unaware of the others.

How do you design an LRU cache?

An LRU (least recently used) cache holds a fixed number of items. When it is full, adding a new item evicts the one used longest ago. Both get(key) and put(key, value) must run in O(1). The classic solution combines two structures:

  • a hash map from key to node, for instant lookup;
  • a doubly linked list ordered by recency, with the most recent at the front. Moving a node to the front and removing the tail are O(1) when you hold the node.

In JavaScript there is a shortcut: a Map remembers insertion order, so deleting a key and setting it again moves it to the end; the first key is always the least recently used. Interviewers often ask for the linked-list version because it shows you understand why the structure works. Here are both:

JS LRU cache: hash map plus doubly linked list

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Follow-ups to prepare for: thread safety (a lock around get and put, because even a read mutates the list), TTL expiry (store an expiry time per node), other policies (LFU evicts the least frequently used; make the eviction policy a strategy), and why not a plain array (moving and finding items would be O(n)).

What makes an LLD answer stand out?

  • Say your assumptions before coding, and list what you leave out.
  • Small classes with clear names that mirror the domain. Prefer composition (a lot has a pricing strategy) over deep inheritance.
  • Enums and constants instead of magic strings spread everywhere (the demos use simple objects to stay short).
  • Validation at the edges: reject unknown tickets, full lots and negative capacities with clear errors.
  • Name the pattern only when it helps: strategy for pricing or dispatch, state for the elevator, observer for notifications, factory for creating vehicles.
  • Run a scenario and show the output matches your expectation.

Other common prompts reuse the same skills: a library system, a vending machine (state machine), a ride-sharing matcher (strategy), a chess game (polymorphism), a notification service (observer), a logger (singleton, with a warning about its downsides).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In the parking lot, why is pricing passed in as an object with a price(hours) method?

    That is the strategy pattern and the open/closed principle: the lot depends on behavior, so new rules are additions, not edits.
  2. 2. Which data structures give an LRU cache O(1) get and put?

    The map finds the node instantly; the doubly linked list moves it to the front and removes the least recent one in constant time.
  3. 3. Why model an elevator as a state machine?

    Explicit states (idle, moving up, moving down, doors open) and rules for changing them prevent subtle bugs such as moving with open doors.
  4. 4. The interviewer says 'now add electric-vehicle spots'. What is the best design response?

    A good design localizes change: a new spot type or selection rule plugs in through the existing extension points.

Summary

  • LLD means classes, responsibilities and collaboration: clarify, find nouns and verbs, code the main flow, then edge cases and extensions.
  • Put rules where the data lives (Spot.fits) and inject changeable behavior (pricing, dispatch) as strategies.
  • Model machines such as elevators with explicit states and SCAN-style scheduling.
  • An LRU cache is a hash map plus a doubly linked list; JavaScript's Map insertion order gives a shortcut.
  • Test with a small scenario and discuss concurrency, expiry and alternative policies.

Next: Testing and Code Quality Questions.