https://leetcode.com/problems/design-tinyurl/description/
How would you design a URL shortening service that is similar to TinyURL?
Background:
TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/problems/design-tinyurl and it returns a short URL such as http://tinyurl.com/4e9iAk.
Requirements:
- For instance, "http://tinyurl.com/4e9iAk" is the tiny url for the page
"https://leetcode.com/problems/design-tinyurl". The identifier (the highlighted part) can be any string with 6 alphanumeric characters containing0-9,a-z,A-Z. - Each shortened URL must be unique; that is, no two different URLs can be shortened to the same URL.
Note about Questions:
Below are just a small subset of questions to get you started. In real world, there could be many follow ups and questions possible and the discussion is open-ended (No one true or correct way to solve a problem). If you have more ideas or questions, please ask in Discuss and we may compile it here!
Questions:
- How many unique identifiers possible? Will you run out of unique URLs?
- Should the identifier be increment or not? Which is easier to design? Pros and cons?
- Mapping an identifier to an URL and its reversal - Does this problem ring a bell to you?
- How do you store the URLs? Does a simple flat file database work?
- What is the bottleneck of the system? Is it read-heavy or write-heavy?
- Estimate the maximum number of URLs a single machine can store.
- Estimate the maximum number of queries per second (QPS) for decoding a shortened URL in a single machine.
- How would you scale the service? For example, a viral link which is shared in social media could result in a peak QPS at a moment's notice.
- How could you handle redundancy? i,e, if a server is down, how could you ensure the service is still operational?
- Keep URLs forever or prune, pros/cons? How we do pruning? (Contributed by @alex_svetkin)
- What API would you provide to a third-party developer? (Contributed by @alex_svetkin)
- If you can enable caching, what would you cache and what's the expiry time? (Contributed by @Humandroid)
// You can type code here and execute it. class Encoder { private Map<String, String> shortToLong = new HashMap<>(); private Map<String, String> longToShort = new HashMap<>(); private static final char[] CHARACTERS = "0123456789abcdeghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray(); private static final Random RANDOM = new Random(); private static final String URL_PREFIX = "http://tinyurl.com/"; public String encode(String longURL) { if (longToShort.containsKey(longURL)) { return URL_PREFIX + longToShort.get(longURL); } String shortURL = generateRandom(); while (longToShort.containsValue(shortURL)) { shortURL = generateRandom(); } longToShort.put(longURL, shortURL); shortToLong.put(shortURL, longURL); return URL_PREFIX + shortURL; } public String decode(String shortURL) { if (shortToLong.containsKey(shortURL)) { return shortToLong.get(shortURL); } return null; } private static String generateRandom() { StringBuilder sb = new StringBuilder(); for (int i = 0; i < 6; i++) { sb.append(CHARACTERS[RANDOM.nextInt(CHARACTERS.length)]); } return sb.toString(); } } class Main { public static void main(String[] args) { Encoder encoder = new Encoder(); for (String s : args) { if (s != null && s.length() != 0) { System.out.println(encoder.encode(s)); } } } }

浙公网安备 33010602011771号