ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
import bisect
a = [10, 9, 2, 5, 3, 7, 101, 18]
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), tails)- A. 4 [2, 3, 7, 18]
- B. 4 [2, 3, 7, 101]
- C. 3 [2, 3, 7]
- D. 5 [2, 3, 7, 18, 101]
Correct answer: A. 4 [2, 3, 7, 18]
This is the O(n log n) longest increasing subsequence. tails[k] holds the smallest possible tail of an increasing run of length k+1. The final tails list has length 4, but its contents are not themselves one valid subsequence.
More Python Programming questions
- What is printed? python from itertools import product print(list(product('ab', repeat=2)), list(product([0, 1], 'x')))
- What is printed? python d = {'pen': 3, 'cap': 5, 'ink': 3, 'box': 1} print(sorted(d.items(), key=lambda kv: (-kv[1], kv[0]))[:3])
- What is printed? python from collections import deque n = 4 edges = [(0, 2), (1, 2), (2, 3), (0, 3)] indeg = [0] n adj = [[] for in…
- What is printed? python def cnt(xs, T): xs = sorted(xs) lo, hi, total = 0, len(xs) - 1, 0 while lo < hi: if xs[lo] + xs[hi] <= T: total +=…
- What is printed? python def knap(items, cap): dp = [0] (cap + 1) for w, v in items: for c in range(cap, w - 1, -1): dp[c] = max(dp[c]…
- What is printed? python import heapq h = [] for x in [4, 9, 1, 7]: heapq.heappush(h, -x) print(-heapq.heappop(h), -h[0])