Practice
canFinish(3, [[1,0],[2,1]]) after the while loop completes?Solution
Step 1: Initialize graph and in-degree arrays
Graph: 0 -> [1], 1 -> [2], 2 -> []. In-degree: [0,1,1]. Queue starts with nodes having in-degree 0 -> [0].Step 2: Process queue and update count
Pop 0, count=1; decrement in-degree of 1 to 0, enqueue 1. Pop 1, count=2; decrement in-degree of 2 to 0, enqueue 2. Pop 2, count=3; queue empty.Final Answer:
Option B -> Option BQuick Check:
All courses processed, so return true [OK]
- Off-by-one error in count increment
- Misunderstanding queue initialization
- Confusing in-degree decrement timing
Solution
Step 1: Identify that updating dist[nr][nc] without checking if it was visited causes overwriting distances.
This leads to revisiting cells multiple times, causing infinite loops or incorrect distances.Step 2: The missing condition dist[nr][nc] == -1 before updating and enqueueing is the bug.
Without it, BFS does not behave correctly as a shortest path search.Final Answer:
Option A -> Option AQuick Check:
Visited check prevents infinite loops and ensures correct distances [OK]
- Forgetting visited check causes infinite loops
- Misplacing initialization lines
- Incorrect early return conditions
n nodes and n-1 edges?Solution
Step 1: Analyze edge processing
Each of then-1edges is processed once, updating frequency counts in O(1) time per update.Step 2: Frequency dictionary iteration
Iterating over frequencies of up tonnodes is O(n), no sorting involved.Final Answer:
Option C -> Option CQuick Check:
Linear time complexity matches single pass over edges and nodes [OK]
- Assuming sorting is needed, adding O(n log n)
- Confusing number of edges with n² complexity
- Thinking frequency updates take O(n) each
Solution
Step 1: Understand impact of self-loops and multiple edges
Self-loops increase in-degree of a node, and multiple edges increase in-degree counts accordingly.Step 2: Modify in-degree initialization to count all edges accurately
Counting all edges including self-loops ensures Kahn's Algorithm correctly identifies cycles by never reducing in-degree to zero for cyclic nodes.Step 3: Confirm no other algorithm change needed
Algorithm logic remains the same; accurate in-degree counts handle these cases.Final Answer:
Option C -> Option CQuick Check:
Proper in-degree counts detect cycles including self-loops [OK]
- Ignoring self-loops leads to false positives
- Assuming BFS cannot detect self-loops
- Treating multiple edges as single edge incorrectly
Solution
Step 1: Understand revisiting impact
Allowing revisits means shortest path distances can improve after initial visit, so standard Dijkstra's visited set is insufficient.Step 2: Adapt algorithm
Dijkstra must allow updating distances and pushing nodes multiple times to handle cycles correctly.Step 3: Evaluate other options
DFS is inefficient; binary search + BFS may work but requires careful feasibility checks; naive Dijkstra fails.Final Answer:
Option D -> Option DQuick Check:
Allowing distance updates handles cycles correctly [OK]
- Assuming no changes needed for revisits
- Using DFS without pruning causes TLE
- Binary search feasibility fails without updates
