Data Structures & AlgorithmsHardProduct company OA · Amazon/Microsoft-style OA
Sorted list [1, 2, 4, 6, 7, 9, 11]. How many index pairs i < j satisfy vals[i] + vals[j] <= 10?
- A. 12
- B. 9
- C. 7
- D. 18
Correct answer: B. 9
Two pointers from both ends: when vals[l] + vals[r] ≤ 10, all r − l pairs with left end l qualify, giving 9 in total. Using < 10 misses the boundary pairs 1 + 9 and 4 + 6 (7), letting j start at i adds 1+1, 2+2, 4+4 (12), and counting ordered pairs gives 18.
More Coding Interview Problems questions
- Four tasks each have (time, value): (3,2), (2,5), (4,4), (5,3). Choose any subset of tasks with total time at most 9 to maximise total…
- In how many ways can a set of 6 distinct people be partitioned into groups so that every group has an even number of people (groups are…
- From the values [3, 5, 6, 7, 10], choose any non-empty subset and XOR its elements together. What is the largest value that can be obtained?
- Four groups are G1 = {1, 4}, G2 = {2, 9}, G3 = {3, 7} and G4 = {5, 6}. Choosing exactly one element from each group, how many choices sum…
- Using each of the values 1, 2, 5, 9 and 20 at most once, which is the smallest positive integer that CANNOT be written as the sum of some…
- Half-open segments [1,4), [3,7), [6,10), [9,12), [2,5), [8,11) are given. Choose pairwise disjoint segments to maximise the total length…