~learn.
0:00 / 1:15
Interview problem · URL shortener

Six characters fit every link. So why use seven?

transcript

Six characters can hold five years of short links. All of them. So why does a good answer use seven? It's not about fitting.

Each character is one of 62 symbols. Lowercase, uppercase, digits. Assume 100 million new links a month, for five years. About 6 billion. Six characters give 62 to the 6th. About 56 point 8 billion codes.

So six fits, with room to spare. Done, right? But here's the catch. Fitting isn't the same as roomy.

6 billion links in 56 point 8 billion codes. About 11 percent full. Pick a random code. About one time in ten, it's taken. And a dense space is easy to walk through, code by code.

Now add one character. 62 to the 7th is about 3 point 5 trillion. The same 6 billion links fill about 0 point 17 percent. One extra character buys all that headroom.

So don't just ask if it fits. Ask how full it gets. 11 percent full collides. 0 point 17 percent full breathes.

Full breakdown in the lecture.