This question evaluates a candidate's competency in algorithm and data-structure design, frequency counting and streaming/approximation techniques, and the ability to reason about time/space complexity and workload-driven trade-offs.

You are working with a large collection of items represented by integer IDs (e.g., product IDs, user IDs, etc.). Updates and queries arrive over time.
You need to support the following operations:
add(x)
: record one occurrence of item
x
(increment its frequency count).
topK(k)
: return any ordering of the
k
most frequent items seen so far, along with their frequencies.
Design data structures and algorithms to support these operations, and discuss different approaches under two workload patterns:
add(x)
updates but
many
topK(k)
queries.
add(x)
updates but relatively
few
topK(k)
queries.
add(x)
and
topK(k)
.
add
and
topK
).
x
(possible IDs) is large.
k
is much smaller than the total number of distinct items.
Given the sequence of operations:
add(1)
add(2)
add(1)
add(3)
add(2)
add(1)
A call to topK(2) should return items [1, 2] (since 1 appeared 3 times, 2 appeared 2 times, 3 appeared 1 time).
Describe, in detail:
Implementation (code) is optional; focus primarily on explaining data structures, algorithms, and their trade-offs.