Data Structures & AlgorithmsHardGATE CS · TCS NQT

The function below computes the length of a longest increasing subsequence.

The function below computes the length of a longest increasing subsequence.
import bisect
a = [4, 4, 1, 5, 5, 6, 2, 7]
t = []
for x in a:
    i = bisect.bisect_left(t, x)
    if i == len(t): t.append(x)
    else: t[i] = x
print(len(t))
What is printed?
  1. A. 4
  2. B. 6
  3. C. 8
  4. D. 5

Correct answer: A. 4

bisect_left places a new value before equal entries, so repeated values replace rather than extend the tails and the length is the strict LIS. bisect_right lets equal values extend the chain. Counting distinct values is a different quantity.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic