educative.io

Why can't we just generate random key?

Why can’t we just generate a random key to be used in the short URL instead of using KGS? If a key already exists, we can just generate another one. The chances that there’ll be a need to regenerate the key more than 1 time are very little.

I think this is something you can discuss with the interviewer. In some cases this can be a viable option. I would say this is something you can definitely calculate in order to defend your choice. After defining the rate of key generation requests, number of chars per key, the lifetime of each key you can calculate the probability of collisions over time.

Also note the interviewer can throw a curve ball that will force you to think of a different approach. For example say you have to reduce the size of your key from 7 chars to 6 chars, assuming base62, that’s a huge difference. If you do the math, with 6chars, 100M DAU (with just 10% write requests) the probability of collisions after 1 yr is already ~6%.

Other factors can also make your implementation difficult, what if a key should live forever, or if the traffic increases dramatically then the argument that the chances you’ll have to regenerate is very little can quickly fall apart.

4 Likes

When you say random key? How does it get generated? How are you so sure the random key generation algorithm will never have any collision? That is what you need to convince your interviewer and that is one of the key points of this design.

1 Like

very good question I want to know how do we proceed in this case ?