These exercises focus on class design rather than distributed systems. The examples use Python.
The six questions
| # | Question | Core idea |
|---|---|---|
| 1 | Design a hash map | Chained buckets, hash % size |
| 2 | Design an LRU cache | Doubly-linked list + hash table |
| 3 | Design a call center | Ranked employees, dispatch queue |
| 4 | Design a deck of cards | Suit/value enums, deal logic |
| 5 | Design a parking lot | Vehicle sizes, spot assignment |
| 6 | Design a chat server | User service, friend requests |
Hash map
class Item:
def __init__(self, key, value): ...
class HashTable:
def __init__(self, size):
self.table = [[] for _ in range(size)] # chained buckets
def _hash_function(self, key):
return hash(key) % self.size
def set(self, key, value): ... # append or replace in bucket
def get(self, key): ... # walk the bucket
LRU cache
Node: holds the result.LinkedList:move_to_front,append_to_front,remove_from_tail.Cache: a hash map (query → node) plus the linked list; evicts the tail at capacity.
Call center
Rankenum: operator, supervisor, director.Employee(abstract) holds rank and current call.- A dispatch
dequeroutes calls to the lowest-rank free employee; escalate if busy.
Deck of cards
Suitenum (heart, diamond, clubs, spade);Card(abstract) with value, suit, availability.- Deck, hand, and deal logic. Blackjack-style scoring is a common extension.
Parking lot
VehicleSizeenum: motorcycle, compact, large.Vehicletrackslicense_plate,spot_size, and the spots it occupies.ParkingLotassigns spots by size and frees them on exit.
Chat server
UserService:add_user,remove_user, and friend-request methods.User,Message, and conversation management.
How to structure the design
- State the entities and their relationships.
- Draw a class diagram (Mermaid
classDiagram). - Write the key methods.
- Agree on the amount of code before implementing.
- Cache interface
get(query) reads a stored result; set(query, results) writes one.
- Lookup
The lookup dictionary identifies the node for a query.
- Order
The linked list tracks node order for the cache policy.
- Node
Each node stores a query and its results. Keep the two structures consistent when entries move or leave.
The cache’s dictionary locates nodes; its linked list maintains the intended cache order.
How to read this model
An indexed record cabinet beside an ordered-node magazine. The object is a teaching analogy. Inspecting a part highlights its role; it does not execute or simulate the software. Cabinet trays are records, workpieces are data, and latches are checks. Unless an explanation states a quantity, compartments and sample workpieces do not denote data counts. Part dimensions are not a quantitative scale.