Data Structures & AlgorithmsModerateTCS NQT · Infosys
Array [3, 7, 2, 9, 4]. How many contiguous subarrays sum to exactly 9?
- A. 3
- B. 1
- C. 2
- D. 5
Correct answer: C. 2
Accumulate from each start index; the matches are [9] and [7, 2], so the answer is 2. Counting only elements equal to 9 misses the window [7, 2].
More Coding Interview Problems questions
- Problem: Fresh oranges (1) rot their neighbours (4-directional) one minute at a time; 0 is empty and 2 is rotten. How many minutes until…
- Problem: How many distinct palindromic substrings does 'aaaa' have? Run the code. python s = 'aaaa' print(len({s[i:j] for i in…
- Problem: Count the start indices i where a permutation of 'abc' begins in s = 'cbaebabacd' (the anagram-window problem). Run the code…
- Problem: What is the length of the shortest substring of 'aabbcxa' that contains every letter of 'abc'? Run the sliding-window code…
- Problem: What is the length of the longest substring of 'banana' that occurs at least twice (occurrences may overlap)? Run the code…
- Problem: Subset sums of items [1, 2, 3] are computed with a bitset: reach starts as 1 and each item x does reach |= reach << x. How many…