How Far Have You Gotten in Leet — (3)

*** This solution heavily depends on yzhou78’s article

Description

resource: link

Explanation

To encode a long URL to a short URL and decode it back to the original, the two URLs need to pair up so that other URLs cannot get tangled up. For that, we use a hash table in algorithms.

When you store the pair into the table, It is key to make a unique short URL to represent the long URL given. Using a hashcode, you can make it easier. Hash code is a numeric value that helps in the identification of an object during equality testing and also can serve as an index for the object. It means that all the different objects have different hashcode and so, they should be unique.

That’s it. Once you get the hash code, convert it into a hex string, which would be an identifier in your short URL, and store them in the table together. So, the hex string value is the key to search the original URL.

Solution

Review

Creating a unique key was quite tricky. The solution seems straightforward and fast. But If you imagine that tons of URLs would be given simultaneously in a real-time service, it is not a perfect system design. Figuring out how to use a hash code is the great earning I got from this, anyway.

Backend Developer, Political Scientist, Writer, Tech Geek and Lifelong Learner