Code RoomLock ordering deadlock avoidance
MediumPrep Room Coding #169

Lock ordering deadlock avoidance

CodingConcurrencyMid–Senior~30 min

Several threads each need to acquire a set of locks. To avoid deadlock, all threads must acquire locks in one consistent global order. You are given a list of thread requirements, where each requirement lists pairs [x, y] meaning 'in this thread, lock x is acquired before lock y'. Treat these as ordering constraints over all distinct locks. Return a single global lock-acquisition order (a list of lock ids) consistent with every constraint; among valid orders, return the one that is lexicographically smallest by lock id. If the constraints are contradictory (cyclic), no safe order exists — return an empty list.

Implement
global_lock_order(constraints: list[list[int]]) → list[int]
Examples
in[[[1,2],[2,3]]]out[1,2,3]
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,2],[2,3]]][1,2,3]not run yetsample