Skip to content

ORIGINAL CODING QUESTION

Directed dependency loop check

Edges are directed. Print 1 if a directed cycle exists, otherwise 0.

Open practice workspace

Original exercise · medium. Editorial role relevance does not imply a company has asked this question.

Input and output

n m, then m vertex pairs (u,v). Vertices are zero-based.

Constraints

1 ≤ n ≤ 30; 0 ≤ m ≤ 100; vertex IDs in [0,n).

Example 1

Input

3 3 0 1 1 2 2 0

Output

1

The arrows form a cycle.

Example 2

Input

3 3 0 1 0 2 1 2

Output

0

The directed triangle is acyclic.

Explain your approach

Explain your time and auxiliary space complexity. Which input makes your algorithm work hardest?

Private grading inputs, expected outputs and canonical reference implementations are withheld. Authored expected outputs were cross-checked with independent reference oracles; compiler/runtime behavior still needs a verified execution provider.

Hint 1

Acyclic dependencies can be removed in zero-indegree order.

Hint 2

Remove each resolved vertex’s outgoing contributions.

Hint 3

Unremoved vertices mean at least one cycle remains.

Practice this question

Save your work privately, explain decisions and return to your revision queue.

Open practice workspaceBrowse the question library →