1. In a URL shortener, how do you generate short codes so two servers never issue the same one?
Seniorid-generationurl-shortener
What the interviewer is really testing
Whether you can get uniqueness across many stateless servers without putting a coordination call on every write, and whether you see the security side of predictable identifiers.
Show the model answer, follow-ups and checklist
Model answer
I would compare three options and pick one.
Random 7-character codes with a conditional write are the simplest. With 3.5 trillion possible codes and 6 billion links, a collision is rare, and “put if absent” turns one into a cheap retry. No coordination is needed.
A counter encoded in base 62 can never collide, so the write needs no check at all. The problem is where the counter lives. A database sequence called on every request is a bottleneck and a single point of failure.
The fix is range leasing. Each API server asks an allocator for a block of a million IDs, then issues them from memory. The allocator is one row in a consistent store, incremented once per million writes, so its load is negligible and servers keep working for hours if it is down. Unused IDs from a crashed server are skipped.
Because sequential IDs are guessable, I would scramble the counter with a reversible function before encoding it, which keeps uniqueness and hides the order.
Custom aliases go in the same table through a conditional write that returns 409 on conflict, with a naming rule that keeps them apart from generated codes.
I would choose leased ranges with a scramble, and say that random codes are a reasonable alternative if the team prefers fewer moving parts.
Likely follow-ups
- A server crashes after leasing a range of one million IDs and using ten. What happens to the rest?
- They are abandoned. The ID space is in the trillions, so gaps are free, and not reclaiming ranges keeps the allocator a single counter.
- How big should the leased range be?
- Big enough that the allocator is called rarely and servers survive an allocator outage, small enough that a crash wastes little. At tens of writes per second per server, a million IDs lasts hours.
- Users complain that links are guessable. How do you fix it without losing uniqueness?
- Apply a bijection to the counter before encoding, such as a Feistel network over the code space. Distinct inputs still map to distinct outputs, but consecutive IDs no longer look consecutive.
- Why not hash the long URL and take the first seven characters?
- Truncated hashes collide long before the space is full, so every write needs a lookup and retry. It also forces two users shortening the same URL to share one code and one set of analytics.
Common mistakes
- Calling a central counter on every request and not noticing it is a bottleneck and a single point of failure.
- Using a truncated hash and ignoring collisions.
- Using UUIDs, which are unique but far too long to be a short code.
- Forgetting custom aliases share the namespace with generated codes.
A strong answer includes
- Compares at least two approaches (hash, random, counter) with their costs
- Removes per-write coordination, usually with leased ranges
- Explains what happens to unused IDs on a crash
- Addresses guessability
- Handles custom aliases with a conditional write
- Sizes the code space against the expected number of links
Revise: Design a URL shortener