Practice
numIslands(grid)?
grid = [
["1","1","0"],
["0","1","0"],
["1","0","1"]
]Solution
Step 1: Trace DFS calls starting from (0,0)
DFS marks (0,0), (0,1), and (1,1) as visited, sinking one island.Step 2: Identify remaining islands
Next unvisited '1' is at (2,0) (second island), then at (2,2) (third island). Total islands counted = 3.Final Answer:
Option D -> Option DQuick Check:
Counting connected components by DFS yields 3 islands [OK]
- Off-by-one counting islands
- Missing diagonal connections (which don't count)
max_time after processing the grid [[2,1,1],[1,1,0],[0,1,1]]?
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 and time_grid[nr][nc] > time + 1):
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: Trace DFS from rotten orange at (0,0) with time=0
It rots adjacent fresh oranges incrementally, updating time_grid and max_time as rot spreads.Step 2: Follow rot spread minute by minute
Rot spreads to (0,1) and (1,0) at time=1, then to (0,2), (1,1), and (2,1) at time=2, and finally to (2,2) at time=3.Final Answer:
Option A -> Option AQuick Check:
Maximum rot time matches the longest path from initial rotten orange [OK]
- Off-by-one counting minutes
- Ignoring multiple rotten sources
- Confusing grid indices
grid = [
[0, 1, 0],
[1, 0, 0],
[1, 1, 0]
]
# Code snippet uses the bidirectional BFS approach as described.
What is the output of shortestPathBinaryMatrix(grid)?Solution
Step 1: Trace BFS from start (0,0)
Start at (0,0) with distance 1. Neighbors reachable: (1,1) at distance 2.Step 2: Trace BFS from end (2,2)
Start at (2,2) with distance 1. Neighbors reachable: (1,2) at distance 2, (1,1) at distance 2.Step 3: Meeting point
Both searches meet at (1,1). Sum distances: 2 (start side) + 2 (end side) = 4, but since both include the meeting cell, shortest path length is 3.Final Answer:
Option A -> Option AQuick Check:
Shortest path length is 3 steps [OK]
- Off-by-one in path length
- Ignoring meeting cell counted twice
n nodes and e edges?Solution
Step 1: Identify loops in the algorithm
The algorithm iterates over all edges once to build the graph and in-degree array, then processes each node and its edges once during the queue processing.Step 2: Combine complexities
Building graph: O(e), processing nodes and edges in BFS: O(n + e). No nested loops over all nodes and edges.Final Answer:
Option B -> Option BQuick Check:
Linear time in nodes and edges for DAG traversal [OK]
- Assuming nested loops cause O(n*e)
- Confusing with dense graph complexities
- Thinking priority queues are used
Solution
Step 1: Understand impact of allowing word reuse
Allowing reuse means cycles can form, so simply removing visited causes infinite loops.Step 2: Modify algorithm to track path length and avoid infinite loops
We must allow revisits but prevent infinite cycles by tracking path length or depth limits.Step 3: Why other options fail
Removing visited causes infinite loops; resetting visited loses progress; priority queue is unrelated to reuse handling.Final Answer:
Option A -> Option AQuick Check:
Tracking path length prevents infinite loops with reuse [OK]
- Removing visited causes infinite loops
- Resetting visited loses pruning
- Using priority queue doesn't solve reuse
