URL shortening is a technique on the internet in which a Uniform Resource Locator (URL) can be made substantially shorter and still direct you to the required page. This shorter URL makes it easier to distribute the link.
This is achieved by using a URL Shortening service/utility which takes the longer URL as input and generates a shorter URL for it. When the Short URL is used to open the webpage, the utility gets this request and redirects you to the web page of the corresponding longer URL.
For example, the URL "https://en.wikipedia.org/wiki/URL_shortening" can be shortened to "https://w.wiki/U" and the URL "https://example.com/assets/category_B/subcategory_C/Foo/" can be shortened to "https://example.com/Foo"
We will create a utility called Short URL, like bit.ly. This utility will create a short URL for a given longer URL and store it.
- generate a short URL for a given longUrl and return the short URL .( Create )
- retrieve the longer URL based on the short URL ( Read )
- remove the longUrl mapping that is currently tracked ( **Delete **)
- custom shortUrl can be specified for a longUrl
- Number of successful lookups for a longUrl (using getUrl())
- The short URL could be one we've randomly generated or one specified by the user.
- Create a concrete class which implements a short URL interface class provided. It contains all the methods that can be used to create long URL to short URL mappings.
- Implement a method to take a long URL as input, generate a random 9 character short URL and return it. Also maintain this mapping using a hash map for quick retrieval.
- Implement a method to take a long URL and a custom short URL as input and register that mapping.
- Error handling to make sure each long URL maps to only one short URL and vice versa.
- Implement a method to lookup short URL for a given long URL, from the mapping created.
- Implement a method to delete the mapping based on a long URL.
- Implement a counter to keep track of the hit count for each long URL - which tells us how many times a particular long URL has been looked up.
- Test these methods using test cases.