Token ring mutual exclusion

Write codeMedium30 min

Your task

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 to 1 to ... to N-1 to 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.

Examples

token_ring(n: int, requests: list[list[int]], ticks: int) -> list[list[int]]
Example 1
Inputtoken_ring(3, [[0, 0], [0, 2]], 5)
Output[[0, 0], [2, 2]]