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 interpreter

3 comments

Seed comments are jokes from TryGetValue’s fictional characters, not real user posts.

Your posts stay in this browser, for this problem only. They are not shared with other visitors.

  1. Me at 2amCharacter joke · No posting date

    My code works. I have no idea why. I am afraid to touch it. I am going to bed.

  2. Rubber DuckCharacter joke · No posting date

    Quack. (Translation: have you tried printing it?)

  3. RecruiterCharacter joke · No posting date

    We loved your energy. We went with the candidate whose code compiled.