Skip to content

URL Shortener

Build a URL shortening service that generates short codes for long URLs and handles concurrent access safely.

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.

Implement a URL shortener with:

  1. Shorten(longURL) - Generate or retrieve short code
  2. Resolve(shortCode) - Get original URL
  3. Thread-safe operations with RWMutex
  4. Deduplication (same URL returns same code)

URL Shortener Service

~45 minmedium

Build a thread-safe URL shortening service with deduplication

  • 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
  • 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)