Practice
1. You are given an array that was originally sorted in ascending order but then rotated at some unknown pivot. The array may contain duplicates. Your task is to find the minimum element efficiently. Which approach best guarantees an optimal solution for this problem?
easy
Solution
Step 1: Understand problem constraints
The array is rotated and may contain duplicates, which complicates the binary search because duplicates can hide the pivot.Step 2: Identify suitable algorithm
Modified binary search that shrinks the search space when duplicates are encountered ensures correctness and efficiency on average.Final Answer:
Option D -> Option DQuick Check:
Modified binary search handles duplicates correctly [OK]
Hint: Duplicates require shrinking high pointer in binary search [OK]
Common Mistakes:
- Assuming simple binary search works without handling duplicates
- Using greedy or DP unnecessarily
2. You are given an array of positions of balls on a number line and a number
m. You want to place m balls in these positions such that the minimum distance between any two balls is maximized. Which algorithmic approach guarantees finding the optimal minimum distance efficiently?easy
Solution
Step 1: Understand the problem structure
The problem asks to maximize the minimum distance between placed balls, which is a classic optimization problem over a numeric answer space.Step 2: Identify the correct approach
Binary search on the answer space (possible distances) combined with a greedy feasibility check efficiently finds the maximum minimum distance. Greedy alone or DP are either incorrect or inefficient.Final Answer:
Option A -> Option AQuick Check:
Binary search narrows down distance candidates efficiently [OK]
Hint: Maximize minimum distance -> binary search on answer space [OK]
Common Mistakes:
- Thinking greedy alone suffices without binary search
- Using DP which is inefficient here
3. What is the time complexity of counting occurrences of a target in a sorted array using binary search to find left and right boundaries?
medium
Solution
Step 1: Analyze binary search calls
Two binary searches are performed independently, each O(log n).Step 2: Combine complexities
Total time is O(log n) + O(log n) = O(log n), since constants are ignored.Final Answer:
Option B -> Option BQuick Check:
Two binary searches do not multiply complexities [OK]
Hint: Two O(log n) calls sum to O(log n) [OK]
Common Mistakes:
- Confusing two calls as O(log n * log n)
- Assuming linear scan is needed
- Believing built-in functions run in O(1)
4. The following code attempts to find the k-th smallest number in an m x n multiplication table. Which line contains a subtle bug that can cause incorrect results for large inputs?
medium
Solution
Step 1: Identify counting logic
The counting function must cap counts per row by n, since each row has only n elements.Step 2: Analyze line 6
Line 6 adds x // i without min(n, x // i), causing overcounting when x // i > n, leading to incorrect binary search decisions.Final Answer:
Option B -> Option BQuick Check:
Bug causes wrong counts and incorrect final answer [OK]
Hint: Count per row capped by n to avoid overcounting [OK]
Common Mistakes:
- Forgetting min(n, x//i) in counting
- Incorrect mid calculation
5. Consider the following buggy code snippet for finding the pivot in a rotated sorted array. Which line contains the subtle bug that can cause incorrect pivot detection?
medium
Solution
Step 1: Identify mid calculation issue
mid is computed using division '/', which returns float in Python 3, causing invalid indexing.Step 2: Impact of float mid
Using float as index causes runtime error or incorrect behavior; integer division '//' is required.Final Answer:
Option B -> Option BQuick Check:
Changing '/' to '//' fixes the bug [OK]
Hint: mid must be integer index [OK]
Common Mistakes:
- Using '/' instead of '//' in Python
- Misplaced comparison operators
