Practice
1. Consider the following Python code implementing the min-heap approach to find the kth smallest element in a sorted matrix. Given the matrix
[[1,3],[2,4]] and k = 3, what value does the function return?easy
Solution
Step 1: Initialize min-heap with first elements
Push (1,0,0) and (2,1,0) into heap.Step 2: Extract elements until count == k
Pop 1 (count=1), push 3 from row 0; pop 2 (count=2), push 4 from row 1; pop 3 (count=3), return 3.Final Answer:
Option C -> Option CQuick Check:
Third smallest element is 3 [OK]
Hint: Trace heap pops and pushes carefully [OK]
Common Mistakes:
- Off-by-one counting of popped elements
- Returning wrong popped value
- Confusing row and column indices
2. Consider the following code snippet that searches for a target in a 2D matrix by performing binary search on each row. Given the matrix = [[1,3,5],[7,9,11],[13,15,17]] and target = 9, what is the return value of the function
searchMatrix(matrix, target)?easy
Solution
Step 1: Trace binary search on second row [7,9,11]
Mid index 1 has value 9 which equals target, so binary_search returns True.Step 2: searchMatrix returns True immediately after finding target
Since binary_search returns True for second row, searchMatrix returns True.Final Answer:
Option A -> Option AQuick Check:
Target found in second row at mid index 1 [OK]
Hint: Binary search returns True when target found [OK]
Common Mistakes:
- Off-by-one errors in mid calculation
- Returning False prematurely
- Confusing return values
3. What is the time complexity of the optimal binary search algorithm that finds the single non-duplicate element in a sorted array where every other element appears twice?
medium
Solution
Step 1: Identify loop behavior
The while loop halves the search space each iteration by adjusting low or high based on parity comparisons.Step 2: Confirm complexity
Halving search space each iteration leads to O(log n) time complexity.Final Answer:
Option B -> Option BQuick Check:
Binary search is classic O(log n) [OK]
Hint: Halving search space -> logarithmic time [OK]
Common Mistakes:
- Confusing linear scan with binary search
- Assuming parity checks add extra complexity
- Misreading O(1) as possible for search
4. What is the time complexity of the Newton's method approach for computing the integer square root of a number
x?medium
Solution
Step 1: Understand Newton's method convergence
Newton's method converges quadratically, roughly doubling correct digits each iteration.Step 2: Analyze iteration count relative to input size
Because of quadratic convergence, the number of iterations is proportional tolog log x, making time complexityO(log log x).Final Answer:
Option A -> Option AQuick Check:
Newton's method converges faster than logarithmic time [OK]
Hint: Newton's method converges quadratically -> O(log log x) [OK]
Common Mistakes:
- Confusing with brute force O(√x)
- Assuming linear O(x) complexity
- Mistaking logarithmic O(log x)
5. Suppose the array is no longer sorted and may contain duplicates, and you still need to count occurrences of a target efficiently. Which approach is best suited to handle this scenario?
hard
Solution
Step 1: Recognize unsorted array limitation
Binary search requires sorted data; it cannot be applied directly here.Step 2: Evaluate alternatives
Sorting first adds O(n log n) overhead; hash map counting is O(n) and direct.Step 3: Choose hash map approach
Hash map counts occurrences in one pass with O(n) time and O(n) space, optimal for unsorted arrays.Final Answer:
Option C -> Option CQuick Check:
Hash map counting works regardless of order [OK]
Hint: Unsorted array -> hash map counting [OK]
Common Mistakes:
- Trying binary search on unsorted data
- Sorting then binary searching is slower
- Greedy skipping duplicates misses counts
