KatInterview

Multiset Frequency Log with Lazy Snapshot Pool Top-K

Data Structure DesignAuto-graded · 23 test cases
MediumAuto-gradedData Structure Designhash-mapheaplazy-deletion

Last updated

Simulate insert, remove and top-k queries on a multiset using a snapshot pool whose stale entries are discarded lazily, testing hash maps with heaps and exact rule-following.

You are given a log of operations to run, in order, against an initially empty multiset of integers. Return the result of every operation.

The topK operation is defined through an explicit snapshot pool. Follow the rules below literally, including the note about repeated values. The expected outputs depend on every rule.

State

  • count[v]: the number of copies of v currently in the multiset. A value that is not present has count 0.
  • A snapshot pool: a collection of pairs (f, v) (a frequency and a value). The same pair may appear in the pool several times, and each copy is tracked separately. The pool starts empty.

Operations

Operation i is operations[i] and takes its parameter from arguments[i], a list with exactly one integer.

"insert" with [x]

  1. Increase count[x] by 1.
  2. Add one copy of the snapshot (count[x], x) to the pool, using the new count.
  3. The result is None.

"remove" with [x]

  1. If count[x] == 0, do nothing. Do not touch the pool.
  2. Otherwise decrease count[x] by 1.
  3. If the new count[x] is at least 1, add one copy of the snapshot (count[x], x) to the pool. If the new count is 0, add nothing to the pool.
  4. The result is None in every case.

"topK" with [k]

Let limit = max(0, k). Start with an empty answer list and examine pool snapshots one at a time in this order:

  • higher f first;
  • among equal f, smaller v first;
  • identical pairs are interchangeable.

Repeat the following until the answer has limit entries (this is checked before examining each snapshot) or no unexamined snapshots remain:

  1. Take the next snapshot (f, v) in that order.
  2. If f == count[v] (the snapshot is valid), append v to the answer and keep that snapshot in the pool.
  3. Otherwise (the snapshot is stale), remove that copy from the pool permanently.

Snapshots that were never examined stay in the pool unchanged. The multiset counts are not modified by topK. The result is the answer list.

Important notes on the rules

  • Each valid snapshot adds its value to the answer, and the same pair can be in the pool more than once. So a value can appear more than once in a topK result. For example, insert(x), insert(x), remove(x) leaves two copies of (1, x) in the pool, and both are valid while count[x] == 1. This is intended, and the answer must include x once per valid copy that is examined.
  • A stale snapshot that was not examined stays in the pool. If its value's count later returns to the snapshot's frequency, it becomes valid again, in addition to any copy added by the operation that restored that count.
  • If limit == 0 or the pool is empty, the result is []. No snapshots are examined when limit == 0.
  • A removal that brings a count to 0 adds no snapshot. All existing snapshots of that value are then stale because every snapshot frequency is at least 1.

Output

Return a list with one entry per operation, in input order: None for insert and remove, and the answer list for topK.

Constraints

  • len(operations) == len(arguments), up to about 10^5.
  • Every operation name is "insert", "remove" or "topK".
  • Every arguments[i] contains exactly one integer.
  • Inserted and removed values are integers and may be negative.
  • k is an integer and may be zero, negative, or larger than the pool can supply.
  • Aim for fast updates and queries. Rescanning or fully re-sorting everything on every operation will be too slow on large inputs.

Examples

Example 1

Input: process_frequency_queries(['insert', 'insert', 'insert', 'insert', 'insert', 'topK', 'remove', 'topK'], [[5], [5], [2], [2], [3], [2], [5], [3]])

Output: [None, None, None, None, None, [2, 5], None, [2, 3, 5]]

Example 2

Input: process_frequency_queries(['topK', 'insert', 'topK', 'remove', 'topK'], [[3], [7], [3], [7], [1]])

Output: [[], None, [7], None, []]

Example 3

Input: process_frequency_queries(['insert', 'insert', 'insert', 'topK', 'remove', 'remove', 'topK'], [[-1], [4], [-1], [5], [-1], [-1], [5]])

Output: [None, None, None, [-1, 4], None, None, [4]]

Hints

Hint 1

Keep a dictionary from value to its current count, and a heap that stores the snapshot pairs as (-frequency, value) so the heap order matches the required scan order.

Hint 2

Pushing one entry per rule is enough: push on insert, and on remove only when the new count is positive. Never modify or delete old entries outside of a query.

Hint 3

In a query, pop entries while the answer is shorter than max(0, k). Keep valid ones (frequency equals the current count) in both the answer and a side list, drop stale ones, then push the side list back into the heap.