KatInterview

Batch Counter Updates with Global Shift and Min Extraction

Data Structure DesignAuto-graded · 18 test cases
MediumAuto-gradedData Structure Designheaplazy-propagationdesign

Last updated

Tests lazy global offset propagation and min-heap with lazy deletion to efficiently manage counters under bulk increments and minimum extraction.

Problem Statement

You are building a lightweight task-tracking system for a fleet of workers. Each worker is identified by a unique integer ID and starts with a workload count of zero. Your system must process a sequence of operations in order:

  • "increment": Given a worker ID and a value, add that value to the specified worker's workload count.
  • "global_increment": Given a value, add it to every currently active worker's workload count simultaneously.
  • "pop_min": Find and permanently remove the active worker with the lowest workload count, then return that worker's ID. If two or more workers share the lowest count, remove and return the one with the smallest ID.

Operations that do not return a meaningful value (i.e., "increment" and "global_increment") contribute null to the output list.

Input Format

  • worker_ids: a list of distinct integers identifying the initial set of active workers.
  • operations: a list of strings, each one of "increment", "global_increment", or "pop_min".
  • arguments: a parallel list of argument lists.
    • For "increment": [worker_id, value]
    • For "global_increment": [value]
    • For "pop_min": []

Output Format

Return a list of the same length as operations. Each element is either:

  • None for "increment" and "global_increment" operations, or
  • the integer ID of the removed worker for "pop_min" operations.

Constraints

  • 1 ≤ len(worker_ids) ≤ 10^5
  • 1 ≤ len(operations) ≤ 10^5
  • All values passed to "increment" and "global_increment" are non-negative integers.
  • Worker IDs are distinct positive integers.
  • "pop_min" will not be called more times than there are active workers.
  • A "pop_min" on an empty set of workers should return None.

Examples

Example 1

Input: simulate_worker_queue([1, 2], ['increment', 'global_increment', 'pop_min', 'pop_min'], [[1, 10], [5], [], []])

Output: [None, None, 2, 1]

Example 2

Input: simulate_worker_queue([3, 1, 2], ['global_increment', 'increment', 'increment', 'pop_min', 'pop_min', 'pop_min'], [[7], [3, 2], [1, 2], [], [], []])

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

Example 3

Input: simulate_worker_queue([1, 2, 3], ['increment', 'increment', 'global_increment', 'pop_min', 'increment', 'pop_min', 'pop_min'], [[1, 4], [2, 4], [3], [], [1, 1], [], []])

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

Hints

Hint 1

A global increment shifts every active worker's count by the same amount — that means their relative order never changes. Can you track one shared offset instead of updating every worker individually?

Hint 2

Store each worker's count relative to the shared offset. True count = stored value + offset. A min-heap keyed on stored values still gives you the correct minimum because all live workers share the same offset.

Hint 3

Python's heapq has no update operation. When a worker's stored value changes, push a new entry and leave the old one. On pop_min, discard any entry whose stored value no longer matches what the worker currently holds — that entry is stale.