Code RoomRead write lock simulator
MediumPrep Room Coding #176

Read write lock simulator

CodingConcurrencyMid–Senior~30 min

Simulate a non-blocking read-write lock. Multiple readers may hold the lock simultaneously, but a writer needs exclusive access (no readers and no other writer). You are given a list of ops, each [actor, action] where action is one of "RL" (acquire read lock), "RU" (release read lock), "WL" (acquire write lock), "WU" (release write lock). An acquire that cannot be granted right now is rejected (skipped, the actor does not retain anything). Specifically: "RL" succeeds if no writer holds the lock; "WL" succeeds only if there are zero active readers and no writer. Releases of a lock the actor doesn't hold are ignored. Process ops in order and return the number of operations that were successfully granted (acquires) or applied (valid releases).

Implement
rw_lock_grants(ops: list[list]) → int
Examples
in[[[1,"RL"],[2,"RL"],[3,"WL"],[1,"RU"],[2,"RU"],[3,"WL"]]]out5
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
[[[1,"RL"],[2,"RL"],[3,"WL"],[1,"RU"],[2,"RU"],[3,"WL"]]]5not run yetsample