This lesson is a full mock interview, written as a conversation. You see what a strong Senior candidate says, minute by minute, and what the interviewer is checking at each step. It reuses the numbers and choices from the design a URL shortener lesson, so read that lesson first. Here the focus is not the design. It is how you run the 45 minutes.
1. How to Use This Mock
- Set a 45-minute timer and a blank page.
- Read only the interviewer's lines. Answer each one out loud, as if you were the candidate.
- Then read the candidate's lines and the notes. Mark where your answer was weaker.
- Score yourself with the rubric in section 4.
Doing it out loud matters. Most people know the answer but fail to say it clearly, in order, with numbers.
2. The Plan and What Senior Means
| Minutes | Phase | What the interviewer wants to see |
|---|---|---|
| 0–5 | Requirements | Good questions, a clear scope, numbers agreed |
| 5–10 | Estimation | Per-day figures first, and a conclusion for each number |
| 10–15 | API and data | Simple API, a storage choice with a reason |
| 15–27 | Key generation | Options compared, one chosen, collisions handled |
| 27–37 | Redirect and cache | Cache layers, 301 vs 302, delete and expiry |
| 37–45 | Scale and failures | What breaks first, and what you do about it |
Background note: What "Senior" means in this round
A Senior candidate drives the conversation: they say the plan, make choices, and give reasons. They know the building blocks (cache, queue, sharding) and say what each one costs. They do not wait to be asked "what about failures?". A Staff candidate goes further: they talk about teams, migration and cost over years. That is not expected here.
3. Worked Example: The Mock Interview
3.1 Minutes 0–5: Requirements
Interviewer: Design a URL shortener.
Candidate: Before I start, I'll say my plan. I'll spend 5 minutes on requirements, 5 on numbers, then the API and data, then key generation and the redirect path, which I think are the interesting parts, and I'll finish with failures. Does that work for you? ... Good. A few questions. Do we need a custom alias and an expiry date? Do we need click analytics? And what scale should I assume?
Interviewer: Yes to alias and expiry. Basic analytics: clicks per day and country. Assume 20 million new links a day, 100 redirects for every new link, and links kept for 5 years.
Candidate: So my scope is: create a short link, redirect, optional alias and expiry, and click counts. I'm leaving out user accounts, QR codes and billing. For quality: redirects must be fast, under 50 ms at the server, and very available, 99.99%, which is about 52 minutes of downtime a year. A dead short link breaks a page that somebody else owns. Creation can be 99.9%, because a user can just retry. A link must never be lost, and one code must never point to two URLs.
Why this works: The candidate states the plan, asks only questions that change the design, writes down the scope and the non-goals, and gives different targets for redirect and create. That last part shows judgment.
3.2 Minutes 5–10: Estimation
Candidate: I'll start with per day. 20 million creates a day is 20,000,000 ÷ 86,400, about 231 a second. Redirects are 100 times more: 2 billion a day, about 23,100 a second on average. I plan for peak at 4 times average, so about 92,400 a second.
For servers: one m5.xlarge handles about 2,000 simple requests a second, so 92,400 ÷ 2,000 is about 46 servers. I want to survive the loss of one of three zones, so I multiply by 1.5. That is about 70 servers.
Storage: 20 million × 365 × 5 years is 36.5 billion links. At about 500 bytes each, that is about 18 TB. That is small for a sharded store, so storage is not the hard part.
Code length: with 62 characters (digits, lowercase, uppercase), 6 characters give 56.8 billion codes. We need 36.5 billion, so the space would be 64% full. That is too tight. 7 characters give about 3.5 trillion. I'll use 7.
So my conclusions are: this is a read-heavy system, writes are tiny, and the work is in caching and in giving out unique codes.
Interviewer: Why not 6 characters? 64% is still room.
Candidate: With random codes, a full space means many retries per create, and the retries grow as the space fills. With counter codes it would work, but I would have no room to grow or to skip ranges. The cost of one more character is nothing, so I take the safe side.
Why this works: Every number is followed by what it means. The candidate defends a choice without getting defensive, and gives a cost-based reason.
3.3 Minutes 10–15: API and Data
Candidate: Two main calls. POST /v1/links takes the long URL, an optional alias and an optional expiry, and returns 201 with the short URL. A taken alias returns 409. It also takes an idempotency key, so a retry does not create a second link. GET /{code} returns a redirect, 404 if the code is unknown and 410 if it has expired.
Data: one table, links(code PRIMARY KEY, long_url, owner_id, created_at, expires_at, status). The only questions we ask are "get by code" and "insert if this code is absent". No joins and no range scans, and billions of rows. That fits a key-value store such as DynamoDB, sharded by code, with a conditional write for uniqueness. A relational database sharded by code would also work. I choose key-value because we do not need relational features and the sharding is built in. Click events go to a separate analytics store.
3.4 Minutes 15–27: Key Generation
Candidate: There are three ways to make a code.
- Hash the URL and take 7 characters. Two different URLs can give the same code, so I need a check and a retry. The same URL always gives the same code, which also stops two owners from having separate links.
- Random code plus a check. It is simple, but every create needs a read or a conditional write, and retries grow as the space fills.
- Counter in base 62. Each number is unique, so there are no collisions by design.
I choose the counter. To avoid one central counter on every create, each create server asks the counter service for a block of 10,000 numbers and hands them out from memory. At peak, creates are about 924 a second (4 × 231), so all servers together use one block about every 11 seconds. The counter service sees less than one request a second.
Two weak points. First, if a server dies, the rest of its block is lost. That is up to 10,000 numbers out of 3.5 trillion, which is nothing. Second, counter codes are in order, so people can guess the next link. If links may be private, I scramble the number before encoding it: multiply by a large prime and take the remainder by 627. This is reversible, so every number still gets its own code, but the order is hidden.
import java.math.BigInteger;
import java.util.function.LongSupplier;
class CodeGenerator {
private static final String ALPHABET =
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
private static final BigInteger SPACE = BigInteger.valueOf(62).pow(7);
private static final BigInteger MIXER = BigInteger.valueOf(1_099_511_628_211L); // a prime
private final LongSupplier leaseBlock; // asks the counter service for the start of a block
private final long blockSize;
private long next, end; // numbers left in the current block
CodeGenerator(LongSupplier leaseBlock, long blockSize) {
this.leaseBlock = leaseBlock;
this.blockSize = blockSize;
}
synchronized String nextCode() {
if (next == end) { // block used up: lease a new one
next = leaseBlock.getAsLong();
end = next + blockSize;
}
long id = next++;
long mixed = BigInteger.valueOf(id).multiply(MIXER).mod(SPACE).longValue();
return encode(mixed);
}
static String encode(long n) {
char[] out = new char[7];
for (int i = 6; i >= 0; i--) {
out[i] = ALPHABET.charAt((int) (n % 62));
n /= 62;
}
return new String(out);
}
}
Interviewer: The counter service is down. What happens?
Candidate: Each create server keeps one spare block, fetched in the background before the current one runs out. With 3 create servers, each uses about 300 numbers a second at peak, so two blocks last about a minute. The counter itself is a replicated store, so it should seldom be down for long. If it is, creates fail with an error and retry. Redirects are not affected, and they are the part with the 99.99% target.
Interviewer: Two users shorten the same long URL. Same code?
Candidate: No, two codes by default. Each owner may want separate analytics, a different expiry, or to delete their link without hurting the other. I only avoid duplicates when the same request is retried, and the idempotency key does that.
Why this works: Three options with one-line costs, a choice, then the two weak points named before the interviewer asks. The candidate also puts the failure of the counter in the context of the SLO: redirects matter most.
3.5 Minutes 27–37: The Redirect Path
flowchart LR
U["Visitor"] --> LB["Load balancer"]
LB --> R["Redirect service"]
R --> L1["In-process cache"]
L1 --> L2[("Redis, 3 shards plus replicas")]
L2 --> DB[("Links store, sharded by code")]
R -.-> K[["Click events queue"]]
LB --> C["Create service"]
C --> DB
Candidate: A redirect looks in a small in-process cache, then in Redis, then in the store, and fills the caches on the way back. The click event goes to a queue without waiting, so analytics never slows a redirect.
Cache size: most clicks go to recent links. If I cache links from the last 5 days, that is 100 million links × about 200 bytes, about 20 GB. That fits in memory, but one Redis node does about 100,000 operations a second and my peak is 92,400, so I use 3 shards with replicas. With a 90% hit rate, the store sees about 9,200 reads a second.
I use 302, not 301. A 301 is cached by browsers for a long time, so I would lose click counts and could not change or disable a link. I accept the extra load.
Interviewer: A link is reported as a phishing page. How do you stop it fast?
Candidate: I set status = disabled in the store, then delete the key in Redis, and the other redirect servers' in-process copies expire within 60 seconds, because I give that cache a short TTL. So the link dies everywhere in about a minute. If a minute is too slow for this case, I broadcast an invalidation message to the redirect servers.
Interviewer: One link goes viral: 50,000 requests a second for one code.
Candidate: That is a hot key: it lands on one Redis shard. The in-process cache absorbs most of it, since every redirect server keeps its own copy for a few seconds. On a miss, I use single flight, so only one request per server goes to Redis. If that is not enough, I keep several copies of the key under different names and pick one at random.
Why this works: Each probe gets a direct answer, a mechanism, and a cost. The candidate uses words from earlier lessons (hot key, single flight, TTL) without explaining them again.
3.6 Minutes 37–45: Scale and Failures
Candidate: Let me say what breaks first. If Redis is down, the store gets 92,400 reads a second, ten times its normal load. So I add in-process caches, and I shed load to protect the store: reject some redirects with a quick error, instead of letting the whole store fall over. If a zone is lost, the 1.5 times headroom covers it. If the analytics pipeline is down, events wait in the queue, and counts are late, but no redirect fails. For abuse, I check each new URL against a block list and rate-limit creates per user (see the rate limiter lesson).
The first thing I would grow is the analytics store: 2 billion events a day is far more data than the links themselves, so it has its own pipeline and retention.
Interviewer: Good. We're at time.
4. Scoring Rubric
| Area | Below Senior | Senior |
|---|---|---|
| Scope | Starts drawing at once | Asks questions, writes scope and non-goals |
| Numbers | Gives numbers with no meaning | Per-day first, peak, then a conclusion for each |
| Key generation | "Use a hash" or "use a UUID" | Compares options, handles collisions and order |
| Redirect path | One database, no cache | Cache layers, 302 reasoning, delete and hot keys |
| Failures | Waits to be asked | Names what breaks first, with a fix |
| Talking | Long silences, jumps around | States the plan, keeps time, checks in |
Score each row 0 to 2 (0 = below, 1 = partly, 2 = Senior). Ten or more out of twelve is a pass. Two zeros in a row is a fail, whatever the total.
5. Common Pitfalls
- Numbers with no conclusion. "231 writes a second." So what? Say "that is tiny, one primary can do it."
- "Take the first 7 characters of a hash" and stop. You must say what happens on a collision.
- Choosing 301 without the cost. You lose click counts and cannot revoke a link.
- Putting analytics in the redirect path. A slow analytics store must never slow a redirect.
- Over-building early. Microservices, Kubernetes and three databases in the first 10 minutes, before a single number is known.
- Silence while thinking. Say "let me think about the options". The interviewer cannot score what they cannot hear.
- Never saying what you gave up. Every choice here has a cost. Name it.
6. Interview Questions
- Why 302 and not 301? A 301 is cached by browsers, so you lose click counts and cannot change or disable the link. A 302 costs extra load, which the cache absorbs.
- How do you stop people guessing codes? Scramble the counter value with a reversible step (multiply by a large prime, modulo 627), or use random codes with a conditional write.
- How do you handle one very hot link? In-process caches, single flight on misses, and several copies of the key if one shard is still too busy.
- How do you disable a link everywhere? Set its status in the store, delete the Redis key, and keep the in-process TTL short so the last copies expire quickly.
- What happens if the click pipeline is down? Events wait in the queue, counts are late, and redirects are not affected.
- What happens if the counter service is down? Servers use their spare blocks for about a minute at peak. Then creates fail and retry. Redirects keep working.