← 返回 microsoft 的题目列表Design and Implement a URL Shortener
类型:online_judge
Problem: Design and Implement a URL Shortener
Design a URL shortening service similar to TinyURL or bit.ly, and implement a simplified working version.
Part 1: High-Level Design
Explain how the system supports the following requirements:
A user submits a long URL and receives a short URL.
A user visits the short URL and is redirected to the original long URL.
The system must handle a high volume of read requests with low redirect latency.
The short code should be compact and collision-free or collision-resistant.
Consider storage, caching, capacity estimation, availability, scalability, rate limiting, and monitoring.
Part 2: Low-Level Design / Coding
Implement a simplified in-memory URL shortener.
Support two operations:
ENCODE long_url: convert a long URL into a short URL and print the short URL.
DECODE short_url: convert a short URL back to the original long URL and print it; if it does not exist, print NOT_FOUND.
Input Format
The first line contains an integer n, the number of operations.
The next n lines each contain one operation:
ENCODE <long_url>
DECODE <short_url>
Output Format
Print one line for each operation.
Constraints
1 <= n <= 10^5
Long URL length: 1 <= len(long_url) <= 2048
URLs do not contain spaces.
The short URL domain is fixed as http://tiny.url/.
Encoding the same long URL multiple times should return the same short URL.
No persistence is required; use in-memory data structures.
Example
Input:
5
ENCODE https://www.microsoft.com
ENCODE https://leetcode.com/problems/encode-and-decode-tinyurl
DECODE http://tiny.url/1
ENCODE https://www.microsoft.com
DECODE http://tiny.url/2
Output:
http://tiny.url/1
http://tiny.url/2
https://www.microsoft.com
http://tiny.url/1
https://leetcode.com/problems/encode-and-decode-tinyurl
Example
Input
5
ENCODE https://www.microsoft.com
ENCODE https://leetcode.com/problems/encode-and-decode-tinyurl
DECODE http://tiny.url/1
ENCODE https://www.microsoft.com
DECODE http://tiny.url/2
Output
http://tiny.url/1
http://tiny.url/2
https://www.microsoft.com
http://tiny.url/1
https://leetcode.com/problems/encode-and-decode-tinyurl