Data Structures & AlgorithmsHardProduct Company SDE · CP practice

Problem: What is the length of the longest increasing subsequence of [3, 10, 2, 1, 20]?

Problem: What is the length of the longest increasing subsequence of [3, 10, 2, 1, 20]?
import bisect
a = [3, 10, 2, 1, 20]
tails = []
for x in a:
    i = bisect.bisect_left(tails, x)
    if i == len(tails):
        tails.append(x)
    else:
        tails[i] = x
print(len(tails))
What is printed?
  1. A. 3
  2. B. 4
  3. C. 2
  4. D. 5

Correct answer: A. 3

The tails array reaches length 3 with the subsequence 3, 10, 20, and no longer increasing subsequence exists.

More Coding Interview Problems questions

Exams that ask this topic