Code RoomToken ring mutual exclusion
MediumPrep Room Coding #308

Token ring mutual exclusion

CodingConcurrencyMid–Senior~30 min

Simulate token-ring mutual exclusion. N nodes are arranged in a ring (0..N-1); a single token starts at node 0 and circulates 0→1→...→N-1→0. At each tick the token is held by exactly one node. requests is a list of [tick, node] meaning 'node' wants to enter its critical section at that tick (each request stands until satisfied). A node may enter the critical section ONLY when it currently holds the token AND it has an outstanding (already-issued at or before the current tick) request; entering consumes that request. Whether or not a node enters, the token advances one step per tick (to (holder+1) mod N) at the end of each tick. Simulate for exactly 'ticks' ticks (tick indices 0..ticks-1). Return the list of [tick, node] critical-section entries in chronological order.

Implement
token_ring(n: int, requests: list[list[int]], ticks: int) → list[list[int]]
Examples
in[3,[[0,0],[0,2]],5]out[[0,0],[2,2]]
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
[3,[[0,0],[0,2]],5][[0,0],[2,2]]not run yetsample