URL Shortener
URL Shortener
Section titled “URL Shortener”Build a URL shortening service that generates short codes for long URLs and handles concurrent access safely.
Background
Section titled “Background”URL shorteners like bit.ly create short, memorable URLs that redirect to longer ones. Key features:
- Short Code Generation: Create unique short codes (e.g., “abc123”)
- Bidirectional Mapping: Store both short→long and long→short
- Thread Safety: Handle concurrent requests with mutex
- Deduplication: Same long URL gets the same short code
This project combines concurrency, data structures, and string manipulation.
Your Task
Section titled “Your Task”Implement a URL shortener with:
Shorten(longURL)- Generate or retrieve short codeResolve(shortCode)- Get original URL- Thread-safe operations with RWMutex
- Deduplication (same URL returns same code)
URL Shortener Service
~45 minmedium
Build a thread-safe URL shortening service with deduplication
Key Concepts
Section titled “Key Concepts”- Bidirectional Mapping: Store both short→long and long→short for deduplication
- Thread Safety: RWMutex allows multiple readers or one writer
- Random Code Generation: Generate unique short codes from character set
- Deduplication: Avoid creating multiple codes for the same URL
- Concurrent Access: Handle multiple simultaneous requests safely
Extensions
Section titled “Extensions”- Add collision handling (check if generated code already exists)
- Implement persistence (save/load from file or database)
- Add expiration times for shortened URLs
- Create HTTP server with /shorten and /r/:code endpoints
- Add analytics (track click counts)