Practice
1. Consider the following Python function implementing space-optimized bottom-up DP for edit distance. What is the value of
current[1] after the first iteration of the inner loop when calling minDistance("abc", "ac") during the iteration where i = 2?easy
Solution
Step 1: Identify iteration i=2 corresponds to last character 'c' in "abc"
At i=2, word1[i] = 'c'. The inner loop runs j=1 to 0 for word2="ac".Step 2: Compute current[1] when j=1 (word2[1] = 'c')
Since word1[2] == word2[1] ('c' == 'c'), current[1] = next_row[2]. Initially, next_row = [2,1,0], so next_row[2] = 0.Final Answer:
Option D -> Option DQuick Check:
Matching chars yield current[j] = next_row[j+1] = 0 [OK]
Hint: Matching chars copy diagonal DP value [OK]
Common Mistakes:
- Off-by-one indexing in j or i
- Confusing current and next_row arrays
2. You are given an undirected graph with n nodes. You need to find the shortest path length that visits every node at least once. Which algorithmic approach guarantees finding the optimal solution efficiently for this problem?
easy
Solution
Step 1: Understand problem constraints
The problem requires visiting all nodes with the shortest path, which is a variant of the Traveling Salesman Problem (TSP) on an unweighted graph.Step 2: Identify suitable algorithm
Greedy and simple DFS approaches do not guarantee shortest paths and are inefficient. DP on subsets is correct but has O(n*2^n) complexity. Bidirectional BFS with bitmask state compression reduces complexity to O(n*2^(n/2)), making it optimal and efficient.Final Answer:
Option D -> Option DQuick Check:
Bitmask BFS tracks visited nodes efficiently and bidirectional search halves the state space [OK]
Hint: Bitmask + bidirectional BFS optimizes shortest path visiting all nodes [OK]
Common Mistakes:
- Assuming greedy nearest neighbor always finds shortest path
- Using DFS without pruning leads to exponential blowup
- Confusing DP on subsets with simpler DP variants
3. What is the worst-case time complexity of the optimal backtracking solution for the Expression Add Operators problem, given a numeric string of length n?
medium
Solution
Step 1: Identify branching factor per digit
At each digit (except the first), there are 4 choices: add '+', '-', '*', or concatenate (no operator).Step 2: Calculate total combinations
With n digits, the number of expressions is roughly 4^(n-1), which is O(4^n) in worst case.Final Answer:
Option C -> Option CQuick Check:
4 choices per digit lead to exponential 4^n growth [OK]
Hint: Count operator choices plus concatenation per digit [OK]
Common Mistakes:
- Confusing 3 operators only, ignoring concatenation
- Assuming polynomial complexity
4. Identify the bug in the following MedianFinder code snippet that causes incorrect median calculation or runtime errors.
medium
Solution
Step 1: Examine heap insertion condition
small is a max heap simulated by negating values, so top is -small[0]. Comparing num to self.small[0] (positive) is incorrect.Step 2: Correct condition
Should compare num <= -self.small[0] to correctly decide which heap to push into.Final Answer:
Option C -> Option CQuick Check:
Negation missing in comparison causes wrong heap insertion [OK]
Hint: Remember to negate values when simulating max heap in Python [OK]
Common Mistakes:
- Forgetting to negate values in comparisons
- Not balancing heaps after insertion
- Incorrect median calculation when heaps unbalanced
5. What is the time complexity of the bitmask backtracking solution for the N-Queens problem, and why is it not simply O(n^n)?
medium
Solution
Step 1: Understand pruning effect on search space
Each queen placed reduces available columns, so permutations of columns are tried, not all combinations.Step 2: Recognize factorial complexity
Backtracking tries permutations of n columns for n rows, leading to O(n!) complexity, not exponential or polynomial.Final Answer:
Option C -> Option CQuick Check:
Pruning reduces search to permutations, not all subsets [OK]
Hint: N-Queens backtracking explores permutations -> O(n!) [OK]
Common Mistakes:
- Confusing subsets (2^n) with permutations (n!)
- Assuming polynomial due to board size
- Ignoring pruning effect
