How to design a system that takes big URLs like “https://www.geeksforgeeks.org/count-sum-of-digits-in-numbers-from-1-to-n/” and converts them into a short 6 character URL. It is given that URLs are stored in the database and every URL has an associated integer id.
One important thing to note is, the long URL should also be uniquely identifiable from the short URL. So we need a Bijective Function
We strongly recommend that you click here and practice it, before moving on to the solution.
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 the database and convert the integer to a 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
1) A lower case alphabet [‘a’ to ‘z’], total 26 characters
2) An upper case alphabet [‘A’ to ‘Z’], total 26 characters
3) 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 the database. The id can be obtained using base 62 to decimal conversion.
Generated short url is dnh Id from url is 12345
Optimization: We can avoid reverse step in idToShortURL(). To make sure that we get the same ID back, we also need to change shortURLtoID() to process characters from the end instead of the beginning.
This article is computed by Shivam. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Design a Hit Counter
- Design a DFA that accepts a string containing 3 a's and 3 b's
- Design a Chess Game
- Design a data structure for LRU Cache
- Design an efficient data structure for given operations
- Efficiently design Insert, Delete and Median queries on a set
- Design a Queue data structure to get minimum or maximum in O(1) time
- Design a stack that supports getMin() in O(1) time and O(1) extra space
- Design a data structure that supports insert, delete, getRandom in O(1) with duplicates
- Design the Data Structures(classes and objects)for a generic deck of cards
- Design a data structure that supports insert, delete, search and getRandom in constant time
- Maximum distance between two points in coordinate plane using Rotating Caliper's Method
- Check if a string can be split into two strings with same number of K-frequent characters
- Sort a Bitonic Array