Data Structures & AlgorithmsHardProduct company OA · Amazon/Microsoft-style OA
Array [1, 3, 5, 4, 2, 6, 8, 7, 9]. What is the length of the shortest contiguous segment that, once sorted, makes the whole array sorted?
- A. 7
- B. 5
- C. 4
- D. 9
Correct answer: A. 7
Scan for the leftmost index where a value exceeds a later minimum (index 1, value 3 > 2) and the rightmost where a value is below an earlier maximum (index 7, value 7 < 8): the segment 1..7 has length 7. Counting only the misplaced positions (1, 2, 4, 6, 7) gives 5, fixing just the first dip [3, 5, 4, 2] gives 4, and 9 sorts the whole array.
More Coding Interview Problems questions
- How many permutations of (0, 1, 2, 3) have exactly one position i with p[i] = i?
- Arrays A = [2, 5, 3, 7] and B = [4, 6, 1, 9]. How many pairs (x from A, y from B) satisfy 10 <= x y <= 40?
- Trains arrive at 100, 140, 150, 180, 200 and depart at 120, 160, 190, 210, 230 (times are not paired). A platform holds one train at a…
- Array [1, 2, 1, 3, 2, 2]. How many contiguous subarrays contain exactly two distinct values?
- Sorted arrays [2, 6, 9, 14] and [4, 7, 11, 15] are merged into one list. How many elements of the merged list lie strictly between 5 and 12?
- Choose a from {6, 9, 12} and b from {3, 5, 10}. What is the largest possible value of a XOR b?