Data Structures & AlgorithmsModerateTCS NQT · Infosys
Problem: The array [3, 7, 1, 9] is used to create all non-empty contiguous subarrays. How many of them have an odd sum?
- A. 10
- B. 4
- C. 2
- D. 6
Correct answer: D. 6
All values are odd, so a sum is odd iff the length is odd: 4 subarrays of length 1 and 2 of length 3 give 6. 10 counts all subarrays, 4 counts the even sums, and 2 counts only length 3.
More Coding Interview Problems questions
- Problem: In the array [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], find the number of pairs (i, j) with i < j whose product is a multiple of 12.
- Problem: How many 5-digit numbers (10000–99999) have digits that sum to exactly 6 and begin with the digit 1?
- Problem: A string of 6 characters is formed from the letters {a, b} such that no two adjacent characters are equal. How many such strings…
- Problem: The Euclidean distance squared between (1,2) and (4,6) is what integer?
- Problem: In the sequence 2, 3, 5, 8, 12, 17, … each term increases by one more than the previous increase (differences 1, 2, 3, 4, …)…
- Problem: How many 4-element subsets of {1, 2, ..., 8} have a sum that is a multiple of 5?