Dsa
Queue
Hard
The Dependency-Aware Task Cycler
A single-core processor executes tasks from a queue. Each task has a unique ID and a list of dependency IDs. The processor follows these rules:
1. It picks the task at the head of the queue.
2. If all dependencies for that task have already been completed, the task is processed immediately and marked as 'finished'.
3. If any dependency is not yet finished, the task is moved to the back of the queue.
4. If the processor cycles through the entire current queue without finishing a single task, a 'DEADLOCK' occurs.
Input: First line contains N (number of tasks). The next N lines contain: `TaskID` followed by a space-separated list of its `Dependencies` (if any).
Output: The sequence of TaskIDs in order of completion, or 'DEADLOCK' if a cycle is detected where no progress can be made.
Example:
Input:
3
A B
B
C A
Output:
B A C
Key concepts
queuedependency trackingsimulation
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now