Code RoomThread livelock detection
MediumPrep Room Coding #298

Thread livelock detection

CodingConcurrencyMid–Senior~25 min

A set of threads is in a livelock if they keep yielding to each other forever without progress. Model each thread's behaviour as a function over a global 'state' integer: transitions is a list of [thread_id, from_state, to_state]. Starting from a given initial state, a deterministic round-robin runtime repeatedly: looks at the current state, finds the FIRST transition (in list order) whose from_state matches, applies it (state := to_state), and continues. Detect whether the system livelocks (enters an infinite loop of state changes) or eventually halts (reaches a state with no applicable transition). Return True if it livelocks, False if it halts.

Implement
detect_livelock(transitions: list[list[int]], initial_state: int) → bool
Examples
in[[[0,1,2],[1,2,1]],1]outtrue
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 25 min
InputExpectedGot
[[[0,1,2],[1,2,1]],1]truenot run yetsample