URL Shortener
TinyURL's core: mint a short code for every distinct URL, hand back the same code on repeats, and resolve codes back to the original link.
Requirements
- Deterministic, dedup-first codes. A monotonic counter starts at 0. The first distinct URL gets code
0, the next1, and so on. Shortening an already-seen URL returns the same code and does not advance the counter. - Code alphabet is base-62 in this exact order: digits
0-9, then lowercasea-z, then uppercaseA-Z(so counter 0→0, 9→9, 10→a, 35→z, 36→A, 61→Z, 62→10). The code is the standard positional base-62 encoding of the counter, most-significant digit first, with no leading padding. resolveon a code that was never issued returns the literal tokennull.- URLs are opaque, whitespace-free tokens; two URLs are "the same" iff their strings are byte-for-byte equal.
API to implement
Implement the class UrlShortener. The I/O driver is already written in the starter code — it parses the command script below and calls your methods. You only implement the class.
| Command | Returns | Behavior |
|---|---|---|
| shorten <url> | string | Return a short code for url. Codes are assigned deterministically: if this exact URL was shortened before, return its existing code; otherwise assign the base-62 encoding of the next counter value (starting at 0) and advance the counter. |
| resolve <code> | string | Return the original URL for code, or the literal null if no such code has been issued. |
Input format
The first line contains Q, the number of operations. Each of the next Q lines is one command as listed above. String arguments contain no spaces.
Output format
For each command with a non-void return, print its result on its own line, in order. The driver does this for you — your methods just return values.
This is a design problem: the hidden tests exercise edge cases and interaction sequences, not performance tricks. Model the state cleanly and the tests will pass.
Sample cases
6
shorten https://a.com
shorten https://b.com
shorten https://a.com
resolve 0
resolve 1
resolve 20
1
0
https://a.com
https://b.com
null5
shorten x.io/p
resolve z
shorten y.io/q
shorten x.io/p
resolve 10
null
1
0
y.io/q