Data Structures & AlgorithmsVery hardProduct company OA · Amazon/Microsoft-style OA
Sorted array a = [-2, -1, -1, 0, 1, 1, 2, 2]. How many DISTINCT value triples (x, y, z) with x <= y <= z taken from the array's values sum to 0? Two index triples with the same values count once.
- A. 9
- B. 5
- C. 4
- D. 3
Correct answer: C. 4
Equal values must be skipped at each pointer so that each value triple is produced once. The distinct zero-sum value triples are [(-2, 0, 2), (-2, 1, 1), (-1, -1, 2), (-1, 0, 1)], which is 4 in total; counting index triples gives 9.
More Coding Interview Problems questions
- Two sorted arrays A = [1, 4, 7, 9] and B = [2, 3, 8, 10, 12] are merged in sorted order with two pointers. What is the 6th smallest…
- Sorted array a = [-3, -1, 0, 2, 4, 5, 9]. How many index pairs i < j have -1 <= a[i] + a[j] <= 5?
- Four towns have road distances M = [[0,10,15,20],[10,0,35,25],[15,35,0,30],[20,25,30,0]] (symmetric). A salesperson starts at town 0…
- The 4-cycle graph 0-1-2-3-0 (undirected, so both directions of every edge exist) is given as an adjacency matrix. How many ordered…
- Worker i has the cost C[i][j] of doing job j, with C = [[9,2,7,8],[6,4,3,7],[5,8,1,8],[7,6,9,4]]. Each worker takes exactly one distinct…
- How many subsets of {1, 2, 3, ..., 12} have XOR equal to 0? The empty subset counts, because its XOR is 0.