Practice
1. You are given a list of courses and their prerequisite pairs. You need to determine if it is possible to finish all courses without running into a circular dependency. Which algorithmic approach guarantees an optimal solution to detect such cycles in the course dependency graph?
easy
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]
Hint: Topological sort detects cycles by processing all nodes [OK]
Common Mistakes:
- Greedy approach fails on complex dependencies
- Dynamic programming is not suited for cycle detection
- Brute force is inefficient and impractical
2. Consider the following Python code implementing the optimal solution for the minimum effort path problem. Given the input heights = [[1,2,2],[3,8,2],[5,3,5]], what is the final returned minimum effort?
easy
Solution
Step 1: Trace initial steps
Start at (0,0) with effort 0. From (0,0), neighbors are (1,0) with effort max(0,|1-3|)=2 and (0,1) with effort max(0,|1-2|)=1.Step 2: Explore paths and update efforts
Heap pops (0,0,1) with effort 1, then explores neighbors updating efforts. The minimal maximum effort path found is 2.Final Answer:
Option A -> Option AQuick Check:
Manual tracing confirms minimal effort is 2 [OK]
Hint: Trace heap pops and max edge efforts [OK]
Common Mistakes:
- Confusing sum of differences with max difference
- Off-by-one in indices
- Returning effort before reaching target
3. What is the time complexity of the optimal BFS approach for the Surrounded Regions problem on an m x n board?
medium
Solution
Step 1: Analyze BFS traversal
Each cell is enqueued and dequeued at most once during BFS marking, so total operations are proportional to the number of cells, m*n.Step 2: Consider neighbor checks
Each cell checks up to 4 neighbors, but this is constant factor, so complexity remains O(m*n).Final Answer:
Option A -> Option AQuick Check:
Linear scan plus BFS over all cells is O(m*n) [OK]
Hint: BFS visits each cell once, so O(m*n) time [OK]
Common Mistakes:
- Confusing BFS with brute force approach leading to O(m*n*(m+n)).
- Counting neighbor checks as multiplicative rather than constant factor.
- Assuming only boundary cells matter for complexity.
4. Suppose the problem is modified so that the town judge can trust some people but must still be trusted by everyone else. Which modification to the optimal trust score approach correctly finds the judge?
hard
Solution
Step 1: Understand the relaxed condition
The judge can trust others, so the original condition that judge trusts nobody is removed.Step 2: Use in-degree only
Count how many people trust each person (in-degree). The judge must have in-degree n-1 regardless of out-degree.Final Answer:
Option A -> Option AQuick Check:
In-degree array correctly identifies judge under relaxed trust condition [OK]
Hint: In-degree alone suffices if judge can trust others [OK]
Common Mistakes:
- Trying to use trust_score with equality check
- Ignoring out-degree but still checking it
- Adding unnecessary verification passes
5. Suppose the problem is modified so that water can flow uphill by at most 1 unit of height difference (i.e., from a cell to a neighbor if neighbor's height ≥ current height - 1). Which modification to the BFS approach correctly handles this variant?
hard
Solution
Step 1: Understand uphill flow constraint
Allowing uphill flow by 1 unit means edges have weights or costs, not uniform.Step 2: BFS is insufficient
Standard BFS assumes uniform edge cost; to handle varying constraints, Dijkstra's algorithm with a priority queue is needed.Step 3: Modify algorithm
Use priority queue BFS (Dijkstra) to explore cells with minimal cost respecting height difference constraints.Final Answer:
Option D -> Option DQuick Check:
Uphill flow requires weighted graph traversal -> Dijkstra [OK]
Hint: Uphill flow needs weighted BFS -> Dijkstra [OK]
Common Mistakes:
- Trying to fix with simple BFS condition change
- Running BFS from every cell causes TLE
