Data Structures & AlgorithmsHardProduct Company SDE · CP practice
Problem: How many subarrays of [2, 4, 1, 3, 6, 5] have an even sum? Run the code.
Problem: How many subarrays of [2, 4, 1, 3, 6, 5] have an even sum? Run the code.
a = [2, 4, 1, 3, 6, 5] print(sum(1 for i in range(len(a)) for j in range(i + 1, len(a) + 1) if sum(a[i:j]) % 2 == 0))What is printed?
- A. 11
- B. 10
- C. 12
- D. 21
Correct answer: A. 11
Prefix parities 0, 0, 0, 1, 0, 0, 1 hold five 0s and two 1s, so C(5,2) + C(2,2) = 11 even-sum subarrays. Odd sums number 10, matching only the endpoint parities gives 12, and 21 counts all subarrays.
More Coding Interview Problems questions
- Problem: How many integers x in [1, 999] satisfy x ≡ 2 (mod 3) and x ≡ 5 (mod 7)? Run the code. python print(sum(1 for x in range(1, 1000)…
- Problem: How many squares (k k with k >= 1) are at most 10^6? Run the code. python print(sum(1 for k in range(1, 10 6) if k k <= 10 6))…
- Problem: What is the maximum value of x (100 - x) over integers 0 <= x <= 100? Run the code. python print(max(x (100 - x) for x in…
- Problem: What is the number of trailing zeros in 100! expressed in base 6 (count of factors of 6)? Run the code. python n = 100 c2 = c3 =…
- Problem: How many ways can you pick 2 different integers from [1, 20] whose sum is divisible by 4? Run the code. python print(sum(1 for i…
- Problem: What is the count of 3-digit numbers whose digits are all distinct and in strictly increasing order? Run the code. python…