Data Structures & AlgorithmsModerateProduct Company SDE · CP practice
Problem: What is the length of the longest subarray of [2, 4, 6, 3, 8, 10, 12] whose elements are all even? Run the code.
Problem: What is the length of the longest subarray of [2, 4, 6, 3, 8, 10, 12] whose elements are all even? Run the code.
a = [2, 4, 6, 3, 8, 10, 12]
best = cur = 0
for x in a:
cur = cur + 1 if x % 2 == 0 else 0
best = max(best, cur)
print(best)What is printed?- A. 4
- B. 3
- C. 2
- D. 5
Correct answer: B. 3
The run resets on the odd value 3, and the longest run of even values is the answer.
More Coding Interview Problems questions
- Problem: How many distinct values does floor(100 / i) take for i = 1 to 100? Run the code. python print(len({100 // i for i in range(1…
- Problem: What is the number of ways to write 20 as an ordered sum of the parts 1 and 2 (compositions)? Run the DP. python f = [1, 1] for i…
- Problem: How many 4-digit numbers have all digits equal to or greater than the digit before them (non-decreasing)? Run the code. python…
- Problem: What is the maximum value of a[i] XOR a[j] over i < j for a = [3, 10, 5, 25, 2, 8, 17]? Run the code. python a = [3, 10, 5, 25…
- Problem: How many subarrays of [1, -1, 1, -1, 1] have sum 0? Run the prefix-count code. python from collections import Counter a = [1, -1…
- Problem: What is the number of paths from (0,0) to (4,4) using right and up moves that never touch the diagonal cells (i, i) except at the…