Lock trace impossibility
A render farm audits the lock trace its job runner recorded. Each event is a string "worker|action|lock", for example "w2|lock|mesh_cache", and the action is either "lock" or "unlock". The locks are reentrant: a worker that already holds a lock may lock it again, which raises its hold count by one, and every unlock lowers that count by one. A lock becomes free only when its count falls back to zero. An event is impossible when a worker locks a lock that a different worker currently holds, or unlocks a lock that is free or held by someone else. Read the events in order and return the 1-based position of the first impossible event, or 0 when the whole trace is consistent. Locks still held at the end are fine.
first_trace_defect(events: list[str]) → int[["w1|lock|mesh","w1|lock|mesh","w1|unlock|mesh","w2|lock|mesh"]]out4[["w1|lock|mesh","w1|unlock|mesh","w2|lock|mesh","w2|unlock|mesh"]]out0[["w3|lock|shader","w4|unlock|shader"]]out2State 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.
[["w1|lock|mesh","w1|lock|mesh","w1|unlock|mesh","w2|lock|mesh"]]4not run yetsample[["w1|lock|mesh","w1|unlock|mesh","w2|lock|mesh","w2|unlock|mesh"]]0not run yetsample[["w3|lock|shader","w4|unlock|shader"]]2not run yetsample