Data Structures & AlgorithmsVery hardProduct company OA · Amazon/Microsoft-style OA
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?
- A. 5
- B. 4
- C. 2
- D. 6
Correct answer: B. 4
For each pair, check the sum against the suffix after j, using a set of values in that suffix to make each test O(1). Checking values before j instead answers a different question.
More Coding Interview Problems questions
- Sorted array b = [1, 2, 3, 4, 5, 7, 8, 9]. What is the largest product b[i] b[j] over index pairs with j - i >= 3?
- Sorted array [1, 3, 4, 6, 8, 9, 11]. How many index triples i < j < k satisfy c[k] - c[i] <= 4?
- How many strings of length 5 over the alphabet {a, b, c} have no two adjacent characters equal?
- How many non-empty subsets of {1, 2, 3, 4, 5, 6} have an XOR of their elements that is at least 4?
- How many subsets (including the empty subset) of the values [2, 3, 5, 7, 11, 13, 4, 6] have a sum between 10 and 20 inclusive?
- The values [7, 4, 9, 2, 6, 5] are distributed among two groups (each value goes to exactly one group, any group may be empty). What is the…