ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
import bisect
def lis(a):
    t = []
    for x in a:
        i = bisect.bisect_left(t, x)
        if i == len(t):
            t.append(x)
        else:
            t[i] = x
    return len(t)
print(lis([3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]))
  1. A. 5
  2. B. 3
  3. C. 4
  4. D. 6

Correct answer: C. 4

The list t holds the smallest possible tail for each strictly increasing length, and bisect_left finds where each value goes. Its final length 4 equals the longest strictly increasing subsequence, such as 1, 4, 5, 9.

More Python Programming questions

Exams that ask this topic