Design a URL Shortener
A classic interview question: design a service like TinyURL. Given https://www.systeminterview.com/q=chatsystem&c=loggedin&v=v3&l=long, it returns something like https://tinyurl.com/y7keocwj. Clicking the short link redirects you to the original.
- 8.1Requirements and estimationURL shortening: given a long URL → return a much shorter one URL redirecting: given a short URL → redirect to the original High…
- 8.2API endpoints, and 301 vs 302 redirectsTwo REST endpoints: POST api/v1/data/shorten, with body {longUrl: longURLString} → returns the shortURL GET api/v1/shortUrl → returns the…
- 8.3How long should the short code be?The code uses [0-9, a-z, A-Z]: 10 + 26 + 26 = 62 characters. We need the smallest n with 62ⁿ ≥ 365 billion. n Max number of URLs (62ⁿ) 1 62…
- 8.4Approach 1: Hash + collision resolutionUse a well-known hash function like CRC32, MD5 or SHA-1 and take the first 7 characters. For https://en.wikipedia.org/wiki/Systems_design:…
- 8.5Approach 2: Base-62 conversion ✅Base conversion is another common approach. Give every URL a unique numeric ID (from a distributed ID generator, Chapter 7), then write…
- 8.6Deep dive: the shortening and redirecting flowsShortening (with base 62) The input is a longURL. Check if it's already in the database. If so, return the existing shortURL. If not,…
- 8.7Wrap upWe covered the API design, data model, hash function, shortening and redirecting. With extra time, discuss: Rate limiter: malicious users…