Data Structures & AlgorithmsModerateProduct Company SDE · binary search round
Problem: Which claim about a binary search over a rotated sorted array (no duplicates) finding the minimum element is correct?
- A. It needs O(n) because the rotation point could be anywhere
- B. It runs in O(log n) by comparing the middle value with the right end to decide which half holds the rotation point
- C. It is impossible because the array is not sorted
- D. It runs in O(sqrt n) with block jumps
Correct answer: B. It runs in O(log n) by comparing the middle value with the right end to decide which half holds the rotation point
If the middle is greater than the right end, the minimum lies to the right; otherwise it lies in the left part including the middle. Each step halves the range.
More Coding Interview Problems questions
- Problem: How many pairs (i < j) of a = [1, 2, 4, 5, 7, 8] have a[i] - a[j] divisible by 3? Run the code. python a = [1, 2, 4, 5, 7, 8]…
- Problem: What is the minimum number of meeting rooms needed for intervals [0,30], [5,10], [15,20]? Run the sweep-line code. python iv =…
- Problem: How many inversions (pairs i < j with a[i] > a[j]) does a = [2, 0, 2, 1, 1, 0] have? Run the code. python a = [2, 0, 2, 1, 1, 0]…
- Problem: How many integers x in [1, 50] are perfect sixth powers (x = y^6 for an integer y)? Run the code. python print(sum(1 for x in…
- Problem: Merging ropes of lengths 2, 3, 4 costs the sum of the two lengths merged each time, always merging the two shortest. What is the…
- Problem: How many subsets (including the empty one) of {1, 2, 3, 4, 5} have a sum divisible by 4? Run the code. python print(sum(1 for m…