Design rotating warehouse simulator with closures
Problem: Rotating Warehouse Simulator
Given an integer array cap[0..n-1] representing the initial capacities for n warehouses (indexed 0 to n-1), simulate package processing based on a sequence of events.
Event Types:
-
"PACKAGE": Process one package by assigning it to the first eligible warehouse. An eligible warehouse is defined as:
- At or after the current pointer (initially warehouse
0). - Not permanently closed.
- Has remaining capacity in the current cycle.
When a warehouse reaches its capacity in the current cycle, it is considered full and skipped until the next cycle. After each package is processed, advance the current pointer to the next eligible warehouse following the one that just processed the package.
- At or after the current pointer (initially warehouse
-
"CLOSURE x": Permanently close warehouse
x. A closed warehouse can never process packages again.
Cycle Reset Logic:
A new cycle begins when the following conditions are met:
- Warehouse
n-1becomes full (reaches its capacity in the current cycle). - There is no eligible warehouse after
n-1in the current cycle.
When these conditions are met, the next "PACKAGE" event will initiate a new cycle. In a new cycle, the remaining capacities of all non-closed warehouses are reset to their initial cap[i] values. Closures persist across cycles.
Deliverables:
After processing all events, return the ID of the warehouse that processed the most packages. In case of a tie, return the largest warehouse ID.
Design Requirements:
Design appropriate data structures and an algorithm that achieves low per-event complexity. Describe how your solution handles edge cases (e.g., if all warehouses are closed).
AI interview prep
Core dilemma
The central engineering conflict is balancing efficient, sub-linear time complexity for finding the 'next eligible' warehouse in a circular fashion, with dynamic updates (closures, capacity changes) and periodic bulk resets (cycle resets). A naive linear scan for each package event will not meet the 'low per-event complexity' requirement for large N.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Design rotating warehouse simulator with closures
Problem: Rotating Warehouse Simulator
Given an integer array cap[0..n-1] representing the initial capacities for n warehouses (indexed 0 to n-1), simulate package processing based on a sequence of events.
Event Types:
-
"PACKAGE": Process one package by assigning it to the first eligible warehouse. An eligible warehouse is defined as:
- At or after the current pointer (initially warehouse
0). - Not permanently closed.
- Has remaining capacity in the current cycle.
When a warehouse reaches its capacity in the current cycle, it is considered full and skipped until the next cycle. After each package is processed, advance the current pointer to the next eligible warehouse following the one that just processed the package.
- At or after the current pointer (initially warehouse
-
"CLOSURE x": Permanently close warehouse
x. A closed warehouse can never process packages again.
Cycle Reset Logic:
A new cycle begins when the following conditions are met:
- Warehouse
n-1becomes full (reaches its capacity in the current cycle). - There is no eligible warehouse after
n-1in the current cycle.
When these conditions are met, the next "PACKAGE" event will initiate a new cycle. In a new cycle, the remaining capacities of all non-closed warehouses are reset to their initial cap[i] values. Closures persist across cycles.
Deliverables:
After processing all events, return the ID of the warehouse that processed the most packages. In case of a tie, return the largest warehouse ID.
Design Requirements:
Design appropriate data structures and an algorithm that achieves low per-event complexity. Describe how your solution handles edge cases (e.g., if all warehouses are closed).