Code RoomData race detection counter
HardPrep Room Coding #171

Data race detection counter

CodingConcurrencyMid–Senior~30 min

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.

0:00 of about 30 min
InputExpectedGot
[[[0,"W","x"],[1,"R","x"]]]1not run yetsample