31. Design a URL Shortener
1
The interview prompt2
31.1 What do you ask first?3
31.2 How big is this, really?4
31.3 What are the doors?5
31.4 What do we store?6
31.5 What does the whole system look like?7
31.6 How do you generate the short code?8
31.7 301 or 302?9
31.8 How do you keep redirects fast at 12,000 a second?10
31.9 How do you count clicks without slowing redirects?11
31.10 What would you say in the last five minutes?12
Score yourself13
Variations to practise14
Sources15
Code lab31. Design a URL Shortener
Score yourself
3 min read+10 XP
Score yourself
Give yourself 0, 1 or 2 points per row.
| Area | 0 points | 1 point | 2 points |
|---|---|---|---|
| Requirements | Started drawing | Listed features | Features and qualities, agreed and written down, scope trimmed |
| Estimates | None | Numbers, not used later | Numbers that drove decisions (cache, no sharding yet) |
| API and data | Skipped | Endpoints or table, not both | Both, with error cases and the code as primary key |
| High-level design | Missing pieces | Boxes, no walkthrough | Every requirement walked through the boxes |
| Deep dive | Hand-waved | One option described | Options compared, a pick with reasons |
| Trade-offs | None named | Named when asked | Raised unprompted ("because, cost, unless") |
| Communication | Long silences | Mostly clear | Thought out loud, checked in, managed time |
12 to 14: strong hire signal. 8 to 11: solid, polish the weak rows. Under 8: redo the round in a week, timer on.
The bar: fresher vs SDE-2
| Fresher | SDE-2 | |
|---|---|---|
| Codes | Base62 of a counter, explained clearly | Compares counter, hash and random; handles collisions, guessability and ID blocks |
| Scale | Adds a cache because reads dominate | Sizes the cache, handles hot keys and penetration |
| Redirects | Knows it's a redirect | Picks 301 vs 302 from the requirements |
| Analytics | Counter in the database | Async events, at-least-once, lag agreed with the interviewer |
| Failure | Mentions replicas | Walks each component's failure and fallback |
Common mistakes
- Hashing the URL with MD5, cutting it to 6 characters, and not mentioning collisions.
- Writing a click counter to the database on every redirect.
- Sharding on day one for 3 TB and 40 writes a second.
- Choosing 301 by habit, then promising click analytics.
- Forgetting abuse: a public shortener with no rate limit becomes a spam tool.
Quick check
Five questions on this chapter. Pick an answer, then check it.
quiz