Skip to main content

Pattern · 25 problems

Class Design Interview Problems

Design problems ask for a class, not a function. I list the methods and the cost I want from each, then choose structures.

When I reach for it

  • The statement describes a class with methods, not one function.
  • Each method needs to be fast, and the structure decides how fast.
  • Requirements arrive in stages.

Problems, easiest first

2 easy · 12 medium · 11 hard. Open one to read it, then write it in the free editor.

The idea

Write down every method and the cost you want for it. Then pick the structure that delivers those costs, such as a hash map plus a list.

State the invariants. What must stay true after every call? Check them on the error paths too.

from collections import deque

class HitCounter:
    def __init__(self):
        self.hits = deque()          # timestamps, oldest first

    def hit(self, t):
        self.hits.append(t)

    def count(self, t):
        while self.hits and self.hits[0] <= t - 300:
            self.hits.popleft()      # drop what has expired
        return len(self.hits)
A hit counter for the last five minutes. The deque keeps timestamps in order, so expired ones leave from the front. Python shown; the idea carries to every language in the editor.

Where it goes wrong

  • Choosing a structure before listing what each method must do.
  • Breaking an invariant on an error path.
  • Skipping the questions: size limits, duplicates and thread safety.

Say this before you type. Say each method, the cost you want from it, and the structure that earns that cost.

Where Coco comes in

Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit, and to talk with her for one hour. After that, talking comes with the $79 pack. Anyone can open the editor and run their code.

Questions people ask

How do I pick the data structure?
Start from the slowest operation you cannot afford. Choose the structure that makes it cheap, then check what that choice does to the others.
Do I write the whole class at once?
No. Get one method working against the example, then add the next. Staged requirements reward small steps.

Meet these next

Fin and Coco are characters, not people. strongyes.io is independent. Leo builds it with AI agents.