URL Shortener

Start Timer

0:00:00

Upvote
1
Downvote
Save question
View comments

Let’s say at Revolut we need a simple URL shortener. Implement a URLShortener class that stores all mappings in memory and has two methods:

  • shorten(long_url) returns a unique short code.
  • expand(short_code) returns the original URL, or None for an unknown code.

Also write tests covering at least idempotency, expand of a valid code, and expand of an invalid code.

Rules - Calling shorten again with an identical long URL string (exact string match, no normalization) must return the same code. - Codes come from a counter starting at 0, encoded in base 62 with the alphabet 0-9, a-z, A-Z and no padding, so the first code issued is '0'.

Example:

Input:

s = URLShortener()
code = s.shorten('https://revolut.com/careers')

Output:

s.expand(code) -> 'https://revolut.com/careers'
s.shorten('https://revolut.com/careers') == code -> True
s.expand('nope') -> None
.
.
.
.
.


Comments

Loading comments