Interview report: Firmware Overheat Controller simulation problem (one coding question, 90 minutes)
Format: one coding question, 90 minutes, language of your choice (C++ / Java / Python). The problem is not a hard algorithm problem. It is mostly state simulation, edge cases and output format. You implement a pure function that simulates the firmware thermal controller of a multicore processor.
The question
The assessment had one coding question, with C++, Java, and Python available as language options.
Simulate a firmware controller that prevents a multicore processor from overheating. Each core has a configured workload, all cores share passive cooling, and each running core can receive a dedicated active-cooling channel.
Implement a pure function that processes the controller operations in order:
simulateOverheatController(
passiveCapacity,
activeCapacityPerCore,
coreIds,
operations
) -> list of Tick results
Each operation is one of:
["set", timestamp, coreId, loadWatts]
["tick", timestamp]
Return one array for every tick. Each array contains strings in the form
"coreId=status", sorted by coreId, for exactly the cores whose status changed at that tick.
The last sentence was cut off in the screenshot of the original question, so I read it as "changed at that tick": each tick returns the cores whose status changed during that tick, in the format coreId=status, sorted by coreId. If no core changed status, return an empty array [].
My understanding
Each core has a current workload, which starts at 0. set only updates one core's workload and produces no output. tick recomputes the status of all cores.
Only running cores take part in the passive cooling split, and each running core gets one share of active cooling. Passive cooling is shared by all running cores, so each running core gets:
passiveCapacity / runningCoreCount
The total cooling a running core has is:
passiveCapacity / runningCoreCount + activeCapacityPerCore
If workload <= available cooling, the status is OK, otherwise it is THROTTLED. A core with workload 0 is OFF / IDLE; go by the original question for the exact status names. Each tick only outputs the cores whose status differs from last time, sorted by coreId.
Solution
Keep two maps:
loads[coreId]: the current workloadstatus[coreId]: the status after the previous tick
On set, update loads. On tick:
- Count the running cores
n, i.e. the number of cores withloads[cid] > 0. - Compute the new status of every core.
- Compare with the old status and collect the cores that changed.
- Sort by coreId and turn them into an array of
"coreId=status"strings. - Update the stored status and append the array to the result.
To avoid floating point error, compare with multiplication. This condition:
load <= passiveCapacity / n + activeCapacityPerCore
is equivalent to:
load * n <= passiveCapacity + activeCapacityPerCore * n
Python reference code:
def simulateOverheatController(
passiveCapacity,
activeCapacityPerCore,
coreIds,
operations
):
loads = {cid: 0 for cid in coreIds}
status = {cid: "OFF" for cid in coreIds}
result = []
for op in operations:
if op[0] == "set":
_, timestamp, coreId, loadWatts = op
loads[coreId] = loadWatts
elif op[0] == "tick":
_, timestamp = op
running = [cid for cid in coreIds if loads[cid] > 0]
n = len(running)
changed = []
for cid in coreIds:
load = loads[cid]
if load <= 0:
new_status = "OFF"
else:
# load <= passiveCapacity / n + activeCapacityPerCore
if load * n <= passiveCapacity + activeCapacityPerCore * n:
new_status = "OK"
else:
new_status = "THROTTLED"
if new_status != status[cid]:
changed.append((cid, new_status))
status[cid] = new_status
changed.sort(key=lambda x: x[0])
result.append([
f"{cid}={st}"
for cid, st in changed
])
else:
raise ValueError("unknown operation")
return result
Example
Say:
passiveCapacity = 100
activeCapacityPerCore = 20
coreIds = ["c1", "c2"]
operations = [
["set", 1, "c1", 80],
["set", 2, "c2", 60],
["tick", 3],
["set", 4, "c2", 0],
["tick", 5],
]
At the first tick: running = 2, so each core's passive share = 100 / 2 = 50, and each core's total cooling = 50 + 20 = 70. c1 load = 80 > 70, so THROTTLED. c2 load = 60 <= 70, so OK.
At the second tick only c1 is running: passive share = 100, total cooling = 100 + 20 = 120. c1 load = 80 <= 120, so OK. c2 load = 0, so OFF.
Output:
[
["c1=THROTTLED", "c2=OK"],
["c1=OK", "c2=OFF"]
]
Easy mistakes
- Every tick must return an array, even when nothing changed. In that case return
[]. setproduces no output, it only affects later ticks.- Only output the cores whose status changed, not all cores.
- Sort by coreId inside each tick.
- Passive cooling is shared by running cores only. Do not divide by 0 when no core is running.
- Compare with multiplication to avoid floating point precision problems.
- Pure function: do not modify the input
operationsorcoreIds; build new dicts and a new result array. - If the same core gets
setseveral times, the later one overwrites the earlier one. - Go by the original question for the status names. It may be
OK/THROTTLED/OFF, or it may beRUNNING/THROTTLED/IDLE.
Discussion
Loading comments…