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
n nodes labeled from 0 to n-1. You need to determine if the graph forms a valid tree. Which algorithmic approach guarantees an optimal solution to check both connectivity and absence of cycles efficiently?Solution
Step 1: Understand problem requirements
The problem requires checking if the graph is connected and contains no cycles, which defines a tree.Step 2: Identify suitable algorithm
BFS or DFS traversal can detect cycles by tracking visited nodes and parent relationships, and also verify connectivity by ensuring all nodes are visited.Final Answer:
Option D -> Option DQuick Check:
BFS/DFS simultaneously checks connectivity and cycles efficiently [OK]
- Assuming greedy edge addition ensures connectivity
- Using DP which is unrelated here
[[1,3],[0,2],[1,3],[0,2]], what is the return value of isBipartite(graph)?Solution
Step 1: Trace union operations for node 0
Node 0 neighbors: 1 and 3. Union(1,3) is called. Since 1 and 3 are separate, union succeeds.Step 2: Trace union operations for other nodes
For node 1 neighbors: 0 and 2. Union(0,2) is called. 0 and 2 are separate, union succeeds. Similarly for node 2 and 3, unions succeed without conflict.Step 3: Check for conflicts
However, the union-find approach here incorrectly unions neighbors of the same node, which can cause false positives. The code logic is flawed for bipartite checking.Final Answer:
Option B -> Option BQuick Check:
The code returns false due to a union conflict, indicating the graph is not bipartite [OK]
- Confusing union arguments order
- Assuming union returns true on conflict
def orangesRotting(grid):
rows, cols = len(grid), len(grid[0])
time_grid = [[-1]*cols for _ in range(rows)]
max_time = 0
def dfs(r, c, time):
nonlocal max_time
if r < 0 or r >= rows or c < 0 or c >= cols:
return
if grid[r][c] == 0:
return
if time_grid[r][c] != -1 and time_grid[r][c] <= time:
return
time_grid[r][c] = time
max_time = max(max_time, time)
directions = [(1,0),(-1,0),(0,1),(0,-1)]
for dr, dc in directions:
nr, nc = r + dr, c + dc
if 0 <= nr < rows and 0 <= nc < cols:
if grid[nr][nc] == 1 or grid[nr][nc] == 2:
dfs(nr, nc, time + 1)
for r in range(rows):
for c in range(cols):
if grid[r][c] == 2:
dfs(r, c, 0)
return max_time
Solution
Step 1: Identify rot spread condition
Rot should spread to fresh oranges or to rotten oranges only if it improves their rot time.Step 2: Check neighbor condition
Line checking neighbors lacks time_grid comparison for rotten oranges, causing unnecessary or premature DFS calls.Final Answer:
Option C -> Option CQuick Check:
Missing time check causes incorrect rot timing and possible infinite recursion [OK]
- Ignoring time_grid for rotten neighbors
- Updating max_time incorrectly
- Not pruning DFS calls
Solution
Step 1: Add diagonal neighbors to the neighbor list
Include all 8 directions: horizontal, vertical, and diagonal neighbors.Step 2: Mark cells as visited when enqueuing
This prevents adding the same cell multiple times from different neighbors, avoiding double counting.Step 3: Keep BFS traversal logic unchanged otherwise
Running BFS twice or switching to DFS is unnecessary and inefficient.Final Answer:
Option D -> Option DQuick Check:
Mark visited on enqueue avoids duplicates with expanded neighbors [OK]
- Not marking visited on enqueue causing duplicates
- Running BFS multiple times unnecessarily
- Thinking BFS can't handle diagonals
