How to design a system that takes big URLs like “https://www.geeksforgeeks.org/count-sum-of-digits-in-numbers-from-1-to-n/%E2%80%9D and converts them into a short 6 character URL. It is given that URLs are stored in database and every URL has an associated integer id.
One important thing to note is, the long url should also be uniquely identifiable from short url. So we need a Bijective Function.
One Simple Solution could be Hashing. Use a hash function to convert long string to short string. In hashing, that may be collisions (2 long urls map to same short url) and we need a unique short url for every long url so that we can access long url back.
A Better Solution is to use the integer id stored in database and convert the integer to character string that is at most 6 characters long. This problem can basically seen as a base conversion problem where we have a 10 digit input number and we want to convert it into a 6 character long string.
Below is one important observation about possible characters in URL.
A URL character can be one of the following:
- A lower case alphabet [‘a’ to ‘z’], total 26 characters
- An upper case alphabet [‘A’ to ‘Z’], total 26 characters
- A digit [‘0’ to ‘9’], total 10 characters
There are total 26 + 26 + 10 = 62 possible characters.
So the task is to convert a decimal number to base 62 number.
To get the original long url, we need to get url id in database. The id can be obtained using base 62 to decimal conversion.
Here is some code in ruby that implements a Base62 encoder and decoder:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
And here is an example:
1 2 3 4 5 6
Stay tuned for a simple personal URL manager coming shortly…