Medium · System Design

Tiny URL

Design a URL shortener built on a host: encode(longUrl) returns a short link host/<code> — the same link again if longUrl was already encoded — and decode(shortUrl) returns the original URL for a link that encode issued (nothing otherwise).

Examples

Example 1

{
  "host": "https://tiny.example",
  "ops": [
    {"kind": "encode", "url": "https://example.com/problems/demo"},
    {"kind": "encode", "url": "https://docs.example"},
    {"kind": "decode", "short": "https://tiny.example/1"}
  ]
}

Output: [https://tiny.example/1, https://tiny.example/2, https://example.com/problems/demo]

Example 2

{
  "host": "https://tiny.example",
  "ops": [
    {"kind": "encode", "url": "https://a.example/x"},
    {"kind": "encode", "url": "https://b.example/y"},
    {"kind": "encode", "url": "https://a.example/x"},
    {"kind": "encode", "url": "https://c.example/z"},
    {"kind": "decode", "short": "https://tiny.example/3"},
    {"kind": "decode", "short": "https://tiny.example/9"}
  ]
}

Output: [https://tiny.example/1, https://tiny.example/2, https://tiny.example/1, https://tiny.example/3, https://c.example/z, none]

Rebuild it in the studio

Read every interview problem free. Ten rooms need no account. A token opens a problem in full — Pro never counts.

More System Design problems