This question evaluates data structure and algorithm design skills, focusing on frequency counting, dynamic maintenance of top-K elements, and efficient update and query operations in a multiset within the Coding & Algorithms domain.
Design a data structure that maintains a multiset of integers and supports the following operations efficiently:
insert(x)
: Add one occurrence of value
x
.
remove(x)
: Remove one occurrence of value
x
if it exists (if
x
is not present, do nothing).
topK(k) -> List[int]
: Return the
k most frequent
values currently in the multiset.
k
distinct values exist, return all distinct values.
k
, empty structure).