Interview problems
25 PROBLEMSFull write-ups for 25 interview problems, classic and AI: requirements, estimates, APIs, data model, and an architecture that grows from MVP to global scale.
Design a URL Shortener (TinyURL)
Design a service that turns long links into short 7-character codes, sends people on to the long link, and counts the clicks.
Think of TinyURL like a coat-check ticket at a theatre. You hand over a bulky winter coat, the long URL, and receive a tiny plastic token, the short code. Whenever you return the token, the attendant uses it to find your exact coat.
- Client
- Browser / App
- Edge / Ingress
- Cloudflare Edge
- Nginx L7 LB
- Application Tier
- URL Service Pod A
- URL Service Pod B
- KGS (Key Gen)
- Cache Tier
- Redis Cache
- Database Tier
- Cassandra Ring
1. Requirements and scope
- Given a long URL, generate a shorter, unique alias (e.g. tinyurl.com/a8B7d9).
- When a user opens the short link, redirect them to the original URL (HTTP 302, so each click can be counted).
- Support custom alias links and optional expiration dates (TTL).
- Count clicks, and record where each click came from: the page that linked to it and the rough location of the user.
- High availability (99.99% uptime, about 53 minutes of downtime a year): redirects matter most.
- Fast redirects: an example target is under 15 ms when the edge already has the link.
- Short codes should be hard to guess, so no one can walk through them in order and copy every link.
- Read-to-write ratio: 100:1 (heavily read-heavy system).
2. Back-of-the-envelope numbers
- Writes: 40 new links a second, on average (1%)
- Reads: 4,000 redirects a second, on average (99%)
Read-heavy: about 100 reads for each write. A cache and extra read copies help most here.
3. API
4. Data model and storage
URL_MAPPING Table: - short_code VARCHAR(7) PRIMARY KEY -- Base62 code - original_url VARCHAR(2048) NOT NULL - user_id BIGINT - created_at TIMESTAMP - expires_at TIMESTAMP - click_count BIGINT DEFAULT 0
5. The design, in three versions
Version 1: Simple MVP Architecture
Single application server connected to a relational database. Good for <10,000 users.
TinyURL v1: Monolithic Server Version 1 of 3
The picture follows one request through the parts of this version, in order. Under it, switch a part off to see what the request does without it.
One request, step by step
Each numbered arrow is one step. Press "Simulate Request Flow" to follow them one at a time.
- The order of the steps
- A request or its reply
The parts: switch one off
1. Client
Example figures
- Technology
- Chrome/iOS
- Speaks
- HTTPS/TLS
- Time budget
- 0ms
- Capacity
- 100K DAU
2. Edge / Ingress
Example figures
- Technology
- Nginx / Maglev
- Speaks
- TCP/HTTP
- Time budget
- < 2ms
- Capacity
- 50,000 QPS
3. Application Tier
Example figures
- Technology
- Go / Java
- Speaks
- gRPC/HTTP
- Time budget
- < 8ms
- Capacity
- 5,000 QPS
4. Cache Tier
Not in this version
5. Database Tier
Example figures
- Technology
- PostgreSQL 16
- Speaks
- SQL / CQL
- Time budget
- < 12ms
- Capacity
- 2,000 W/s
The steps in words0 of 4 steps shown
Browser to Nginx
GET /abc1234
LB to URL Service
Round-robin dispatch
URL Service to Database
SELECT original_url WHERE short_code
URL Service to Browser
Location: https://example.com/target
Write path, read path and failures
Each path follows one request through the final design (version 3), one hop at a time.
Write pathCreate a short link
- Client
- App servers
- Key generation service
- Sharded database
Client to App servers
The client sends POST /api/v1/urls with the long URL.
App servers to Key generation service
The app server takes the next unused code from its block of 1,000 codes. It asks for a new block only when the block runs out.
App servers to Sharded database
It saves the code and the long URL. The write is refused if the code already exists.
App servers to Client
It returns the short URL. Nothing is cached yet: the first read fills the cache.
Read pathOpen a short link
- Client
- CDN edge
- App servers
- Redis cluster
- Sharded database
- Kafka clickstream
Client to CDN edge
The browser asks for /a8B7d9. A nearby edge server answers if it has seen this code in the last minute, and logs the click.
CDN edge to App servers
On an edge miss, the request goes on to an app server.
App servers to Redis cluster
The app server looks the code up in Redis. Most lookups end here.
App servers to Sharded database
On a Redis miss, it reads the database and saves the answer in Redis with a TTL.
App servers to Client
It answers 302 with the long URL. Browsers do not keep a 302, so every click is seen and can be counted.
App servers to Kafka clickstream
Clicks from the app servers and from the edge logs go to Kafka. Counting happens later and never slows the redirect.
What breaks, and what happens then
Redis is down
- What users see
- Each click reads the database. As clicks grow, the database gets busy and each click waits longer.
- What the design does
- Read replicas are sized for the peak of 10,000 redirects a second. Each app server also keeps the hottest links in its own memory.
The key generation service is down
- What users see
- Redirects are not affected. New links work until each server's block of codes runs out.
- What the design does
- Run two copies of the service. Each hands out different ranges of numbers, so codes never collide.
Kafka is down
- What users see
- Redirects still work, but clicks are not counted for a while.
- What the design does
- App servers hold recent clicks in a small buffer and send them when Kafka returns. A few clicks may be lost; redirects never wait.
A database shard is down
- What users see
- Links on that shard cannot be created. Cached links still open.
- What the design does
- A replica of the shard takes over. Until then, the cache answers what it can.
6. Deep dives
7. Follow-up questions
- How do you purge expired short URLs? (Answer: Delete a link when someone opens it after it expired, and run a nightly batch job to sweep the rest).
- What if a user requests a short URL that does not exist? (Answer: Return 404. Cache that miss for a short time, or use a Bloom filter, so repeated lookups skip the database).
- How do you handle celebrity hot links? (Answer: Cache key in local process memory of application pods via Guava/Caffeine to protect Redis).
What it costs
Reads are about 100 times the writes, so the cache and the CDN carry most of the load. The data is small: about 600 GB a year. The first saving is to answer popular links at the edge, so they never reach your servers.
Another way to design it
You can skip the key service and hash the long link, then take the first 7 characters. That needs no shared counter. It does need a check for two links that give the same code. It suits a small service.
8. How answers are judged
A passing answer at Mid level. A complete design with numbers: about 40 new links and 4,000 redirects a second. It explains how codes stay unique and goes deep on one part, such as the cache or code generation.
| What is judged | Junior | Mid | Senior | Staff |
|---|---|---|---|---|
| BreadthHow much of the system does the answer cover? | Names the core parts: clients, servers, a database and a cache. | Covers the whole main flow, from the request to the stored data and back. | Builds a complete design without being led through it. | Covers the system, the systems around it and who owns each part. |
| DepthHow far does it go into the hard parts? | Explains what each part does and why it is there. | Goes deep on one part, such as the database or the cache. | Goes deep on the two or three riskiest parts. | Goes deepest where the risk is largest, and explains why the risk is there. |
| DrivingWho leads the conversation? | Follows the interviewer, step by step. | Moves forward alone, with an occasional nudge. | Leads the conversation from start to finish. | Changes the problem when the question asked is not the right one. |
| Trade-offsAre choices compared and justified? | Names more than one option. | Says why one option was picked over another. | Compares options in detail, with what each one costs. | Ties each choice to cost, speed and risk for the business. |
| Unclear questionsWhat happens when the question is vague? | Answers the questions that are asked. | Asks questions to make the problem clear. | Sets the scope and says what is left out. | Questions the assumptions behind the problem itself. |
| ScopeWhat does it consider beyond the normal path? | Covers the normal path, when everything works. | Adds what happens when a part fails. | Adds growth: what breaks at ten times the load, and the fix. | Adds abuse, cost and the daily work of running the system. |
Requirements and scope
Before I design, I want to check the scope. Users create a short link and anyone can open it. Do links expire? Can users choose their own code? Do we count clicks? I will assume redirects must be fast and must never break, and that reads outnumber writes about 100 to 1.
Users paste a long link and get a short one back. When someone clicks the short link, they go to the long one. Let me start drawing the servers.
Shows: Unclear questionsThe difference: The weak answer starts drawing before asking about expiry, custom codes or click counts, so it may design the wrong system.
Estimates
About 100 million new links a month is roughly 40 a second. At 100 reads per write, that is about 4,000 redirects a second, and maybe 10,000 at peak. Each link takes about 500 bytes, so a year of links is about 600 GB. One database can store that.
This will get huge traffic from millions of users, so from day one we need many data centres, sharding and a large cache cluster.
Shows: ScopeThe difference: The weak answer designs for a scale it never measured. The strong one turns rough numbers into rates and storage, and lets them pick the design.
Core design
Each new link gets the next number from a counter. A secret one-to-one mix scrambles it, and it is written as 7 letters and digits (Base62). Each number is used once, so codes never collide, and nobody can guess the next code. One table maps codes to long links; redirects check the cache, then the database.
I hash the long link with MD5 and keep the first 7 characters as the code. I save the code and the long link in a table, and a cache in front serves the redirects.
Shows: DepthThe difference: Two different links can get the same 7 characters from the hash, and the weak answer never checks for that. A code from a counter cannot collide.
Deep dive
The hard part is handing out unique codes from many servers. One database counter copes with 40 writes a second, but if it is down, nobody can create links. So each server reserves 1,000 numbers at a time and hands them out from memory. Numbers lost in a crash leave gaps, which do no harm.
Redirects are the main load, so I would add more cache. With a big enough Redis cluster, nearly every redirect is a cache hit and the database is safe.
Shows: DepthThe difference: The weak answer spends its deep dive on the easy part. The strong one picks the riskiest part, unique codes across servers, and shows the failure it avoids.
Failures and trade-offs
If the cache fails, all 4,000 redirects a second fall on the database. Read replicas sized for the peak of 10,000 a second keep redirects working while the cache refills. I would also pick a 302 over a 301. Every click then reaches us and can be counted, at the cost of a little speed.
If a server fails, the load balancer sends traffic to the others. Everything is replicated, so nothing can go wrong.
Shows: Trade-offsThe difference: The weak answer names one failure and waves the rest away. The strong one follows a real failure to its effect, and names what a choice gives up.
We wrote this guide in our own words, informed by public interview guides such as AlgoMaster and Hello Interview. We are not connected to either site. Interviewers differ. The guide and the example answers show the direction each level takes, not a checklist.
9. One problem, four levels
The same question, "design a URL shortener", answered at four levels. Each column shows only what that level adds to the one before it.
| Part of the answer | Junior | Mid adds | Senior adds | Staff adds |
|---|---|---|---|---|
| Requirements and scope |
|
|
|
|
| Estimates |
|
|
|
|
| Core design |
|
|
|
|
| Deep dive |
|
|
|
|
| Failures and trade-offs |
|
|
|
|
Requirements and scope
- Create a short link from a long one, and send anyone who opens the short link to the long one.
- New at MidSplits must-haves (create, redirect) from qualities: redirects are fast, links never break, and reads outnumber writes about 100 to 1.
Estimates
- Says the system is read-heavy: far more people open links than create them.
- New at MidAbout 40 new links and 4,000 redirects a second on average, about 10,000 at peak, and about 600 GB of new links a year.
Core design
- An API server and one table that maps each code to its long link, with a cache in front for popular links.
- New at MidCodes come from a counter, written as 7 letters and digits (Base62). A secret one-to-one mix scrambles each number first, so codes never repeat and cannot be guessed.
Deep dive
- Explains why the cache helps: popular links are read again and again, so most redirects never reach the database.
- New at MidExplains 301 against 302: browsers remember a 301, which is faster but hides later clicks from the click count.
Failures and trade-offs
- Says the database needs a backup.
- New at MidIf the cache goes down, redirects still work from the database, only slower.
10. Check your design with your own AI
Write your design first. Then copy this prompt into the AI chat you already use, and add your design at the end. It asks for a score in each area, one fix for each, and two follow-up questions.
The site sends nothing anywhere. An AI review can be wrong, so check what it says against sections 8 and 9.
See the prompt
You are a system design interviewer. Review my design for the interview problem below. Problem: Design a URL Shortener (TinyURL) Design a service that turns long links into short 7-character codes, sends people on to the long link, and counts the clicks. What users must be able to do: - Given a long URL, generate a shorter, unique alias (e.g. tinyurl.com/a8B7d9). - When a user opens the short link, redirect them to the original URL (HTTP 302, so each click can be counted). - Support custom alias links and optional expiration dates (TTL). - Count clicks, and record where each click came from: the page that linked to it and the rough location of the user. How well it must work: - High availability (99.99% uptime, about 53 minutes of downtime a year): redirects matter most. - Fast redirects: an example target is under 15 ms when the edge already has the link. - Short codes should be hard to guess, so no one can walk through them in order and copy every link. - Read-to-write ratio: 100:1 (heavily read-heavy system). Numbers to assume: - 100 Million new URLs generated per month (~40 writes/sec). - 10 Billion redirections per month (~4,000 reads/sec average). - URL mapping stored for 5 years retention. Judge my design at the Mid level. A passing answer at this level: A complete design with numbers: about 40 new links and 4,000 redirects a second. It explains how codes stay unique and goes deep on one part, such as the cache or code generation. For each area below, say whether my design is strong, fine or weak. Give one reason and one fix for each: 1. Requirements and scope 2. Estimates 3. Core design 4. Deep dive 5. Failures and trade-offs Then ask me the two follow-up questions an interviewer would ask next. Do not write a full model answer for me. My design: [Describe your parts, how one request moves through them, and why you chose each one.]