KatInterview

Multicore Thermal Simulation with Adaptive Cooling Selection

Data Structure DesignAuto-graded · 18 test cases
HardAuto-gradedData Structure Designsimulationstate-machinefixed-point

Last updated

Tests discrete-event simulation skills: delayed load semantics, proportional passive cooling with vibration penalties, fixed-point active cooling selection, and transition-only status reporting.

Problem

You are building a firmware simulator for a multicore processor's thermal management unit. The processor has a fixed set of cores that share a passive cooling budget. Each core can also be assigned its own dedicated active cooling channel, but running multiple active channels introduces mechanical vibration that reduces passive cooling efficiency for everyone.

Implement the following function:

thermal_controller_sim(
    passive_capacity,
    active_capacity_per_core,
    core_ids,
    operations
) -> list of tick results

Operations arrive in one of two forms:

["set", timestamp, core_id, load_watts]
["tick", timestamp]

Return one list per tick operation. Each list contains strings of the form "core_id=status", sorted alphabetically by core_id, covering only the cores whose status changed at that tick. A status is one of:

  • idle — the core is running and was not assigned active cooling for the coming interval
  • cooling — the core is running and was assigned active cooling for the coming interval
  • shutdown — the core has been switched off due to overheating

Initial State

Every core begins at 20.0 °C, running with a load of 0 watts, active cooling off, and status idle.


Handling set Operations

A set records a pending load for a core but does not advance time or change temperatures. If multiple set operations target the same core before the next tick, only the last one is kept.

  • For a running core, the pending load is applied after temperature advancement and shutdown checks at the next tick.
  • For a shutdown core, the set acts as a one-time restart request. At the next tick, after advancement, the core restarts with that load if and only if its temperature is strictly below 50.0 °C. Otherwise the request is silently discarded and the core remains shutdown with load 0.

Handling tick Operations

Each tick runs the following five phases in order.

Phase 1 — Advance temperatures.

The first tick establishes the time origin; it advances by zero seconds. Every subsequent tick advances by the elapsed seconds since the previous tick. The interval uses the loads and active-cooling assignments established at the previous tick.

For each core, its passive demand is load + 2 watts. If k cores were assigned active cooling during this interval, the effective passive capacity is:

effective_capacity = passive_capacity × (1 − vibration_penalty(k) / 100)

where:

vibration_penalty(0) = 0
vibration_penalty(k) = sum(10 / F[i]  for i = 1 to k)
F = [1, 2, 3, 5, 8, 13, ...]   (Fibonacci, starting at F[1] = 1)

If the sum of all cores' passive demands is at most effective_capacity, every core receives its full demand. Otherwise every core receives the same proportional fraction:

passive_for_core = effective_capacity × core_demand / total_demand

A core that was assigned active cooling also receives active_capacity_per_core additional watts of cooling. Net heat for a core is:

net_heat = load − passive_for_core − active_cooling_for_core

Temperature changes at 0.02 °C per second per watt of net heat, and never falls below 20.0 °C. A shutdown core contributes its 2-watt passive demand to the total but has load 0 and no active cooling.

Phase 2 — Shutdown check.

After advancement, any running core whose temperature is at or above 80.0 °C shuts down. Its load is set to 0, its active-cooling flag is cleared, and any pending load for it is discarded.

Phase 3 — Apply pending loads and restart requests.

For each core with a pending entry (in any order):

  • Running cores adopt the pending load immediately.
  • Shutdown cores attempt a restart as described above.

All pending entries are cleared after this phase, regardless of outcome.

Phase 4 — Recompute active cooling.

Active-cooling assignment is rebuilt from scratch. Shutdown cores are never assigned cooling. Use the following monotone fixed-point procedure:

  1. Seed the selected set with every running core whose current temperature is strictly above 60.0 °C.
  2. With the current selected-set size k, recompute effective_capacity and the passive allocation using the current loads.
  3. For each running core not yet in the selected set, compute its temperature rate without active cooling: rate = 0.02 × (load − passive_for_core). If rate > 0.5 °C/s, add it to the selected set.
  4. Repeat from step 2 until no new core is added.

The resulting set is applied for the interval ending at the next tick.

Phase 5 — Report status changes.

Each core's new status is shutdown, cooling, or idle. Compare it to the status recorded after the previous tick (or the initial idle before the first tick). Emit one "core_id=status" string for every core whose status changed, sorted alphabetically. A tick where no core changes status returns an empty list.


Constraints

  • 1 ≤ len(core_ids) < 1024; all identifiers are unique, non-empty strings.
  • passive_capacity > 0 and active_capacity_per_core > 0.
  • 1 < len(operations) < 1024.
  • All operation timestamps are strictly increasing positive values with millisecond precision.
  • 0 ≤ load_watts ≤ 32768, with at most three decimal places.
  • Test values are designed to stay safely away from the exact boundary values 50.0, 60.0, 80.0, and 0.5 except when the problem intentionally tests strict vs. non-strict behavior.
  • Temperature values are never returned; only status strings appear in the output.

Examples

Example 1

Input: thermal_controller_sim(100, 30, ['core1', 'core2'], [['tick', 1], ['set', 2, 'core1', 20], ['tick', 3], ['tick', 8]])

Output: [[], [], []]

Example 2

Input: thermal_controller_sim(8, 10, ['b', 'a'], [['set', 1, 'a', 40], ['tick', 2], ['set', 3, 'b', 5], ['tick', 5], ['tick', 10]])

Output: [['a=cooling'], [], []]

Hints

Hint 1

A `set` never changes temperature or status on its own — it only writes into a pending map. Repeated sets to the same core before a tick are naturally handled by overwriting the same key.

Hint 2

The vibration penalty depends only on how many cores have active cooling, not which ones. This means effective passive capacity is a monotone non-increasing function of the selected-set size, so once a core's uncooled rate crosses the threshold it stays above it for any larger selected set — the fixed point is guaranteed to terminate.

Hint 3

Keep two clearly separated snapshots: the loads and active-cooling set from the *previous* tick (used for thermal advancement) and the updated state being built for the *next* interval. Mixing them up — especially applying new loads before advancing temperature, or using the new cooling set for the current interval — is the most common source of wrong answers.