Lock ordering deadlock avoidance
Your task
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.
Examples
global_lock_order(constraints: list[list[int]]) -> list[int]Example 1
Input
global_lock_order([[1, 2], [2, 3]])Output
[1, 2, 3]