Data Structures & AlgorithmsVery hardProduct Company SDE · Amazon/Microsoft-style OA
Problem: A sorted list 2, 4, 6, 8, 10, 12, 14 is treated as a rotated array: the first 3 values are moved to the end, giving 8, 10, 12, 14, 2, 4, 6 (the rotation is known). Searching for the value 4 with a rotated-array binary search, how many probes (midpoints examined, counting the first) are needed before the value is found, starting with lo = 0 and hi = 6?
- A. 2
- B. 3
- C. 1
- D. 4
Correct answer: A. 2
In a rotated array one half is always sorted, so the search decides which half contains the target. Using plain binary search on the unrotated order mis-directs the search.
More Coding Interview Problems questions
- Problem: Four processes arrive at times 0, 2, 4, 6 with burst times 6, 2, 8, 3. The scheduler is shortest-remaining-time-first…
- Problem: In the grid of 0s and 1s below, count the number of 2×2 all-ones squares. 1110 1111 0111 1111
- Problem: A string s = "abacaba" is given. How many of its substrings are palindromes of odd length (counting index ranges, so repeated…
- Problem: A directed graph has 4 vertices (0–3) and arcs with weights: 0→1 (4), 1→2 (−3), 0→2 (2), 2→3 (1), 1→3 (5), 3→1 (−1). Start with…
- Array a = [4, 2, 6, 1, 5, 3, 3, 9, 2]. How many contiguous subarrays of length at least 3 have a sum divisible by 6?
- Array a = [3, 5, 8, 2, 7, 10, 4]. How many pairs of positions i < j have a[i] + a[j] equal to some value located strictly after position j?