Practice
1. Given the following Python code implementing the two-pass DP solution for the 01 Matrix problem, what is the final distance matrix returned for input
[[0,1,1],[1,1,0],[1,1,1]]?easy
Solution
Step 1: Initialize distances
Set dist to 0 where mat is 0, else infinity: [[0,inf,inf],[inf,inf,0],[inf,inf,inf]]Step 2: Forward pass updates
Update dist using top and left neighbors: [[0,1,2],[1,2,0],[2,3,1]]Step 3: Backward pass updates
Update dist using bottom and right neighbors: [[0,1,2],[1,1,0],[2,1,1]]Final Answer:
Option B -> Option BQuick Check:
Distances match shortest paths to nearest zero [OK]
Hint: Two-pass DP updates distances from neighbors correctly [OK]
Common Mistakes:
- Off-by-one in indices
- Forgetting backward pass
- Incorrect initialization
2. Consider the following Python code implementing the Bellman-Ford algorithm for the Network Delay Time problem. Given the input
times = [[1,2,1],[2,3,2],[1,3,4]], n = 3, and k = 1, what is the final returned value?easy
Solution
Step 1: Initialize distances with dist[1]=0, dist[2]=inf, dist[3]=inf
After first iteration, relax edges: dist[2]=1 (via 1->2), dist[3]=4 (via 1->3)Step 2: Second iteration relaxes edge 2->3: dist[3] updated to dist[2]+2=3 which is less than 4
No further updates after this iteration, so loop breaks earlyFinal Answer:
Option B -> Option BQuick Check:
Maximum distance is 3, so return 3 [OK]
Hint: Relax edges repeatedly until no updates or n-1 iterations [OK]
Common Mistakes:
- Not updating dist[3] to 3 after second iteration
- Returning initial max distance 4 instead of updated 3
- Confusing iteration count and stopping early
3. You are given a 2D grid map of '1's (land) and '0's (water). You need to count the number of distinct islands, where an island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. Which algorithmic approach guarantees an optimal solution to this problem?
easy
Solution
Step 1: Understand the problem as finding connected components in a grid
The problem requires identifying groups of adjacent '1's connected horizontally or vertically, which is a classic graph traversal problem.Step 2: Recognize that DFS or BFS traversals efficiently explore connected components
DFS or BFS starting from each unvisited land cell will mark all connected land cells as visited, ensuring each island is counted exactly once.Final Answer:
Option A -> Option AQuick Check:
DFS/BFS is the standard approach for connected components in grids [OK]
Hint: Islands = connected components -> use DFS/BFS [OK]
Common Mistakes:
- Thinking greedy or sorting can identify islands correctly.
4. Consider the following Python code implementing bidirectional BFS for the Word Ladder problem. Given
beginWord = "hit", endWord = "cog", and wordList = ["hot", "dot", "dog", "lot", "log", "cog"], what is the returned value?easy
Solution
Step 1: Trace initial sets and level
begin_set = {"hit"}, end_set = {"cog"}, level = 1Step 2: Expand begin_set neighbors and check for intersection
Level 2: neighbors of "hit" include "hot"; no intersection yet. Level increments to 2.Step 3: Expand smaller frontier each iteration until intersection
Level 3: from "hot" neighbors "dot", "lot"; still no intersection. Level 4: from "dot", "lot" neighbors "dog", "log"; next level neighbors include "cog" which is in end_set, so return level + 1 = 4.Final Answer:
Option A -> Option AQuick Check:
Shortest transformation length is 4 steps [OK]
Hint: Bidirectional BFS returns shortest path length 4 [OK]
Common Mistakes:
- Off-by-one in level counting
- Not updating visited correctly
- Returning 0 if endWord missing
5. Suppose the flood fill problem is modified so that pixels can be recolored multiple times if they become connected again after recoloring neighbors (e.g., due to dynamic color changes). Which modification to the algorithm is necessary to handle this correctly?
hard
Solution
Step 1: Understand the problem variant
Pixels can be recolored multiple times if connectivity changes dynamically, so simple DFS with early exit is insufficient.Step 2: Choose an approach that tracks pixels safely
BFS with a visited set can manage dynamic recoloring by controlling which pixels are processed and preventing infinite loops.Step 3: Explain why other options fail
Use a visited set to prevent recoloring any pixel more than once disallows multiple recolors, B removes necessary early exit causing infinite loops, D risks infinite recursion without control.Final Answer:
Option A -> Option AQuick Check:
BFS with visited set handles dynamic recoloring safely [OK]
Hint: Dynamic recoloring needs careful tracking with BFS and visited set [OK]
Common Mistakes:
- Removing early exit causes infinite loops
- Ignoring visited set leads to stack overflow
