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?
  1. A. 4
  2. B. 3
  3. C. 2
  4. 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

Exams that ask this topic