Practice
1. Consider the following Python code to find the town judge:
def findJudge(n, trust):
trust_score = [0] * (n + 1)
for a, b in trust:
trust_score[a] -= 1
trust_score[b] += 1
for i in range(1, n + 1):
if trust_score[i] == n - 1:
return i
return -1
print(findJudge(3, [[1,3],[2,3]]))
What is the output of this code?easy
Solution
Step 1: Trace trust_score updates
For trust pairs [1,3] and [2,3]: trust_score[1] -= 1, trust_score[3] += 1; trust_score[2] -= 1, trust_score[3] += 1. Final trust_score = [0, -1, -1, 2].Step 2: Identify judge by trust_score == n-1
n=3, so n-1=2. Only person 3 has trust_score 2, so return 3.Final Answer:
Option C -> Option CQuick Check:
Person 3 trusted by 2 others, trusts nobody [OK]
Hint: Trust score array sums trust and distrust correctly [OK]
Common Mistakes:
- Returning the person who trusts others
- Off-by-one indexing in trust_score
- Confusing trust_score values with counts
2. Consider the following Python code implementing flood fill using DFS. Given the input image = [[1,1,1],[1,1,0],[1,0,1]], starting pixel (sr=1, sc=1), and newColor=2, what is the final image returned?
easy
Solution
Step 1: Trace dfs starting at (1,1) with original color=1
The pixel at (1,1) is recolored to 2. Its neighbors with color 1 are (0,1), (2,1) is 0 so skipped, (1,0), and (1,2) is 0 skipped.Step 2: Recursively recolor connected pixels with color 1
All connected pixels with color 1 reachable from (1,1) are recolored to 2, resulting in [[2,2,2],[2,2,0],[2,0,1]]. The pixel at (2,2) remains 1 as it is not connected 4-directionally.Final Answer:
Option C -> Option CQuick Check:
Only connected pixels with original color changed [OK]
Hint: Trace DFS calls carefully to find connected pixels [OK]
Common Mistakes:
- Changing pixels diagonally connected
- Forgetting to check boundaries
3. Consider the following code snippet that finds the smallest set of vertices to reach all nodes in a DAG. Given
n = 4 and edges = [[0,1],[1,2],[2,3]], what is the final returned list?easy
Solution
Step 1: Initialize in-degree and queue
Nodes: 0,1,2,3; in-degree: [0,1,1,1]. Only node 0 has zero in-degree, so queue = [0], result = [0].Step 2: Process queue
Pop 0, decrement in-degree of 1 to 0, enqueue 1. Pop 1, decrement in-degree of 2 to 0, enqueue 2. Pop 2, decrement in-degree of 3 to 0, enqueue 3. Pop 3, no neighbors.Final Answer:
Option A -> Option AQuick Check:
Only node 0 has zero in-degree initially, so result is [0] [OK]
Hint: Only nodes with zero in-degree initially are in the result [OK]
Common Mistakes:
- Returning nodes that become zero in-degree later
- Returning all nodes visited in BFS
- Confusing final queue state with result
4. Given the following Python code snippet implementing the BFS approach for Pacific Atlantic Water Flow, and the input heights = [[1,2,2],[3,2,3],[2,4,5]], what is the value of pacific_reachable after initializing the queues and marking the border cells?
easy
Solution
Step 1: Mark Pacific border cells
Pacific borders are first row and first column. So pacific_reachable[0][j] and pacific_reachable[i][0] are True.Step 2: Fill pacific_reachable matrix
For m=3, n=3, first row and first column cells are True, others False initially.Final Answer:
Option A -> Option AQuick Check:
First row and first column are True, rest False [OK]
Hint: Pacific reachable True on top row and left column [OK]
Common Mistakes:
- Forgetting to mark all border cells
- Marking wrong cells True
5. What is the time complexity of the optimal approach using a single trust score array to find the town judge, given
n people and m trust pairs?medium
Solution
Step 1: Identify loops in the algorithm
The algorithm iterates once over all trust pairs (m) and then once over all people (n).Step 2: Combine complexities
Processing trust pairs is O(m), scanning people is O(n), total O(n + m).Final Answer:
Option D -> Option DQuick Check:
Single pass over trust and people arrays [OK]
Hint: Single pass over trust pairs and people yields O(n + m) [OK]
Common Mistakes:
- Assuming nested loops cause O(n*m)
- Confusing trust pairs count with people count
- Ignoring linear scans after trust processing
