Code Room
CodingHardcod-g1012
Subject Concurrency race condition detectionLevel Mid–Senior~30 minCommon in Concurrency interviewsIndustries Software development, Technology

Question

Given an interleaving of memory-access events on shared variables with no synchronization, detect data races. Each event is [thread, op, var] where op is "R" (read) or "W" (write). Two events on the SAME variable from DIFFERENT threads constitute a data race if at least one of them is a write. Return the number of distinct unordered variable-level conflicting thread pairs: for each variable, count the unordered pairs of distinct threads {t1, t2} such that the accesses to that variable by t1 and t2 conflict (some access among the two threads is a write). Each such variable-thread-pair is counted once.

Implement
count_races(events: list[list]) → int
Examples
in[[[0,"W","x"],[1,"R","x"]]]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.