Practice
Solution
Step 1: Understand the problem as cycle detection in a directed graph
The problem requires detecting if a cycle exists in the course prerequisite graph, which invalidates any course order.Step 2: Identify the algorithm that detects cycles efficiently
Kahn's Algorithm performs a topological sort using BFS and in-degree counts. If all nodes are processed, no cycle exists; otherwise, a cycle is detected.Final Answer:
Option A -> Option AQuick Check:
Topological sort with BFS is the standard cycle detection method for this problem [OK]
- Greedy approach fails on complex dependencies
- Dynamic programming is not suited for cycle detection
- Brute force is inefficient and impractical
Solution
Step 1: Identify BFS visits each cell at most once
Each cell is enqueued and dequeued once, so total operations proportional to number of cells.Step 2: Each cell checks up to 4 neighbors in constant time
Neighbor checks are constant per cell, so total is O(m*n).Final Answer:
Option C -> Option CQuick Check:
Linear in grid size, no repeated visits [OK]
- Assuming BFS revisits cells multiple times
- Confusing rows+cols with total cells
- Ignoring constant neighbor checks
n nodes and e edges?Solution
Step 1: Identify BFS visits each node once
Each node is enqueued and dequeued exactly once, so node visits are O(n).Step 2: Identify BFS visits each edge twice
Each undirected edge is checked from both endpoints, so edge visits are O(e).Final Answer:
Option C -> Option CQuick Check:
Sum of node and edge visits -> O(n + e) [OK]
- Assuming BFS repeats visits causing O(n²)
- Confusing edge visits with sorting cost
from collections import deque
def solve(board):
if not board or not board[0]:
return
m, n = len(board), len(board[0])
queue = deque()
for i in range(m):
for j in [0, n-1]:
if board[i][j] == 'O':
queue.append((i, j))
board[i][j] = 'X' # Bug here
for j in range(n):
for i in [0, m-1]:
if board[i][j] == 'O':
queue.append((i, j))
board[i][j] = 'X' # Bug here
while queue:
x, y = queue.popleft()
for dx, dy in [(-1,0),(1,0),(0,-1),(0,1)]:
nx, ny = x + dx, y + dy
if 0 <= nx < m and 0 <= ny < n and board[nx][ny] == 'O':
board[nx][ny] = 'S'
queue.append((nx, ny))
for i in range(m):
for j in range(n):
if board[i][j] == 'O':
board[i][j] = 'X'
elif board[i][j] == 'S':
board[i][j] = 'O'
Solution
Step 1: Identify the purpose of marking boundary 'O's
Boundary 'O's must be marked as safe (e.g., 'S'), not flipped to 'X' immediately.Step 2: Locate the bug
The lines setting boundary 'O's to 'X' instead of 'S' cause these safe cells to be flipped incorrectly.Final Answer:
Option D -> Option DQuick Check:
Marking safe cells incorrectly flips boundary 'O's [OK]
- Flipping boundary 'O's before BFS causes incorrect final board.
- Not marking visited cells leads to infinite loops.
- Only checking immediate neighbors without full BFS traversal.
Solution
Step 1: Understand movement restriction impact
Reducing to 4 directions limits possible paths, potentially increasing shortest path length.Step 2: Evaluate algorithm suitability
Bidirectional BFS still finds shortest path efficiently; DFS and greedy do not guarantee shortest path.Final Answer:
Option B -> Option BQuick Check:
Bidirectional BFS adapts to movement constraints, still optimal [OK]
- Assuming DFS or greedy become optimal
- Thinking DP is needed for movement changes
