TRT
Sign in
mediumAdvancedLeetCode#key-generation#read-heavy-service#caching

Design a Short-Link Service

Design short-link creation and redirection from traffic assumptions through identifier generation, storage, cache, and abuse controls.

The Runtime Theory Team1 min read
Taught inCache-Conscious Data Structures: Alignment, False Sharing, and Structure of ArraysPro — members onlyStructure C data layouts for CPU cache lines, eliminate multi-core false sharing with explicit alignment, and transform Array of Structures into Structure of Arrays for SIMD saturation.
Solve it

Solving happens on the judge — come back and mark it done

Sample cases

inCreate two links for different long URLs

outEach short identifier maps to the intended destination

inRedirect with unknown, expired, or disabled identifier

outReturn a defined not-found or unavailable response without leaking internal details

Design a service with create(longUrl) and redirect(shortCode) that generates collision-free identifiers, stores the mapping, handles cache misses and expiration, and limits abuse.

Before you code

Decide the key-generation strategy (base62 from a counter/ULID/Snowflake, or hash+collision check) and whether short links need to be globally unique and unpredictable.

Hints

  1. Base62 over a monotonically increasing ID avoids collisions if the ID space is managed.
  2. For custom aliases, check uniqueness in a store; consider expiration.
  3. Plan for redirection: return 301/302/308 with correct cache headers.

Production & complexity notes

When solving Design a Short-Link Service on LeetCode (medium), always state your loop or recursion invariant before writing code. Verify how your chosen data structure behaves on degenerate inputs (single-element inputs, all-duplicate keys, strictly monotonic sequences, or maximum constraint sizes up to $10^5$ elements). In production interviews, pair your implementation with an explicit time and auxiliary space complexity proof, explain why alternative brute-force approaches violate the latency budget, and trace how cache locality and memory allocation overhead affect wall-clock throughput on real hardware. Review the step-by-step memory trace in Cache-Conscious Data Structures: Alignment, False Sharing, and Structure of Arrays to reinforce the underlying pattern.

What it practices

URL shortening: ID space design, collision avoidance, and HTTP redirection semantics, from What a hash function does.

Not started

Sign in to save your learning progress.

Sign in to save