Implement an in-memory URL shortener with idempotent base-62 short codes
Quick Overview
Implement a URL shortening library that maps each distinct long URL to a base-62 short code under a fixed domain and resolves short URLs back to the original. It tests class design, lookups in both directions, idempotent behavior for repeated URLs, and handling of invalid input.
Implement an in-memory URL shortener with idempotent base-62 short codes
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design and implement a URL shortening library that can shorten long URLs into compact short URL codes and retrieve the original URL from a shortened code.
The library should generate unique short URL codes for each distinct long URL. If the same long URL is shortened multiple times, the same short code should be returned. Short URL codes use a base-62 encoding scheme (`a-z`, `A-Z`, `0-9`) and are appended to the base domain `http://short.url/`.
Codes come from a counter. The first distinct long URL gets id 1, the next gets id 2, and so on. The code is the id written in base 62, most significant digit first, where `a` to `z` are the digit values 0 to 25, `A` to `Z` are 26 to 51, and `0` to `9` are 52 to 61. So id 1 is `b`, id 61 is `9`, and id 62 is `ba`.
If a short URL is not recognized or is invalid, the retrieval method should return an empty string.
Implement the class `URLShortener`:
- `URLShortener()` creates an empty shortener.
- `shorten_url(long_url: str) -> str` shortens the given long URL and returns the full short URL string. If the same long URL has been shortened before, the same short URL is returned.
- `get_original_url(short_url: str) -> str` returns the original long URL for the given short URL. If the short URL does not exist or is invalid, it returns an empty string.
Example 1:
```text
Input:
["URLShortener", "shorten_url", "shorten_url", "shorten_url", "get_original_url", "get_original_url"]
[[], ["https://example.com/a/very/long/path"], ["https://example.org/page"], ["https://example.com/a/very/long/path"], ["http://short.url/c"], ["hello"]]
Output: [null, "http://short.url/b", "http://short.url/c", "http://short.url/b", "https://example.org/page", ""]
```
The first long URL gets id 1 (`b`) and the second gets id 2 (`c`). Shortening the first URL again returns its existing short URL and uses no new id. `c` resolves to the second URL, and `hello` does not start with the base domain, so it returns an empty string.
Example 2:
```text
Input:
["URLShortener", "get_original_url", "shorten_url", "get_original_url", "get_original_url"]
[[], ["http://short.url/a"], ["https://www.test.com"], ["http://short.url/b"], ["http://short.url/xyz"]]
Output: [null, "", "http://short.url/b", "https://www.test.com", ""]
```
Nothing has been shortened when `http://short.url/a` is looked up, so it returns an empty string. The first URL gets id 1, which encodes to `b`, and `http://short.url/b` then resolves to it. The code `xyz` was never issued, so the last call returns an empty string.
```hint Both directions
Each method looks things up in a different direction. Think about what you need to store so that neither method has to scan every URL stored so far.
```
```hint Repeated and invalid input
Check what happens to the counter when a URL is shortened a second time, and what `get_original_url` should do with a string that does not start with the base domain.
```
### Constraints & Assumptions
- `1 <= long_url.length <= 10^4`
- `long_url` starts with `http://` or `https://`.
- `short_url` passed to `get_original_url` either starts with `http://short.url/` or is an arbitrary string.
- At most `10^4` calls are made to `shorten_url` and `get_original_url` combined.
### Clarifying Questions to Ask
- Should long URLs be compared as exact strings, or should `https://example.com` and `https://example.com/` count as the same URL?
- Does the mapping need to survive a restart, or is an in-memory library enough?
- Will the library be called from several threads at once?
- Is it acceptable that sequential codes are easy to guess?
### What a Strong Answer Covers
- The data kept for lookups in each direction, and the cost of each operation.
- Base-62 encoding of the counter that produces the expected codes, including the first code and the step from one digit to two.
- Idempotent shortening: a repeated long URL returns its existing code and does not consume a new id.
- Handling of short URLs that lack the base domain, have an empty code, or carry a code that was never issued.
- Time and space complexity in terms of the number of stored URLs and the URL length.
### Follow-up Questions
- How would you make the library safe to call from many threads at once?
- Sequential codes reveal how many links exist and let anyone enumerate them. How would you make codes hard to guess while keeping them short and unique?
- How would you persist the mappings, and what changes when several servers issue codes?
- How would you support custom aliases without colliding with codes the counter will issue later?
Overview: Implement a URL shortening library that maps each distinct long URL to a base-62 short code under a fixed domain and resolves short URLs back to the original. It tests class design, lookups in both directions, idempotent behavior for repeated URLs, and handling of invalid input.
Design and implement a URL shortening library that can shorten long URLs into compact short URL codes and retrieve the original URL from a shortened code.
The library should generate unique short URL codes for each distinct long URL. If the same long URL is shortened multiple times, the same short code should be returned. Short URL codes use a base-62 encoding scheme (a-z, A-Z, 0-9) and are appended to the base domain http://short.url/.
Codes come from a counter. The first distinct long URL gets id 1, the next gets id 2, and so on. The code is the id written in base 62, most significant digit first, where a to z are the digit values 0 to 25, A to Z are 26 to 51, and 0 to 9 are 52 to 61. So id 1 is b, id 61 is 9, and id 62 is ba.
If a short URL is not recognized or is invalid, the retrieval method should return an empty string.
Implement the class URLShortener:
URLShortener()
creates an empty shortener.
shorten_url(long_url: str) -> str
shortens the given long URL and returns the full short URL string. If the same long URL has been shortened before, the same short URL is returned.
get_original_url(short_url: str) -> str
returns the original long URL for the given short URL. If the short URL does not exist or is invalid, it returns an empty string.
The first long URL gets id 1 (b) and the second gets id 2 (c). Shortening the first URL again returns its existing short URL and uses no new id. c resolves to the second URL, and hello does not start with the base domain, so it returns an empty string.
Nothing has been shortened when http://short.url/a is looked up, so it returns an empty string. The first URL gets id 1, which encodes to b, and http://short.url/b then resolves to it. The code xyz was never issued, so the last call returns an empty string.
Constraints & Assumptions
1 <= long_url.length <= 10^4
long_url
starts with
http://
or
https://
.
short_url
passed to
get_original_url
either starts with
http://short.url/
or is an arbitrary string.
At most
10^4
calls are made to
shorten_url
and
get_original_url
combined.
Clarifying Questions to Ask Guidance
Should long URLs be compared as exact strings, or should
https://example.com
and
https://example.com/
count as the same URL?
Does the mapping need to survive a restart, or is an in-memory library enough?
Will the library be called from several threads at once?
Is it acceptable that sequential codes are easy to guess?
What a Strong Answer Covers Guidance
The data kept for lookups in each direction, and the cost of each operation.
Base-62 encoding of the counter that produces the expected codes, including the first code and the step from one digit to two.
Idempotent shortening: a repeated long URL returns its existing code and does not consume a new id.
Handling of short URLs that lack the base domain, have an empty code, or carry a code that was never issued.
Time and space complexity in terms of the number of stored URLs and the URL length.
Follow-up Questions Guidance
How would you make the library safe to call from many threads at once?
Sequential codes reveal how many links exist and let anyone enumerate them. How would you make codes hard to guess while keeping them short and unique?
How would you persist the mappings, and what changes when several servers issue codes?
How would you support custom aliases without colliding with codes the counter will issue later?