Data Structures & AlgorithmsModerateTCS NQT · basics

Problem: What is the length of the longest strictly increasing contiguous run in [1, 3, 5, 4, 7, 8, 2]? Run the code.

Problem: What is the length of the longest strictly increasing contiguous run in [1, 3, 5, 4, 7, 8, 2]? Run the code.
a = [1, 3, 5, 4, 7, 8, 2]
best = cur = 1
for i in range(1, len(a)):
    cur = cur + 1 if a[i] > a[i - 1] else 1
    best = max(best, cur)
print(best)
What is printed?
  1. A. 4
  2. B. 2
  3. C. 3
  4. D. 5

Correct answer: C. 3

The runs 1,3,5 and 4,7,8 both have length 3. Subsequences would allow longer answers, but the question asks for contiguous runs.

More Coding Interview Problems questions

Exams that ask this topic