Practice
1. You are given a sorted array and need to efficiently find the index of a target value or determine it is not present. Which algorithmic approach guarantees the fastest search in O(log n) time by repeatedly dividing the search interval in half?
easy
Solution
Step 1: Understand problem constraints
The array is sorted, enabling divide-and-conquer strategies.Step 2: Identify algorithm matching O(log n) search
Binary search halves the search space each step, guaranteeing O(log n) time, unlike linear or greedy approaches.Final Answer:
Option C -> Option CQuick Check:
Binary search is the classic O(log n) search on sorted arrays [OK]
Hint: Sorted array + fast search -> binary search [OK]
Common Mistakes:
- Confusing greedy or DP with binary search
- Assuming linear scan is optimal
- Thinking DP applies to simple search
2. You are given a sorted array and a target value. You need to find the starting and ending indices of the target value in the array. Which algorithmic approach guarantees an optimal solution with logarithmic time complexity?
easy
Solution
Step 1: Understand problem constraints
The array is sorted, so binary search can be used to find target positions efficiently.Step 2: Identify suitable algorithm
Modified binary search can find leftmost and rightmost indices in O(log n) time, which is optimal compared to linear scan or greedy methods.Final Answer:
Option C -> Option CQuick Check:
Binary search exploits sorted property for O(log n) search [OK]
Hint: Sorted array + position search -> modified binary search [OK]
Common Mistakes:
- Thinking linear scan is optimal
- Confusing greedy with binary search
- Using DP unnecessarily
3. What is the time complexity of the recursive modified binary search algorithm for finding a peak element in an array of size n, and why might some candidates mistakenly think it is worse?
medium
Solution
Step 1: Analyze recursive calls
Each call halves the search space, so number of calls is O(log n).Step 2: Confirm work per call
Each call does O(1) work (simple comparisons), so total time is O(log n).Final Answer:
Option B -> Option BQuick Check:
Halving search space leads to logarithmic time [OK]
Hint: Halving input size -> O(log n) time [OK]
Common Mistakes:
- Confusing recursion stack space with time
- Assuming linear scan inside recursion
- Thinking recursion multiplies complexity
4. Examine the following buggy code that attempts to search a target in a 2D matrix using the staircase search approach starting from the top-left corner. Identify the line containing the subtle bug that causes incorrect behavior or infinite loops.
medium
Solution
Step 1: Analyze starting point
Starting at top-left corner breaks the staircase search logic because we cannot eliminate rows or columns correctly.Step 2: Check pointer movements
Moving row down when current < target and col left when current > target only works if starting at top-right corner.Final Answer:
Option D -> Option DQuick Check:
Starting at top-left causes infinite loops or wrong results [OK]
Hint: Staircase search must start at top-right corner [OK]
Common Mistakes:
- Starting at top-left or bottom-left corner
- Not checking empty matrix
- Moving pointers in wrong directions
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
