Dsa
Graphs Dfs
Easy
Supply Chain Loop Detector
A factory produces various items. Each item might depend on other items to be manufactured. Given a list of dependencies, determine if item 0 is involved in any 'Infinite Loop' (a circular dependency) such that it could never be completed. Note that item 0 might not be directly in the loop, but it might depend on an item that is part of a loop.
Input: The first line contains N (the number of items, 0 to N-1). The next N lines start with an item ID followed by K (number of dependencies) and then K space-separated item IDs that this item depends on.
Output: Print 'LOOP' if item 0 is stalled by a circular dependency, otherwise 'SAFE'.
Example:
Input:
3
0 1 1
1 1 2
2 1 0
Output:
LOOP
Key concepts
graphs_dfscycle_detection
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