Multicore Overheat Controller: Report Core Status Changes per Tick
Company: Optiver
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Online Assessment
A firmware controller keeps a multicore processor from overheating. Every core has a workload in watts, which starts at 0. All cores share a passive-cooling capacity, and every running core also gets its own active-cooling channel with a fixed capacity.
Implement a pure function that processes the controller's operations in order and returns, for every tick, the cores whose status changed at that tick. Each operation is one of:
- `["set", timestamp, coreId, loadWatts]`: set the workload of core `coreId` to `loadWatts`. It produces no output and takes effect at the next tick.
- `["tick", timestamp]`: recompute the status of every core from the current workloads and report the cores whose status changed.
### Function Signature
```python
def simulate_overheat_controller(passive_capacity: int, active_capacity_per_core: int, core_ids: list[str], operations: list[list[str | int]]) -> list[list[str]]:
```
### Rules
- Every core starts with workload 0 and status `OFF`.
- A core is running when its workload is greater than 0. At a tick, let `r` be the number of running cores. The passive capacity is split evenly among the running cores only, so each running core has a cooling capacity of `passive_capacity / r + active_capacity_per_core`.
- At a tick, each core gets a new status:
- `OFF` if its workload is 0;
- `OK` if it is running and its workload is at most its cooling capacity;
- `THROTTLED` if it is running and its workload is greater than its cooling capacity.
- The comparison is exact, with no rounding: a workload equal to the cooling capacity is `OK` even when `passive_capacity / r` is not a whole number.
- A tick's output is a list with one string `"coreId=status"` for each core whose new status differs from its status after the previous tick (for the first tick, from the starting status `OFF`). Cores whose status did not change are left out, and the list is empty when no core changed.
- Strings within one tick's list are sorted by `coreId` in ascending lexicographic order using plain string comparison by character code, so `"c10"` comes before `"c2"`.
- The result holds exactly one list per `tick`, in the order the ticks appear, and is empty when there are no ticks. `set` operations add nothing to the result.
- When a core gets several `set` operations before a tick, only the last one counts.
- Timestamps do not affect the result: operations are processed in list order.
- The function must not modify `core_ids` or `operations`.
### Constraints
- `1 <= len(core_ids) <= 100`; core ids are distinct strings of 1 to 10 lowercase letters and digits.
- `0 <= len(operations) <= 10^4`
- `0 <= passive_capacity <= 10^9` and `0 <= active_capacity_per_core <= 10^9`
- In every `set` operation, `coreId` is in `core_ids` and `loadWatts` is an integer with `0 <= loadWatts <= 10^9`.
- Every operation is a well-formed `set` or `tick`. Timestamps are integers in `[0, 10^9]` and never decrease along the list.
- Intermediate values in an exact comparison can reach about `10^11` (for example `10^9` watts times 100 running cores), beyond the 32-bit signed integer range, so use 64-bit integers in languages with fixed-width types.
### Examples
#### Example 1
```text
Input: passive_capacity = 100, active_capacity_per_core = 20, core_ids = ["c1","c2"],
operations = [["set",1,"c1",80],["set",2,"c2",60],["tick",3],["set",4,"c2",0],["tick",5]]
Output: [["c1=THROTTLED","c2=OK"],["c1=OK","c2=OFF"]]
```
At the first tick both cores run, so each has a cooling capacity of `100 / 2 + 20 = 70`: `c1` (80 watts) is throttled and `c2` (60 watts) is `OK`. At the second tick only `c1` runs and has `100 + 20 = 120`, so it returns to `OK`, and `c2` turns `OFF`.
#### Example 2
```text
Input: passive_capacity = 90, active_capacity_per_core = 10, core_ids = ["c2","c10","c7"],
operations = [["set",1,"c2",50],["tick",2],["tick",3],["set",4,"c10",30],
["set",5,"c7",40],["set",6,"c10",45],["tick",7]]
Output: [["c2=OK"],[],["c10=THROTTLED","c2=THROTTLED","c7=OK"]]
```
At tick 2 only `c2` runs, with a capacity of `90 + 10 = 100`. Tick 3 changes nothing, so its list is empty. Before tick 7 the second `set` on `c10` replaces the first, and three cores run, each with a capacity of `90 / 3 + 10 = 40`. `c2` (50) and `c10` (45) are throttled, and `c7` (40) is exactly at capacity, so it is `OK`; had the first `set` on `c10` (30) counted, `c10` would be `OK` as well. The list is ordered `c10`, `c2`, `c7` by string comparison.
#### Example 3
```text
Input: passive_capacity = 0, active_capacity_per_core = 5, core_ids = ["a"],
operations = [["tick",1],["set",2,"a",5],["tick",3],["set",4,"a",6],["tick",5],["set",6,"a",0],["tick",7]]
Output: [[],["a=OK"],["a=THROTTLED"],["a=OFF"]]
```
The first tick comes before any workload is set, so `a` stays `OFF` and nothing is reported. With no passive cooling, `a` can carry at most 5 watts: 5 is `OK`, 6 is throttled, and a workload of 0 turns it `OFF`.
Overview: Simulate a firmware controller for a multicore processor where running cores split passive cooling evenly and each also gets a fixed active-cooling channel. For every tick, return the cores whose OK, THROTTLED or OFF status changed, sorted by core id, which tests careful state simulation, exact boundary comparisons and output formatting.
Read the full Optiver Software Engineer interview experience this question came from
A firmware controller keeps a multicore processor from overheating. Every core has a workload in watts, which starts at 0. All cores share a passive-cooling capacity of `passive_capacity` watts, and every running core also gets its own active-cooling channel with a fixed capacity of `active_capacity_per_core` watts.
Implement `simulate_overheat_controller(passive_capacity, active_capacity_per_core, core_ids, operations)`, a pure function that processes the controller's operations in order and returns, for every tick, the cores whose status changed at that tick. Each operation is one of:
- `['set', timestamp, coreId, loadWatts]`: set the workload of core `coreId` to `loadWatts`. It produces no output and takes effect at the next tick.
- `['tick', timestamp]`: recompute the status of every core from the current workloads and report the cores whose status changed.
### Rules
- Every core starts with workload 0 and status `OFF`.
- A core is running when its workload is greater than 0. At a tick, let `r` be the number of running cores. The passive capacity is split evenly among the running cores only, so each running core has a cooling capacity of `passive_capacity / r + active_capacity_per_core`.
- At a tick, each core gets a new status: `OFF` if its workload is 0; `OK` if it is running and its workload is at most its cooling capacity; `THROTTLED` if it is running and its workload is greater than its cooling capacity.
- The comparison is exact, with no rounding: a workload equal to the cooling capacity is `OK` even when `passive_capacity / r` is not a whole number.
- A tick's output is a list with one string `coreId=status` (for example `c1=OK`) for each core whose new status differs from its status after the previous tick (for the first tick, from the starting status `OFF`). Cores whose status did not change are left out, and the list is empty when no core changed.
- Strings within one tick's list are sorted by `coreId` in ascending lexicographic order using plain string comparison by character code, so `c10` comes before `c2`.
- The result holds exactly one list per `tick`, in the order the ticks appear, and is empty when there are no ticks. `set` operations add nothing to the result.
- When a core gets several `set` operations before a tick, only the last one counts.
- Timestamps do not affect the result: operations are processed in list order.
- The function must not modify `core_ids` or `operations`.
### Integer width and operation types
Intermediate values in an exact comparison can reach about 10^11 (for example 10^9 watts times 100 running cores), which exceeds the 32-bit signed limit of 2^31-1, so use 64-bit integers: `long` in Java and `long long` in C++. Each operation mixes strings and integers. In Java it arrives as a `java.util.List<Object>` whose integers are `java.lang.Number` values, and in C++ as a `std::vector<std::string>` that holds every field as a string, with numbers written in decimal, so `['set', 1, 'c1', 80]` arrives as `{"set", "1", "c1", "80"}`.
### Constraints
- `1 <= len(core_ids) <= 100`; core ids are distinct strings of 1 to 10 lowercase letters and digits.
- `0 <= len(operations) <= 10^4`
- `0 <= passive_capacity <= 10^9` and `0 <= active_capacity_per_core <= 10^9`
- In every `set` operation, `coreId` is in `core_ids` and `loadWatts` is an integer with `0 <= loadWatts <= 10^9`.
- Every operation is a well-formed `set` or `tick`. Timestamps are integers from 0 to 10^9 and never decrease along the list.
### Example 1
```text
Input: passive_capacity = 100, active_capacity_per_core = 20, core_ids = ['c1', 'c2'],
operations = [['set', 1, 'c1', 80], ['set', 2, 'c2', 60], ['tick', 3], ['set', 4, 'c2', 0], ['tick', 5]]
Output: [['c1=THROTTLED', 'c2=OK'], ['c1=OK', 'c2=OFF']]
```
At the first tick both cores run, so each has a cooling capacity of 100 / 2 + 20 = 70: `c1` (80 watts) is throttled and `c2` (60 watts) is `OK`. At the second tick only `c1` runs and has 100 + 20 = 120, so it returns to `OK`, and `c2` turns `OFF`.
### Example 2
```text
Input: passive_capacity = 90, active_capacity_per_core = 10, core_ids = ['c2', 'c10', 'c7'],
operations = [['set', 1, 'c2', 50], ['tick', 2], ['tick', 3], ['set', 4, 'c10', 30],
['set', 5, 'c7', 40], ['set', 6, 'c10', 45], ['tick', 7]]
Output: [['c2=OK'], [], ['c10=THROTTLED', 'c2=THROTTLED', 'c7=OK']]
```
At tick 2 only `c2` runs, with a capacity of 90 + 10 = 100. Tick 3 changes nothing, so its list is empty. Before tick 7 the second `set` on `c10` replaces the first, and three cores run, each with a capacity of 90 / 3 + 10 = 40. `c2` (50) and `c10` (45) are throttled, and `c7` (40) is exactly at capacity, so it is `OK`. The list is ordered `c10`, `c2`, `c7` by string comparison.
Constraints
- 1 <= len(core_ids) <= 100; core ids are distinct strings of 1 to 10 lowercase letters and digits.
- 0 <= len(operations) <= 10^4
- 0 <= passive_capacity <= 10^9 and 0 <= active_capacity_per_core <= 10^9
- In every set operation, coreId is in core_ids and loadWatts is an integer with 0 <= loadWatts <= 10^9.
- Every operation is a well-formed set or tick. Timestamps are integers from 0 to 10^9 and never decrease along the list.
- Intermediate values in an exact comparison can reach about 10^11 (for example 10^9 watts times 100 running cores), beyond the 32-bit signed integer range, so use 64-bit integers in languages with fixed-width types.
Examples
Input: (100, 20, ['c1', 'c2'], [['set', 1, 'c1', 80], ['set', 2, 'c2', 60], ['tick', 3], ['set', 4, 'c2', 0], ['tick', 5]])
Expected Output: [['c1=THROTTLED', 'c2=OK'], ['c1=OK', 'c2=OFF']]
Explanation: Source example 1: two running cores split 100 W of passive cooling, then c2 stops and c1 drops back to OK.
Input: (90, 10, ['c2', 'c10', 'c7'], [['set', 1, 'c2', 50], ['tick', 2], ['tick', 3], ['set', 4, 'c10', 30], ['set', 5, 'c7', 40], ['set', 6, 'c10', 45], ['tick', 7]])
Expected Output: [['c2=OK'], [], ['c10=THROTTLED', 'c2=THROTTLED', 'c7=OK']]
Explanation: Source example 2: the later set on c10 wins, an unchanged tick gives an empty list, c7 at exactly 40 is OK, and c10 sorts before c2.
Hints
- The passive share at a tick depends on how many cores have a positive workload at that moment, and a change in that count can flip the status of a core whose own workload did not change.
- Remember each core's status from the previous tick; a core belongs in a tick's list only when its newly computed status differs from that remembered value.
- A workload equal to the cooling capacity is OK even when passive_capacity / r is fractional, so the comparison cannot round, and the values involved can pass the 32-bit range.