Lock Ordering to Prevent Deadlock
medium · global ordering
Given requests, a list of integer lock-ID lists (one per worker), return each worker's acquisition order. Deduplicate that worker's IDs and sort them ascending; retain the outer worker order. Every worker must use the same total order to eliminate circular wait. Empty requests and negative lock IDs are valid. This exercise builds a safe acquisition plan; it does not execute threads or simulate waiting. Locks are released in reverse order after the critical section.
Examples (JSON; null means Python None):
Input: {"requests": [[2, 1], [1, 2]]}
Output: [[1, 2], [1, 2]]
Input: {"requests": [[3, 3, 1], []]}
Output: [[1, 3], []]
Starter code
def ordered_locks(requests: list[list[int]]) -> list[list[int]]:
"""Return a common ascending, duplicate-free lock plan for every worker."""
pass
Loading editor…
Each test has a five-second execution limit. Your practice drafts and solved progress stay in this browser.
Return to the Python compiler