Practice
1. Consider the following Python code using bisect to count occurrences of a target in a sorted array. What is the output when
nums = [1, 2, 2, 3] and target = 2?easy
Solution
Step 1: Trace bisect_left for target=2
bisect_left(nums, 2) returns index 1 (first 2 in array).Step 2: Trace bisect_left for target+1=3
bisect_left(nums, 3) returns index 3 (first 3 in array).Step 3: Calculate count
Count = right - left = 3 - 1 = 2 occurrences.Final Answer:
Option A -> Option AQuick Check:
Indices 1 and 2 are 2's in array [OK]
Hint: bisect_left finds first occurrence index [OK]
Common Mistakes:
- Confusing bisect_left with bisect_right
- Off-by-one errors in index subtraction
- Using bisect_right for target+1 incorrectly
2. You are given an
n x n matrix where each row and each column is sorted in ascending order. You need to find the kth smallest element in the matrix efficiently. Which of the following approaches guarantees the optimal time complexity for this problem?easy
Solution
Step 1: Understand problem constraints
The matrix rows and columns are sorted, so each row is sorted and each column is sorted independently.Step 2: Identify the optimal approach
Using a min-heap to merge sorted rows efficiently extracts the smallest elements in ascending order, achieving O(k log n) time, better than flattening and sorting.Final Answer:
Option D -> Option DQuick Check:
Min-heap merges sorted lists optimally [OK]
Hint: Min-heap merges sorted rows efficiently [OK]
Common Mistakes:
- Assuming flatten and sort is optimal
- Confusing DP with this problem
- Thinking greedy without data structures works
3. What is the time complexity of the optimal binary search algorithm that treats an m x n 2D matrix as a 1D sorted array to search for a target?
medium
Solution
Step 1: Understand the search space size
The matrix has m*n elements, treated as a sorted 1D array.Step 2: Apply binary search complexity
Binary search on an array of size m*n takes O(log(m*n)) time.Final Answer:
Option A -> Option AQuick Check:
Binary search halves search space each step, log scale [OK]
Hint: Binary search complexity depends on total elements, not rows or columns separately [OK]
Common Mistakes:
- Confusing with O(m*n) brute force
- Thinking binary search per row is O(m log n)
- Adding logs incorrectly as O(log m + log n)
4. Suppose the problem is modified so that the input array
arr can contain negative numbers and zero, and you still need to find the kth positive missing number (positive integers missing from the array). Which of the following changes to the optimal binary search approach is correct?hard
Solution
Step 1: Understand the problem modification
Negative numbers and zero do not affect the count of missing positive integers, but they disrupt the assumption that arr contains only positive sorted numbers.Step 2: Adjust the input for binary search
Filtering out non-positive numbers creates a sorted array of positive integers, restoring the problem's original assumptions and enabling correct binary search.Step 3: Reject other options
No change (B) fails because negative numbers break missing count logic; offset (C) is ambiguous and error-prone; brute force (D) is inefficient and unnecessary.Final Answer:
Option C -> Option CQuick Check:
Filtering preserves correctness and efficiency [OK]
Hint: Filter positives before binary search [OK]
Common Mistakes:
- Assuming binary search works without filtering
- Trying to adjust missing count with offsets
- Switching to brute force unnecessarily
5. Suppose the problem is modified so that
x can be negative, and you want to return the integer square root of its absolute value. Which modification to the Newton's method code below correctly handles this variant without breaking the algorithm?hard
Solution
Step 1: Understand the problem change
Allowing negativexmeans we must compute the integer square root of|x|.Step 2: Modify input safely
Addingx = abs(x)at the start ensures the algorithm works unchanged on non-negative inputs.Step 3: Avoid incorrect changes
Removing base cases or changing loop conditions breaks correctness; floating point division violates problem constraints.Final Answer:
Option C -> Option CQuick Check:
Absolute value conversion preserves algorithm correctness [OK]
Hint: Convert input to non-negative before applying integer sqrt [OK]
Common Mistakes:
- Removing base cases
- Changing loop condition incorrectly
- Using float division causing precision errors
