KatInterview

Track Busiest Processing Node in Rotating Allocation System

Data Structure DesignAuto-graded · 18 test cases
MediumAuto-gradedData Structure Designarraysdesign-dssimulation

Last updated

Simulate round-robin package allocation across nodes with cycle resets, capacity limits, and permanent node shutdowns.

You are tasked with simulating an event-driven system that routes incoming packages across a list of processing nodes indexed from 0 to n - 1.

Each node i has a capacity cap[i] per cycle. You are given an initial capacity array cap and a sequence of events.

Rules & Mechanics

  1. Pointer & Traversal:

    • Package processing begins with an internal pointer at index 0.
    • When a "PACKAGE" event occurs, the package is assigned to the first eligible node at or after the current pointer index.
    • A node is eligible if it is not permanently shut down and still has remaining capacity in the current cycle.
    • Once assigned, the node processes the package (incrementing its lifetime package count and consuming 1 unit of its remaining cycle capacity).
    • If the node's cycle capacity drops to 0, it becomes full for the rest of the current cycle, and the pointer advances to node + 1. Otherwise, the pointer remains at that node.
  2. Cycle Resets:

    • If a "PACKAGE" event arrives and no eligible node exists at or after the current pointer index in the current cycle:
      • A cycle reset occurs automatically before placing the package.
      • Upon reset, all active (non-shutdown) nodes have their cycle capacity restored back to cap[i]. Permanently shut down nodes remain shut down.
      • The pointer moves back to index 0.
      • The package is then assigned to the first eligible node starting from index 0.
      • If no eligible node exists even after a reset (e.g., all nodes are closed or have 0 initial capacity), the package cannot be processed and is discarded.
  3. Shutdown Events:

    • A ["CLOSURE", x] event permanently shuts down node x.
    • Node x can no longer receive packages in the current cycle or any future cycles.

Output

After processing all events, return the integer ID of the node that processed the maximum total number of packages. If there is a tie, return the largest node ID among those tied.

Constraints

  • n == len(cap)
  • 1 <= n <= 10^4
  • 0 <= cap[i] <= 10^5
  • 1 <= len(events) <= 10^4
  • Each event is a list: either ["PACKAGE"] or ["CLOSURE", x] where 0 <= x < n.

Input format note

Each element of events is a list of objects. For package events, event[0] is "PACKAGE". For closure events, event[0] is "CLOSURE" and event[1] is the integer index of the node to close.

Examples

Example 1

Input: simulate_warehouse_processing([2, 1, 2], [[',', 'PACKAGE'], ['PACKAGE'], ['PACKAGE'], ['PACKAGE'], ['PACKAGE']])

Output: 0

Example 2

Input: simulate_warehouse_processing([1, 1], [['PACKAGE'], ['CLOSURE', 0], ['PACKAGE'], ['PACKAGE']])

Output: 1

Example 3

Input: simulate_warehouse_processing([1, 2, 1], [['CLOSURE', 1], ['PACKAGE'], ['PACKAGE'], ['PACKAGE']])

Output: 0

Hints

Hint 1

Maintain an array for current cycle capacities and a boolean array for closed nodes.

Hint 2

When looking for an eligible node, search forward from the current pointer. If you reach the end of the array, trigger a cycle reset.

Hint 3

Don't forget tie-breaking: if multiple nodes processed the maximum number of packages, pick the one with the highest index.