KatInterview

Sliding-Window Trending URLs with Deduplication

Data Structure DesignModel answer
MediumModel answerData Structure Designsliding-windowheaphash-map

Last updated

Design a data structure that tracks the most-visited URLs over a configurable recent time window, with idempotent inserts and efficient eviction.

Sliding-Window Trending URLs

You are building a real-time analytics backend that ingests a continuous stream of page-visit events. Each event carries three fields:

  • event_id — a globally unique string identifier for the event
  • url — the page that was visited
  • ts — the time the visit occurred, in milliseconds

Implement a tracker that supports the following operations:

record(event_id, url, ts) Registers one visit. The tracker should:

  • Silently ignore duplicate event_ids (retries may arrive; treat them as no-ops).
  • Silently ignore events that arrive too late, defined as more than a configurable lateness budget L ms behind the furthest-ahead timestamp the tracker has ever seen.
  • Otherwise, store the event and update all internal state.

top_urls(T, K, now) Returns the K most-visited URLs whose timestamps fall in the half-open interval (now − T, now], ordered by visit count descending. Break ties alphabetically by URL so results are deterministic. T will never exceed the tracker's configured maximum window size W.

unique_count(T, now) Returns the number of distinct URLs visited in the same interval (now − T, now].

Configuration

The tracker is constructed with three parameters:

  • W — the maximum window size in ms (no query will ask for a span longer than this).
  • L — the maximum lateness in ms (events older than max_seen_ts − L are dropped).
  • g — bucket granularity in ms. Time is divided into buckets of width g. Smaller values are more precise but use more memory.

Requirements and constraints

  • Inserts should be efficient: aim for better than O(N) per event where N is the number of distinct URLs in the window.
  • Memory must stay bounded: old data outside the window should be evicted automatically as the clock advances.
  • Out-of-order events within the lateness budget L must be accepted and counted correctly.
  • Deduplication by event_id must also expire over time — a global ever-growing set of seen IDs is not acceptable.

What is expected in your answer

Walk through the data structures you would choose and why. Explain how correctness is maintained across inserts, evictions, and queries. Analyze time and space complexity. Discuss how you would extend the design to support multiple concurrent writer threads and horizontal sharding across machines.

Example

W = 10_000 ms, L = 2_000 ms, g = 1_000 ms

record("e1", "a.com", 1000)
record("e2", "a.com", 2000)
record("e3", "b.com", 2000)
record("e1", "a.com", 1000)  # duplicate → ignored

top_urls(T=5000, K=2, now=5000)
# window is (0, 5000]: a.com×2, b.com×1
# → [("a.com", 2), ("b.com", 1)]

unique_count(T=5000, now=5000)
# → 2

Hints

Hint 1

Time-bucketing is the key to bounded memory and automatic dedup expiry: store event IDs per bucket so they expire for free when the bucket is evicted, instead of keeping a single ever-growing seen-ID set.

Hint 2

For the live count structure, think about what operations you actually need: you only ever increment by 1 on insert and decrement by 1 per click on eviction. A doubly-linked list of frequency nodes (LFU-style) gives O(1) increment and decrement while letting you read the top counts by walking from the high end.

Hint 3

For sub-window queries (T < W) or a now value behind the current clock, the live structure covers the full window W and cannot be used directly — fall back to scanning only the relevant buckets and aggregating their per-bucket counters.