The interview problem
Track query frequencies in a stream with millions of distinct strings using bounded memory. Explain collision bias and how candidates are discovered.
Original practice, not a reported company interview. Scenarios and numbers are illustrative. Interview date: not applicable. Sources retrieved October 1, 2026.
1. Scope and worked approach
Assume nonnegative increments and an insertion-only count-min sketch. Hash each key into one counter per row; estimate its count by the minimum of those counters. Compatible shards merge by adding corresponding counters, with contribution deduplication outside the sketch. Keep a separate candidate structure because counters alone do not enumerate hot keys. Version the hash functions and window boundaries. Exact verification can follow for shortlisted keys.
2. Trace the design or derivation
Increment compatible counters → maintain candidates → query minimum → verify heavy hitters
Use immutable shard-window contribution IDs in the aggregation manifest. The merge is additive, so replacing a contribution requires a defined rebuild or subtraction protocol consistent with the data structure. A retry-safe transport alone does not stop double accounting after a coordinator restart.
3. Calculate the example
For a queried key whose three counters are 14,11,13, the estimate is 11. If its true count is 9, collision contamination contributes at least two to every row. With illustrative error parameter 0.001 and stream mass one million, the usual additive-error scale is 1,000 counts under the algorithm's hashing/probability assumptions; it is not 0.1% of that key's frequency.
4. Failure cases and trade-offs
Replaying a shard doubles its contribution. Negative updates and overflow can invalidate the insertion-only overestimation guarantee. More width reduces collisions while increasing memory; more rows reduce failure probability at higher hashing cost. Rare-key estimates can be poor even when relative stream error is small.
5. Validation, follow-ups and self-review
Compare exact and sketched counts under uniform and skewed streams; inject duplicates and counter saturation. Follow-up: how are expired windows removed? Reward stream-relative error reasoning, compatible merges and a candidate-recovery plan.
Sources and related practice
Technical reference: redis.io — Insertion-only frequency sketches and stream-relative error.
Source publication/update: Not established. Retrieved October 1, 2026. The workflow, calculations and review exercises are original; this reference is not evidence that a company asked this question.
Continue with Compute trending items from distributed streams; Count distinct identifiers across overlapping partitions.
Continue your preparation
Choose a connected learning path or browse more System design questions. Read our editorial method and attribution policy.