AlgoViz

Design a URL shortener

medium 45 min

Turn 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 yourselfHere
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)

Agree on what you are building before you draw a box, and say trade-offs out loud as you go.

Time

Spend the 45 minutes

MinutesDoLeave withSkip
0 to 5Clarify, choose three features, set targetsScope on the boardQR codes, teams, themes
5 to 10Only the sums that decide somethingReads a second, total storage, code lengthBandwidth, unless asked
10 to 15Endpoints and main recordCreate, redirect, statisticsUser table fields
15 to 25Version 1, then grow itA working create and redirectNaming every cloud product
25 to 40Two deep dives: codes, then readsA decision with a cost for eachBase62 arithmetic by hand
40 to 45Failures, what you would do next, recapA clear last sentenceNew features

Lines you can open with

  1. 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. 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. 3

    When stuck

    "Let me go back to the targets. Which one does my design not meet yet?"

  4. 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

PatternThe signalWhere it lands
Scaling reads100 reads for each write, same few keysEdge, Redis, key-value store
Unique IDs without coordinationMany writers, no duplicatesLeased ranges
ContentionTwo people want one aliasConditional write
Stream aggregationBillions of small events, summary questionsQueue, 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