Track Busiest Processing Node in Rotating Allocation System
Data Structure DesignAuto-graded · 18 test casesLast 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
-
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
1unit 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 tonode + 1. Otherwise, the pointer remains at that node.
- Package processing begins with an internal pointer at index
-
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
0initial capacity), the package cannot be processed and is discarded.
- If a
-
Shutdown Events:
- A
["CLOSURE", x]event permanently shuts down nodex. - Node
xcan no longer receive packages in the current cycle or any future cycles.
- A
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^40 <= cap[i] <= 10^51 <= len(events) <= 10^4- Each event is a list: either
["PACKAGE"]or["CLOSURE", x]where0 <= 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.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
We can maintain the exact state of the warehouse system as events are processed step-by-step:
-
State Tracking:
closed: A boolean array of sizeninitialized toFalseto track permanently shut-down nodes.remaining: An integer array of sizeninitialized tocap[:]to track remaining cycle capacity.counts: An integer array of sizeninitialized to0tracking total packages processed by each node.pointer: An integer starting at0.
-
Event Processing:
"CLOSURE": Markclosed[x] = Trueand setremaining[x] = 0."PACKAGE": Search forward frompointerfor the first indexiwherenot closed[i]andremaining[i] > 0.- If no eligible node exists at or after
pointer, perform a cycle reset: restoreremaining[i] = cap[i]for all non-closed nodes and search again starting from index0. - If still no node is eligible (all closed or 0 capacity), drop the package.
- Otherwise, assign the package to node
i: decrementremaining[i], incrementcounts[i]. - If
remaining[i] == 0, updatepointer = i + 1. Otherwise, updatepointer = i.
- If no eligible node exists at or after
-
Tie-Breaking Result:
- Find the maximum value in
counts. Iterate from indexn - 1down to0and return the first index that achieves this maximum value to satisfy the tie-breaker rule.
- Find the maximum value in
Complexity Analysis
- Time Complexity:
- Each
PACKAGEsearch scans forward in $O(n)$ time worst-case, and a reset takes $O(n)$ time. - Given $E$ events and $n$ nodes, the total time complexity is $O(E \cdot n)$.
- Each
- Space Complexity:
- $O(n)$ extra space to store current state arrays (
closed,remaining,counts).
- $O(n)$ extra space to store current state arrays (
Python Solution
from typing import List
def simulate_warehouse_processing(cap: List[int], events: List[List[object]]) -> int:
n = len(cap)
closed = [False] * n
remaining = cap[:]
counts = [0] * n
pointer = 0
def find_next_eligible(start: int) -> int:
for i in range(start, n):
if not closed[i] and remaining[i] > 0:
return i
return -1
def reset_cycle():
for i in range(n):
if not closed[i]:
remaining[i] = cap[i]
for event in events:
if event[0] == "PACKAGE":
warehouse = find_next_eligible(pointer)
if warehouse == -1:
reset_cycle()
warehouse = find_next_eligible(0)
if warehouse == -1:
continue
remaining[warehouse] -= 1
counts[warehouse] += 1
if remaining[warehouse] == 0:
pointer = warehouse + 1
else:
pointer = warehouse
elif event[0] == "CLOSURE":
x = int(event[1])
closed[x] = True
remaining[x] = 0
max_count = max(counts)
for i in range(n - 1, -1, -1):
if counts[i] == max_count:
return i
return 0