Loading
0xA0Lesson 11 of 13

Design a URL shortener

Walk through a classic prompt: short codes, redirects, and scale.

20 min 6-question quiz
By the end of this lesson you can
  • Scope and estimate a URL shortener.
  • Generate unique short codes with base62 encoding.
  • Choose between 301 and 302 redirects and a storage model.

Requirements. Functional: create a short link for a long URL; redirect a short link to the original; optionally custom aliases and expiry. Non-functional: redirects must be fast and highly available; the system is extremely read-heavy (each link is created once but clicked many times); codes should not be trivially guessable. Estimates: 100 million new links a month is ~40 writes per second; at 100:1 that is ~4,000 redirects per second. At ~500 bytes a link, five years is 100M × 60 months × 500 B ≈ 3 TB.

API, codes, and storage

  • API: POST /links {longUrl} returns {code}; GET /{code} responds with a redirect.
  • Code space: base62 (0-9, a-z, A-Z) with 7 characters gives 62^7 ≈ 3.5 trillion codes - plenty.
  • Generating codes: (1) hash the long URL and take 7 characters, checking for collisions; or (2) take a unique integer id and base62-encode it. To avoid a single counter, each app server reserves a block of ids (say 1,000 at a time) from a coordinator.
  • Storage: a key-value lookup code → {longUrl, createdAt, expiresAt, ownerId} fits a key-value or wide-column store, or a sharded relational table. Hot codes are cached.

Base62 works like any other base: 125 is 2 × 62 + 1, so it encodes as 21. Seven characters give 62^7, or about 3.5 trillion codes.

Redirects: a 301 (permanent) lets browsers cache the redirect, which cuts load but hides repeat clicks from analytics. A 302 (temporary) sends every click through the service, which enables analytics. Deep dives interviewers like: handling hash collisions, preventing enumeration of sequential codes, blocking malicious URLs, expiring links, and recording click analytics asynchronously through a queue so redirects stay fast.

Practice: size it

Try it

Up to 15 XP

Estimate a URL shortener

Assume 100 million new links a month, 100 redirects per link created, and about 500 bytes per link.

Question 1 of 4Score 0/4
writes/second
You can write 10000, 10k, 1e4 or 10^4.

Practice: survive the deep dive

Try it

Up to 15 XP

The interviewer digs in

You’ve drawn the high-level design, and now the questions start. Choose your replies, then try a different path.

  1. InterviewerHow do you generate the short code?

What do you say?

Put it together: a mock design

Try it

Up to 15 XP

Present a design out loud

Talk through your design as you would in the room, sketching on paper if you can. You have five minutes for the outline, so don’t get stuck in the detail.

Question 1

“Design a URL shortener.”

Testing: Read-heavy lookups

The question stays hidden until you start. Answer it out loud as you would in the room.

Key takeaways

  • Seven base62 characters give ~3.5 trillion codes.

  • Generate codes by hashing with collision checks, or by base62-encoding pre-allocated unique ids.

  • Use 302 when you need click analytics; cache hot codes to keep redirects fast.

Lesson quiz

6 questions · pass with 5 correct · up to 50 XP

Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.

Questions about this lesson

Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.

Loading posts…