Skip to lesson

Classic design questions practice

A review of this module. Skip it if you feel confident; the next module does not need it.

Fix the mistakes

The slips that cost marks in this module. Spot each one, then check the fix.

Each classic design question has a skeleton you can say in ten minutes and a few follow-ups that test whether you understood it. These drills practise outlines, spotting flaws, picking a fan-out strategy with numbers, and fixing a small encoder.

Timed outline: URL shortener and Pastebin in 10 minutes

A 10-minute outline for the URL shortener, then the three answers that change for Pastebin.

Example 1 · A 10-minute outline, then the Pastebin changes

Text
URL shortener, 10-minute outline 0-2   Requirements: create short link, redirect; fast, always up; 100 reads per write 2-4   Estimates: 1M links/day -> 12 writes/s, ~1,160 reads/s (peak ~5,800); ~1 TB in 5 years 4-5   API: POST /api/v1/urls -> 201 {code}; GET /{code} -> 302 Location 5-7   Codes: base62 of a counter from id ranges, 7 chars (start at 62^6) 7-9   Data + read path: urls(code PK, long_url, expires_at); Redis LRU for hot links 9-10  Extras: custom alias (insert if free), expiry (410), clicks via queue + workerPastebin, the same outline with three changes  Estimates: the text is the big number (~1.8 TB) -> text in object storage, metadata in MySQL  Keys: 8 random base62 chars from SecureRandom (unlisted pastes must be unguessable)  Expiry: check on read + batched cleanup job; CDN max-age <= time left
Time plan
10 minutes: 2 + 2 + 1 + 2 + 2 + 1
Pastebin changes: storage, keys, expiry

What this code does

  • Every line has a number or a concrete choice, so the interviewer can pick any line and ask "why?".
  • The Pastebin part lists only the differences, which is how you should reuse a design you already know.

The first 10 minutes of a round should cover requirements, estimates, API, the key idea (how codes are made), data, the read path and the extras. Pastebin uses the same outline; only three answers change: the text is the big number, so it goes to object storage; keys must be random because unlisted pastes are private; and expiry needs a cleanup job.

Practise saying the outline aloud with a timer. If one part takes over 2 minutes, cut detail there; the deep dive comes later. Review: Design a URL shortener (opens in a new tab) and Design Pastebin (opens in a new tab)

Find the flaw: a weak notification design

Five numbered points of a weak notification design. Find each flaw before you read the fixes.

Example 2 · Five flaws in a notification design

Text
Weak design (as a candidate said it):  (1) POST /orders saves the order, then calls sendEmail(), sendSms() and      sendPush() one after another before answering.  (2) If any call fails, the order service retries the whole notification step.  (3) All messages, OTPs and sale offers, share one queue "notifications".  (4) A worker that crashes after sending simply gets the job again.  (5) Promotions go out at any hour, as many as marketing wants.
Flaws and fixes
(1) User waits for 3 providers -> enqueue jobs, return 202; workers send.
(2) Whole step retried -> one job per channel, retried alone, with backoff.
(3) OTPs stuck behind a sale campaign -> separate urgent queue and workers.
(4) Crash after send gives a duplicate -> dedup key + provider idempotency key.
(5) No limits -> per-user daily cap, quiet hours, preferences.

What this code does

  • Each numbered line in the design maps to exactly one flaw and one fix.
  • The four questions (what waits, what retries, what fails, what blocks) find all five.

Interviewers often describe a design and ask "what is wrong with this?". Read it one numbered point at a time and ask the same four questions: what does the user wait for, what happens on a retry, what happens when a provider is down, and what can block what.

The weak design fails all four. Naming each flaw with its fix, in one line each, is the answer they want. Review: Design a notification system (opens in a new tab)

Choosing a fan-out strategy for three feeds

Three feeds with different authors, and a program that counts push inserts and pull lookups for each.

Example 3 · Push, pull or hybrid for three scenarios

FanoutChoiceDemo.javaJava
record Scenario(String name, long postsPerDay, long avgFollowers, long maxFollowers,                long feedReadsPerDay, long followeesPerReader) {}
public class FanoutChoiceDemo {    static final long CELEBRITY = 100_000;
    static String choose(Scenario s) {        if (s.maxFollowers() < CELEBRITY) {            return "push (no author is big enough to hurt)";        }        if (s.avgFollowers() >= CELEBRITY) {            return "pull (almost every author is big)";        }        return "hybrid (push normal authors, pull the big ones)";    }
    public static void main(String[] args) {        Scenario[] scenarios = {            new Scenario("Campus club app", 2_000, 150, 6_000, 400_000, 40),            new Scenario("News publishers app", 5_000, 2_000_000, 9_000_000, 50_000_000, 20),            new Scenario("General social app", 2_000_000, 200, 5_000_000, 100_000_000, 300),        };        for (Scenario s : scenarios) {            long pushInserts = s.postsPerDay() * s.avgFollowers();            long pullLookups = s.feedReadsPerDay() * s.followeesPerReader();            System.out.println(s.name());            System.out.println("  push inserts/day: " + pushInserts + ", pull lookups/day: " + pullLookups);            System.out.println("  choice: " + choose(s));        }    }}
Output
Campus club app
  push inserts/day: 300000, pull lookups/day: 16000000
  choice: push (no author is big enough to hurt)
News publishers app
  push inserts/day: 10000000000, pull lookups/day: 1000000000
  choice: pull (almost every author is big)
General social app
  push inserts/day: 400000000, pull lookups/day: 30000000000
  choice: hybrid (push normal authors, pull the big ones)

What this code does

  • The campus app has small authors: 300,000 inserts a day is cheap, so push.
  • The news app's authors all have millions of followers: push would be 10 billion inserts a day, while pull is 1 billion lookups, so pull.
  • The general social app has mostly small authors plus a few stars, so hybrid: push for most, pull above 100,000 followers.

If no author is big, push is cheap and reads are fast. If nearly every author is huge (a news app with a few big publishers), pull wins. Most social apps are in between, so they use a hybrid with a follower threshold.

The fan-out choice is made with numbers, not taste: push inserts per day (posts × average followers) against pull lookups per day (feed reads × accounts followed), and the largest account. Review: Design a news feed (opens in a new tab)

Fixing a base62 encoder bug

A base62 encoder with the reverse missing. The output for 125 shows the bug.

Example 4 · The missing reverse in a base62 encoder

Base62BugDemo.javaJava
public class Base62BugDemo {    static final String ALPHABET = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    // Buggy: no reverse, and 0 becomes an empty code    static String buggyEncode(long n) {        StringBuilder sb = new StringBuilder();        while (n > 0) {            sb.append(ALPHABET.charAt((int) (n % 62)));            n /= 62;        }        return sb.toString();    }
    static String encode(long n) {        if (n == 0) {            return "0";        }        StringBuilder sb = new StringBuilder();        while (n > 0) {            sb.append(ALPHABET.charAt((int) (n % 62)));            n /= 62;        }        return sb.reverse().toString();    }
    public static void main(String[] args) {        long[] ids = {0, 7, 63, 125, 64};        for (long id : ids) {            System.out.println(id + ": buggy='" + buggyEncode(id) + "' fixed='" + encode(id) + "'");        }    }}
Output
0: buggy='' fixed='0'
7: buggy='7' fixed='7'
63: buggy='11' fixed='11'
125: buggy='12' fixed='21'
64: buggy='21' fixed='12'

What this code does

  • Without reverse, codes for multi-symbol numbers come out backwards; 0 becomes an empty code.
  • Buggy 125 gives 12, which is the real code of 64, so two links would collide.
  • Single-symbol numbers like 7 look correct either way, which is why small tests miss this bug.

A base62 encoder turns a number into a short code by taking remainders by 62. The two classic bugs are forgetting to reverse the symbols, and returning an empty code for 0.

The missing reverse is the dangerous one: id 125 gets code 12, which is the correct code of id 64. Two links now share a code, so a user can be sent to someone else's URL. A round-trip test, decode(encode(n)) == n for many numbers, catches it at once.

Interview drill: the follow-up questions

The standard follow-ups for each classic problem, with a one-line answer for each.

Example 5 · Standard follow-ups with one-line answers

Text
URL shortener  Q: Two servers create links at once. Can codes clash?   A: Not with id ranges per server.  Q: A link goes viral.                                  A: Hot key in cache; CDN/301 if no analytics.  Q: How do you stop people walking through codes?       A: Random codes or rate limits on misses.Pastebin  Q: Why not store text in MySQL?                        A: 30x bigger than metadata; object storage is cheaper.  Q: A paste is 50 MB.                                   A: 413; or allow via direct upload to storage.Rate limiter  Q: Redis is down.                                      A: Fail-open for reads, fail-closed for login/OTP.  Q: Why not limit per server?                           A: Traffic spreads; limit x servers gets through.Notifications  Q: How do you avoid a duplicate SMS?                   A: Dedup key + provider idempotency key.  Q: The SMS provider is down for an hour.               A: Backoff retries, DLQ, fallback provider.News feed  Q: A celebrity with 5M followers posts.                A: Do not fan out; merge at read time.  Q: The user scrolls while new posts arrive.            A: Cursor (time, id), not offset.
Count
11 follow-ups across 5 classic problems

What this code does

  • Each answer names the problem and the fix in a few words; say it in one or two full sentences in the interview.
  • Practise by covering the A: column and answering aloud before you look.

After your outline, interviewers push on one weak point with a follow-up question, such as "a link goes viral" or "Redis is down". Each classic problem has a handful of standard follow-ups.

Answer each in one or two sentences: the problem it causes, then the fix from this module. Saying "it depends" is fine only when you say what it depends on. Review: Design a rate limiter as a service (opens in a new tab)

Finish up

Mark complete & continue

Next: Project: URL shortener design and simulation