Data Structures & AlgorithmsModerateProduct company OA · Amazon/Microsoft-style OA
Array a = [2, 7, -3, 5, 11, 4, 9, -1, 6]. How many index pairs (i, j) with i < j have a[i] + a[j] = 10?
- A. 2
- B. 4
- C. 3
- D. 6
Correct answer: A. 2
Each unordered pair of positions is counted once. Counting ordered pairs doubles the answer, and counting i <= j adds self-pairs, which matter only when 2x = 10 with a repeated lookup; here the rule is simply one count per index pair.
More Coding Interview Problems questions
- Array a = [2, 1, -1, 4, -2, 3, 0, 1, 1, 0, 2]. How many indices i have the sum of the elements strictly left of i equal to the sum of the…
- Array a = [3, 1, 2, 6, 4, 5, 9]. How many index pairs (i < j) satisfy a[i] mod 3 = a[j] mod 3?
- Sorted array a = [-4, -1, 0, 2, 3, 5, 7]. How many triples of distinct indices i < j < k have a[i] + a[j] + a[k] <= 6?
- For the positive array a = [2, 3, 1, 2, 4, 3] and S = 7, what is the length of the shortest contiguous subarray whose sum is at least 7?
- Sorted array a = [1, 3, 4, 7, 10, 12, 15]. How many pairs of indices i < j satisfy a[j] - a[i] <= 3?
- Given the array [-1, 2, 1, -4, 6, 3] and target 1, pick three different elements whose sum is as close to 1 as possible. What is that…