Skip to content
Theory · URL shortener · lesson 9 of 10

What interviewers ask

In 45 minutes of interview you need to cover the whole path of this chapter: from questions to the interviewer to the trade-offs. Time is short, so the order decides. A candidate who spends 20 minutes drawing boxes without numbers usually never reaches the questions the interviewer grades on.

How to split 45 minutes

  1. 5–7 minutes: questions about requirements and load, with the answers written down.
  2. 5 minutes: estimates — 40 writes and 4,000 reads a second, 1.2 TB over 5 years, code length.
  3. 5 minutes: the four API calls and the links table.
  4. 10–15 minutes: the diagram step by step — two paths, cache and store, codes, click counting.
  5. 10 minutes: a deep dive into whatever the interviewer picks, and failures.
  6. The last minutes: trade-offs and your questions.

If the interviewer steers the talk into one topic, follow: they are showing what they want to check.

Practice step 4: every block is already on the canvas, and most links are drawn. Find the mistake from the lesson on the API and data, and draw what is missing.

Sketch

Links are created at 40 a second, redirects run at 4,000 on average and 40,000 at peak, a redirect takes under 50 ms, and statistics may lag by an hour. Every block is in place. The diagram has one mistake and is missing three links. Fix it.

Questions after the design

Once the diagram is done, the interviewer asks follow-up questions. Four come up more often than others.

Race for an alias

"Two users ask for the alias sale at the same time. What happens?" Checking that the alias is free and then writing it does not work: both see "free" and both write. The write must be conditional and atomic, for example INSERT … IF NOT EXISTS in Cassandra. One request wins, the other gets 409. A conditional write is slower than a plain one, but there are only about 40 new links a second, so the cost is small.

"How does a link stop working after its expiry?" The expiry date sits in the cache next to the URL. The redirect service checks expires_at on every answer and returns 404. The link service removes a deleted link from the cache at once. The rows themselves can be deleted later by a background job: the code will not come back into use, because the counter only moves forward. There is no rush to delete: all links over 5 years take about 1.2 TB.

Statistics at scale

"Will counting hold 4,000 events a second?" The queue is partitioned by code, so one link's events reach one aggregator instance. The aggregator sums them in memory and writes to click_counts once a minute, not on every event. With at-least-once delivery an event may arrive twice, and the aggregator counts the click twice. For click statistics that is usually acceptable, and if not, events carry an ID and duplicates are dropped.

Multiple regions

"Users are all over the world — what then?" A second region gets an asynchronous copy of the data. In a disaster DNS moves traffic there, and the links created in the last seconds before it may be lost. Each region has its own half of the counter values, so both can create links without coordinating with each other.

How the solution is graded

The interviewer grades the solution along six dimensions, and the practice on the site checks the same six:

  1. Requirements: questions about load, latency, availability and the scope of the prompt.
  2. API and data model: the create and redirect calls, the table mapping a code to a URL, the click data.
  3. Architecture: separate read and write paths, unique codes, counting off the redirect path.
  4. Scaling: estimates, popular links, a partitioned store.
  5. Reliability: behavior when a node, the cache, counting or a region fails.
  6. Trade-offs: 301 or 302, how codes are made, where stale data is acceptable and why this store was chosen.

Common mistake

Naming technologies instead of reasons: "We'll take Cassandra and Kafka." The interviewer will ask why. The answer must rest on the requirements: each redirect is a lookup by one key, the data is 1.2 TB, statistics may lag by an hour. A technology without a reason scores lower than a reason without a technology.

Key points

  • 45 minutes: requirements and estimates about 10, the diagram about 15, the rest on deep dives and trade-offs.
  • An alias is claimed with a conditional atomic write, and the loser gets 409.
  • Counting scales with a queue partitioned by code and infrequent writes of the summed numbers.
  • The solution is graded on six dimensions: requirements, API and data, architecture, scaling, reliability, trade-offs.