Design a URL shortener
medium 45 minTurn a long URL into a short code and redirect anyone who opens it.
How to approach it
What this question tests, where it gets hard, and how to spend the time.
Read the question
What this problem tests
Can you stay simple?
The whole product is one lookup. Interviewers watch who adds queues and shards before a number asks for them.
Can you make unique IDs?
Many servers, no duplicates, short output, hard to guess. This is the hard part.
Can you serve reads?
Huge read volume, tiny responses, a few links far hotter than the rest.
Can you separate concerns?
Click counting is bigger than the redirect traffic looks, and it must not slow it.
Think
Five questions before you draw
| Ask yourself | Here |
|---|---|
| Who does what? | A creator makes a link; strangers open it; the creator reads statistics |
| What must never go wrong? | A link must never open the wrong page, and an issued link must not be lost |
| What is the hardest thing? | Short, unique, hard-to-guess codes from many servers |
| What breaks first as it grows? | Reads on the database, then total storage, then click volume |
| What am I giving up? | A 302 keeps control and counts at the price of more traffic; edge caching moves counting to edge logs |
The interview, in order (SACRED)
Time
Spend the 45 minutes
| Minutes | Do | Leave with | Skip |
|---|---|---|---|
| 0 to 5 | Clarify, choose three features, set targets | Scope on the board | QR codes, teams, themes |
| 5 to 10 | Only the sums that decide something | Reads a second, total storage, code length | Bandwidth, unless asked |
| 10 to 15 | Endpoints and main record | Create, redirect, statistics | User table fields |
| 15 to 25 | Version 1, then grow it | A working create and redirect | Naming every cloud product |
| 25 to 40 | Two deep dives: codes, then reads | A decision with a cost for each | Base62 arithmetic by hand |
| 40 to 45 | Failures, what you would do next, recap | A clear last sentence | New features |
Lines you can open with
- 1
Start
"I will agree the scope and targets first, get a simple version working, then spend most of our time on codes and the read path. Does that suit you?"
- 2
Park something
"Uniqueness across servers is the hard part. I will treat it as a function for now and come back to it."
- 3
When stuck
"Let me go back to the targets. Which one does my design not meet yet?"
- 4
Close
"Reads end at the edge or Redis, codes cannot collide, clicks are asynchronous. With more time I would test region failover."
Recognise
Patterns inside this problem
| Pattern | The signal | Where it lands |
|---|---|---|
| Scaling reads | 100 reads for each write, same few keys | Edge, Redis, key-value store |
| Unique IDs without coordination | Many writers, no duplicates | Leased ranges |
| Contention | Two people want one alias | Conditional write |
| Stream aggregation | Billions of small events, summary questions | Queue, windows, rollups |
Do
- Say the read to write ratio in the first five minutes
- Draw two boxes before ten
- Choose one ID scheme and defend it
- Say what happens when Redis is down
Traps
- Ten minutes of arithmetic with no decision
- Sharding on arrival
- "Hash it" with no collision plan
- Claiming edge caching and exact server-side counts together