Practice
freq after processing all edges for the input edges = [[1,2],[2,3],[4,2]]?Solution
Step 1: Count frequencies for each edge
Edges are [1,2], [2,3], [4,2]. Counting occurrences: 1 appears once, 2 appears in all three edges (3 times), 3 once, 4 once.Step 2: Construct frequency dictionary
freq = {1:1, 2:3, 3:1, 4:1} after processing all edges.Final Answer:
Option A -> Option AQuick Check:
Node 2 appears 3 times, matching n-1=3 for n=4 nodes [OK]
- Miscounting frequency of node 2
- Adding extra counts for nodes
- Confusing node labels with counts
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
from collections import defaultdict
import heapq
def findCheapestPrice(n, flights, src, dst, K):
graph = defaultdict(list)
for u, v, w in flights:
graph[u].append((v, w))
heap = [(0, src, 0)] # cost, node, stops
while heap:
cost, node, stops = heapq.heappop(heap)
if node == dst:
return cost
if stops < K: # BUG: should be stops <= K
for nei, price in graph[node]:
heapq.heappush(heap, (cost + price, nei, stops + 1))
return -1
Solution
Step 1: Understand stops constraint
The problem allows up to K stops, meaning stops can be equal to K, not strictly less.Step 2: Identify bug
The condition 'if stops < K:' excludes paths with exactly K stops, missing valid solutions.Final Answer:
Option A -> Option AQuick Check:
Changing to 'stops <= K' fixes the bug [OK]
- Using stops < K instead of stops <= K
- Ignoring stops count in pruning
- Returning early without checking stops
Solution
Step 1: Analyze BFS visits
Each BFS starts from border cells and visits each cell at most once per ocean, so total visits ≤ 2 * m * n.Step 2: Confirm no repeated work
Visited arrays prevent revisiting cells, so BFS complexity is linear in number of cells.Final Answer:
Option A -> Option AQuick Check:
Each cell processed once per ocean -> O(m*n) [OK]
- Confusing with brute force DFS from every cell
- Assuming BFS uses priority queue
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
