Skip to content
Tech Interview Prep
Technical interview guide

URL Shortener Design

Designing a URL shortener: unique keys, redirect semantics, cache TTLs, click accounting off the GET path, and open-redirect abuse.

Read
38 min
Practice MCQs
25
Interview Q&A
25
Edition
v4
Editorial status
Reviewed

Scope: HTTP per RFC 9110 and RFC 6585; UUIDs per RFC 9562; uniqueness illustrated with PostgreSQL unique indexes and Redis GET; abuse controls per OWASP and Unicode TR36.

Curated: · Written: · Reviewed:

25 questions

MCQ-24A URL shortener mints keys by base62-encoding a monotonic counter, left-padded to 6 characters (62^6 ≈ 56.8 billion values). The service takes a sustained 5,000 new URLs per second and never reuses a key. Which conclusion about the 6-character choice is correct?
MCQ-3A URL shortener mints a 6-character code for each URL by taking the first 6 characters of the base62 encoding of SHA-256(url). After 200,000 distinct URLs, two different URLs resolve to the same code. The design doc justified the scheme with 62^6 ≈ 5.68 × 10^10 possible codes. Which diagnosis is correct?
MCQ-4A URL shortener issues 7-character keys from a 62-symbol alphabet (62^7 ≈ 3.5 × 10^12). Three regional writers each own a separate database and cannot call a shared service, and the product team does not want a key to reveal how many links exist or in what order they were created. An engineer proposes replacing the base62 key with the first 7 hex characters of a UUIDv4. What does that proposal produce?
MCQ-13A URL shortener architecture switches from hashing long URLs with a distributed counter retry loop to an offline Key Generation Service (KGS) that dispenses pre-generated Base62 tokens. How does this structural change eliminate write collisions during concurrent creation requests?
MCQ-9A URL shortener must sustain 5,000 new-link writes per second. Two key schemes are benchmarked against the same table, unique index and connection pool: (A) truncate a SHA-256 hash of the target URL to 7 Base62 characters; (B) draw pre-allocated keys from a Key Generation Service. Scheme A's p99 insert latency is 180 ms and climbs as the table grows; scheme B holds 30 ms. Which property of scheme B accounts for the difference?
MCQ-5A URL shortener generates a 7-character base62 key (62^7 ≈ 3.5×10^12 values) for each new link and stores it in PostgreSQL as `links(public_key text PRIMARY KEY, owner_id bigint, destination_url text)`. The insert path runs `SELECT 1 FROM links WHERE public_key = $1` and, on a miss, `INSERT`. The table holds 100M rows and the service takes 500 inserts/s, so a freshly generated key already exists about once every 35,000 inserts; each of those raises `duplicate key value violates unique constraint "links_pkey"` and returns a 500. Separately, when a client retries a POST after a timeout, two rows exist for the same owner and destination. Which change fixes both problems?
MCQ-10Two concurrent HTTP POST requests attempt to create a shortened link with the identical custom alias 'launch' for the same destination URL 'https://example.com/product'. Which mechanism correctly ensures idempotent creation without race conditions or inconsistent client responses?
MCQ-23A short-link service runs active-active in us-east and eu-west. Each region mints a 6-character key from [a-z0-9] (36 symbols) for every new link and replicates it to the other region. Each region creates 200 links/second. The create path must return within a 20 ms p99 budget, and the inter-region RTT is 90 ms. The team is seeing duplicate keys across regions. Which change removes the duplicates?
MCQ-20A ticketing platform mints public order codes as 8 characters over base62 (62^8 ≈ 2.2×10^14). Support reads codes aloud over the phone, and about 4% of calls stall on a mis-heard character (0/O, 1/l/I). The team wants to switch the generator to Crockford base32, which drops I, L, O and U. The platform has already issued 3×10^12 codes and mints 1×10^9 per day. What should the team do?
MCQ-12A high-volume URL shortener serves paid ad campaigns. Attribution requires the shortener to write a server-side log entry for every click on a campaign slug, and the team wants to keep origin infrastructure spend down. Which redirect status should campaign slugs return, and what does that choice cost?
MCQ-22A short-link service returns `302` with `Location` and `Cache-Control: public, max-age=300`, behind a CDN. A POP caches one link's redirect at 08:58. At 09:00:05 an abuse takedown makes the origin return `410` for that key, with no purge issued. A reporter in that POP's region requests the link at 09:01. What does the reporter get, and what is the correct fix?
MCQ-1Your API gateway answers every POST /checkout with a 302 redirect to one of two backends, and ops flips the target between checkout-a and checkout-b as the canary percentage changes. Third-party SDKs follow redirects. Redirected checkouts reach the backend as GET with an empty body and fail with 422 missing items[]. Which change fixes the failures without breaking the canary?
MCQ-19A link shortener serves its 302 redirect over HTTPS and sets `Referrer-Policy: unsafe-url` on the response. The destination host is not on the HSTS preload list and has not previously sent Strict-Transport-Security. A user clicks a short link whose Location is an http:// URL. Which description of what the browser does is correct?
MCQ-11A single viral link's slug hashes to one shard. The redirect service runs cache-aside with an LRU cache in front of that shard, and the entry carries a 60-second TTL. Sustained traffic to the link is 40,000 redirects per second. Which statement describes which layer absorbs the hot key and what the shard sees when the entry expires?
MCQ-8An attacker targets a URL shortener with a cache-penetration attack, querying millions of randomized, non-existent slugs per minute. The read cache misses on every request, saturating database connection pools. How does negative caching mitigate this vulnerability while limiting side effects on newly created slugs?
MCQ-14A URL shortener takes 1 million new links/day and 10 billion redirects/day. Creates peak at 16x the daily average; redirects peak at 4x. The mapping table runs on one Postgres primary measured at ~200 inserts/s before p99 write latency crosses 50 ms. A Redis cache fronts redirect reads, and a single Redis INCR counter issues unique IDs (one round trip per create). Which conclusion about the write path is correct?
MCQ-15A URL shortener stores key → long_url in one Postgres primary that now peaks at 9,000 writes/s and 180,000 reads/s, and the primary is CPU-saturated. To spread writes, the team proposes sharding the table across 16 nodes by hash(long_url), so that duplicate submissions of the same destination land on one node and dedup becomes a local check. What does that sharding choice do to the read path?
MCQ-2A shortener serves 40,000 redirects per second at a p99 of 12 ms. Each click currently runs an UPDATE on the links row and an INSERT into clicks inside the same request; during a campaign spike the write pool saturates and redirect p99 crosses 900 ms, while the analytics team still needs every click for attribution. Which change addresses the cause?
MCQ-21An analytics pipeline for a link shortener writes one row per click: timestamp, short code, IP address, full User-Agent, and referrer. It handles 400M clicks a day. The fraud team needs to detect repeat-click abuse by the same client within a 30-day window. The analytics team needs per-link daily click counts for two years. Legal has classified the IP address as personal data and requires data minimisation. Which retention scheme satisfies all three requirements?
MCQ-7A link shortener validates a destination before issuing its 302 with `if (target.startsWith('https://') && target.includes('yourbrand.example'))`. A pentester submits `https://yourbrand.example.evil.example/login`. Which statement describes the outcome and its cause?
MCQ-6A link shortener stores user-chosen aliases in a PostgreSQL `citext` column with a unique index. An alias is stored only if the validator returns True and the insert does not violate the unique index. On insert, Python 3.11 runs: ```python import re RESERVED = {"api", "health", "v1", "admin", "login"} def valid(alias): return bool(re.fullmatch(r"[\w-]{1,32}", alias)) and alias.lower() not in RESERVED ``` The table already holds `apple` and `my-launch`. Which submitted alias is stored?
MCQ-18A URL shortener limits POST /v1/links with a token bucket keyed by client IP: 60 creates/min. Two incidents land the same week. A corporate customer whose 4,000 employees share one NAT egress IP reports that after the first 60 creates each minute, every user gets 429. Separately, one authenticated account running a buggy integration through a 20,000-IP proxy pool mints 300,000 links/day and is never limited. Which change fixes both?
MCQ-16You are rebuilding the write path of a high-traffic URL shortener. Threat-intel lookups (Safe Browsing API, local blocklist replicas) currently run inline on link creation, pushing p99 to 800 ms and failing creation outright whenever a feed is degraded, so scanning has to move off the request path. Two read-path constraints remain: a short link must never issue an automatic redirect to a destination that has not been cleared, and the first fetch of a new link, usually a messaging-app preview crawler arriving within a second of creation, must return a usable page, because a non-2xx response gets cached as a broken link for 24 hours. Which design meets both?
MCQ-17A short link is created and served immediately, and an asynchronous abuse scanner flags its destination as phishing forty minutes later. Which mechanism correctly performs retroactive takedown, and what state must the mapping record so that the redirect path can refuse the link without a synchronous scan?
MCQ-25A 15-person company wants short links for its internal wiki and its customer emails: about 200 new links a week, 40 redirects per second at peak, and destinations restricted to the company's own domains. Which design fits?