Data Structures & AlgorithmsHardProduct Company SDE · CP practice

Problem: What is the length of the longest subsequence of [3, 1, 4, 1, 5, 9, 2, 6] whose elements are strictly increasing? Run the code.

Problem: What is the length of the longest subsequence of [3, 1, 4, 1, 5, 9, 2, 6] whose elements are strictly increasing? Run the code.
a = [3, 1, 4, 1, 5, 9, 2, 6]
L = [1] * len(a)
for i in range(len(a)):
    for j in range(i):
        if a[j] < a[i]:
            L[i] = max(L[i], L[j] + 1)
print(max(L))
What is printed?
  1. A. 5
  2. B. 3
  3. C. 6
  4. D. 4

Correct answer: D. 4

A strictly increasing subsequence 1, 4, 5, 9 has length 4, and no longer one exists.

More Coding Interview Problems questions

Exams that ask this topic