Data Structures & AlgorithmsModerateTCS NQT · Infosys
Array a = [2, -2, 3, -3, 4, -4, 1]. How many DISTINCT values appear among the prefix sums P[0], P[1], ..., P[7] (including P[0] = 0)?
- A. 8
- B. 5
- C. 7
- D. 4
Correct answer: B. 5
Compute the eight prefix values 0, 2, 0, 3, 0, 4, 0, 1. Several prefixes repeat the value 0, so the number of distinct values is smaller than the number of prefixes.
More Coding Interview Problems questions
- Sorted arrays A = [3, 8, 12, 17, 25] and B = [5, 9, 14, 20]. Choose one element from A and one from B so that the sum is as large as…
- Balloons span closed integer intervals [1,4], [2,3], [4,6], [5,8], [7,9]. One arrow shot at integer x bursts every balloon whose interval…
- How many binary strings of length 6 contain neither two consecutive 0s nor three consecutive 1s?
- How many subsets (including the empty subset) of [3, 5, 9, 11, 14, 2] have a sum divisible by 7?
- Closed intervals [1,4], [2,6], [4,7], [5,9], [8,10] are given. What is the largest number of intervals that share a single integer point?
- Array [2, 8, 9, 18, 4, 16, 6, 3]. How many index pairs i < j have a[i] a[j] equal to a perfect square?