1.Input: given long URL : return a much shorter URL
2.URL re-direct: input: shorten URL, re-direct it to the original URL
Estimate the scale of the system you are going to design...
Define what APIs are expected from the system...
Generate shorten URL
Hash the longULR (hashKey) to shortURL (hashValue)
Re-direct:
Once service received a tinyurl request, it will map back to the original longULR key with 301 re-direct
URL mapping DB check shortURL -> longURL hash Map
Get longURL: hashTable.get(shortURL)
Perform the redirect based on the longURL
We could consider base 62 hash algorithm or hash collision resolution
Hash collision: MD 5 , CDC 32 which uses first 7 character
Approach 1 [chosen]Base 62: Support 62 character.
Pros:
No need to concern collision
URL length is not fixed, it goes up with ID
Cons: Require unique ID generator
Approach 2: Hash collision:
Pros:Fixed short ULR length
No need unique ID generator
Cons: expensive to query the DB and build mitigate for resolving collision (recursive to append pre-defined string)
Try to discuss as many failure scenarios/bottlenecks as possible.