Encode and Decode TinyURL - Problem
TinyURL is a URL shortening service that transforms long web addresses into compact, shareable links. Imagine you have a lengthy URL like https://leetcode.com/problems/design-tinyurl and you want to share it on social media where character count matters. TinyURL converts it into something like http://tinyurl.com/4e9iAk.
Your task: Design a class that can both encode a long URL into a short URL and decode a short URL back to the original long URL.
Requirements:
- Implement a
Solutionclass with constructor,encode(longUrl), anddecode(shortUrl)methods - The encoding algorithm is completely up to you - be creative!
- Guarantee that any URL encoded by your system can be decoded back to the original
- Handle multiple URLs and ensure each gets a unique short code
This problem tests your understanding of hash tables, string manipulation, and system design principles.
Input & Output
example_1.py โ Basic Encoding and Decoding
$
Input:
codec = Codec()
originalUrl = "https://leetcode.com/problems/design-tinyurl"
shortUrl = codec.encode(originalUrl)
decodedUrl = codec.decode(shortUrl)
โบ
Output:
originalUrl: "https://leetcode.com/problems/design-tinyurl"
shortUrl: "http://tinyurl.com/4e9iAk" (example)
decodedUrl: "https://leetcode.com/problems/design-tinyurl"
๐ก Note:
The codec successfully encodes a long URL into a short URL and decodes it back to the original URL. The short URL format and code can vary based on implementation.
example_2.py โ Duplicate URL Handling
$
Input:
codec = Codec()
url = "https://www.google.com"
short1 = codec.encode(url)
short2 = codec.encode(url) # Same URL again
โบ
Output:
short1: "http://tinyurl.com/xY2bv"
short2: "http://tinyurl.com/xY2bv"
short1 == short2: True
๐ก Note:
When the same long URL is encoded multiple times, the system should return the same short URL to avoid creating duplicates and save storage space.
example_3.py โ Multiple Different URLs
$
Input:
codec = Codec()
urls = ["https://leetcode.com", "https://github.com", "https://stackoverflow.com"]
shortUrls = [codec.encode(url) for url in urls]
decodedUrls = [codec.decode(short) for short in shortUrls]
โบ
Output:
Original URLs: ["https://leetcode.com", "https://github.com", "https://stackoverflow.com"]
Short URLs: ["http://tinyurl.com/aB3c", "http://tinyurl.com/dE4f", "http://tinyurl.com/gH5i"]
Decoded URLs: ["https://leetcode.com", "https://github.com", "https://stackoverflow.com"]
๐ก Note:
Each unique URL gets its own unique short code, and all URLs can be successfully decoded back to their originals.
Visualization
Tap to expand
Understanding the Visualization
1
URL Arrives
A long URL comes in requesting to be shortened
2
Check Existing
System checks if this URL was already shortened before
3
Generate Code
If new, generate a unique random short code
4
Store Mapping
Save the bidirectional mapping in hash tables
5
Return Short URL
Provide the short URL with the generated code
Key Takeaway
๐ฏ Key Insight: The power of hash tables enables instant bidirectional lookup, making URL shortening services both fast and scalable. The system maintains data integrity by preventing duplicates while ensuring unique short codes for different URLs.
Time & Space Complexity
Time Complexity
O(1)
Hash table lookup and insertion are O(1) average case
โ Linear Growth
Space Complexity
O(n)
Two hash tables storing n URL mappings each
โก Linearithmic Space
Constraints
- 1 โค longUrl.length โค 2048
- longUrl is guaranteed to be a valid URL
- The generated short URL should be as short as possible
- Follow-up: How would you handle URL expiration, analytics, and rate limiting?
- Consider collision handling if using hash-based approaches
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code