Software Engineer interview question
How would you design a URL shortener?
A URL shortener needs a collision-safe ID generator, durable mapping storage, a read-optimized redirect path, caching for hot links, abuse controls, expiry rules, analytics off the critical path, and capacity estimates.
Direct answer
What a strong answer covers
How it works
The core is a mapping from a short code to a long URL with a read path that must be fast and highly available and a write path that is rare by comparison. Codes come from a base62 encoding of a counter or from random IDs checked for collision; the mapping lives in a key-value store or a simple table with the code as primary key; hot codes are cached at the edge; the redirect is a 301 or 302 depending on whether analytics need every hit. Abuse controls scan destinations, rate-limit creation, and allow takedown. Analytics are written asynchronously so a slow pipeline never delays a redirect. Capacity is estimated from write rate, read rate, and retention.
Example
For 100 million links a year and 10 billion redirects, a 7-character base62 code space is ample; writes go to a partitioned key-value store with the code as key; a CDN caches redirects for popular codes; each hit publishes an event to a stream for counting. Creation requires an API key with per-key quotas, and new destinations pass through a safe-browsing check before the link becomes active.
Common mistakes
- Using an auto-incrementing integer directly as the code so links are enumerable.
- Returning a 301 while promising per-click analytics that browsers will then skip.
- Putting the analytics write on the redirect path and making every click wait for it.
A useful answer structure
Answer from your own work.
Do not memorise a script: use only examples you can explain honestly in follow-up questions.
Open the desktop workspace and rehearse a clear answer in your own words.