Deadlock involves tasks unable to progress because required resources are held in a circular dependency. Prevention targets the underlying conditions.
Before you start
You should understand processes, threads, memory and basic file operations. Track ownership and state changes over time. For concurrent scenarios, write down at least two possible execution orders; the same instructions can behave differently when scheduling or resource availability changes.
The practical goal is to reason through this situation: Two tasks acquire account locks in opposite order and wait for each other. Read the walkthrough first, then try the interview exercise before opening its answer. The important part is explaining the decision and its consequences, rather than remembering a definition alone.
Step-by-step walkthrough
Step 1: Draw resource waits
A circular dependency can stop all participating tasks.
Step 2: Order acquisition consistently
Both transfers acquire accounts by the same ordering rule.
Step 3: Plan interruption
Timeouts need safe cleanup and do not automatically complete the original operation.
Worked scenario
Two tasks acquire account locks in opposite order and wait for each other.
Task A holds account one and waits for two; task B holds two and waits for one. Neither can progress. A consistent acquisition order breaks that cycle for those locks, while short critical sections reduce contention. Other resources can still form cycles, so examine the whole ownership graph.
Common mistake
Timeouts can reveal a stuck operation without guaranteeing safe recovery.
Verify the behavior
Trace the cycle and test competing transfers under the revised order.
Interview exercise
Prevent a transfer deadlock.
Answer and reasoning
Acquire related locks in one consistent order and keep ownership short, with an explicit interruption or failure policy.
Continue learning
Compare the scenario with the Operating Systems interview questions and test your understanding with the Operating Systems MCQs. For terminology and implementation details, consult the reference material.