Data Structures & AlgorithmsVery hardProduct company OA · Amazon/Microsoft-style OA
Sorted list [3, 6, 9, 12, 15, 18, 21]. How many index triples i < j < k satisfy vals[i] + vals[j] = vals[k]?
- A. 9
- B. 12
- C. 7
- D. 22
Correct answer: A. 9
Fix the largest index k and use two pointers over the smaller values: a sum that equals vals[k] is a hit, a smaller sum moves the left pointer, and a larger sum moves the right pointer inward. Counting the at-most case gives a larger number that includes these triples.
More Coding Interview Problems questions
- How many contiguous subarrays of [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] sum to exactly 15?
- Array a = [9, 4, 7, 1, 8, 2, 6, 3]. How many index pairs i < j have |a[i] - a[j]| >= 5?
- Among [5, 3, 6, 12, 9, 10, 1], choose two different elements. What is the largest possible value of x OR y?
- How many pairs of distinct elements of [1, 4, 6, 7, 10, 13, 15] have a sum that is a perfect square (1, 4, 9, 16, ...)?
- Closed intervals [2,9], [4,7], [6,12], [10,14], [11,13], [15,18]. What is the total length of the set of points covered by at least one…
- With coins of values 1, 3 and 4 (each usable any number of times), in how many ways (order does not matter) can you make the amount 6?