Object-oriented design

Reusable class-design exercises covering hash maps, caches, call centers, and other domain models.

In this article

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

  • Rank enum: operator, supervisor, director.
  • Employee (abstract) holds rank and current call.
  • A dispatch deque routes calls to the lowest-rank free employee; escalate if busy.

Deck of cards

  • Suit enum (heart, diamond, clubs, spade); Card (abstract) with value, suit, availability.
  • Deck, hand, and deal logic. Blackjack-style scoring is a common extension.

Parking lot

  • VehicleSize enum: motorcycle, compact, large.
  • Vehicle tracks license_plate, spot_size, and the spots it occupies.
  • ParkingLot assigns 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

  1. State the entities and their relationships.
  2. Draw a class diagram (Mermaid classDiagram).
  3. Write the key methods.
  4. Agree on the amount of code before implementing.
Fig 1A lookup index and an order structure cooperate
STUDY MODEL
Inspect one part at a timeCache interface
  1. Cache interface

    get(query) reads a stored result; set(query, results) writes one.

  2. Lookup

    The lookup dictionary identifies the node for a query.

  3. Order

    The linked list tracks node order for the cache policy.

  4. 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.

---
Keep track of your reading.

Your reading progress stays in this browser.