Data Structures & AlgorithmsVery hardProduct company OA · Amazon/Microsoft-style OA
Sorted array a = [-3, -1, 0, 2, 4, 5, 9]. How many index pairs i < j have -1 <= a[i] + a[j] <= 5?
- A. 10
- B. 8
- C. 7
- D. 12
Correct answer: A. 10
Count pairs with sum ≤ 5 (12) and subtract those with sum ≤ −2 (2), each with a linear two-pointer pass: 12 − 2 = 10. A strict lower bound loses the two pairs summing to exactly −1 (8), making both bounds strict also loses 0 + 5 (7), and ignoring the lower bound gives 12.
More Coding Interview Problems questions
- 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.
- How many binary strings of length 10 contain no two adjacent 1s?
- In how many ways can a 3 x 4 grid be covered completely by non-overlapping 1 x 2 dominoes (each domino covers two adjacent cells, in…