Complexity Analysis
Time Complexity:
Space Complexity:
Problem Description
Design a URL shortening service like bit.ly or goo.gl.
Requirements
- Given a long URL, return a unique short URL.
- When a user accesses the short URL, redirect them to the original long URL.
- Users can optionally pick a custom alias for their URL.
- Links expire after a standard default timespan (e.g., 5 years).
High-Level Design
- API Endpoints:
POST /api/v1/data/shorten: TakeslongUrl, returnsshortUrl.GET /{shortUrl}: Redirects tolongUrl.
- Database Schema:
- URLMapping Table:
Hash(PK): varchar(6-7)OriginalURL: varchar(2048)CreationDate: datetimeExpirationDate: datetimeUserID: int (optional)
- URLMapping Table:
- URL Encoding:
- Use Base62 encoding ([a-z, A-Z, 0-9]) for the hash.
- A 7-character Base62 string offers ~3.5 trillion combinations ($62^7$).
- Key Generation Service (KGS):
- To avoid collision checks on every insertion, pre-generate keys and store them in a separate DB or use a standalone service (KGS) to hand out unique keys.