Code Room
CodingMediumcod-g1013
Subject Concurrency atomic increment interleavingLevel Mid–Senior~25 minCommon in Concurrency interviewsIndustries Software development, Technology

Question

A shared counter starts at 0. N threads each perform one non-atomic increment, implemented as three steps in order: LOAD (read counter into the thread's local register), ADD (local register += 1), STORE (write the local register back to the counter). You are given a schedule: a list of [thread, step] events that is a valid interleaving of these steps (each thread's LOAD precedes its ADD precedes its STORE). Simulate the interleaving and return the FINAL value of the shared counter. Because of lost updates this can be anywhere from 1 to N.

Implement
interleaved_counter(n: int, schedule: list[list]) → int
Examples
in[2,[[0,"LOAD"],[1,"LOAD"],[0,"ADD"],[0,"STORE"],[1,"ADD"],[1,"STORE"]]]out1
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.