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?- A. 3
- B. 4
- C. 2
- 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
- Problem: How many distinct values does floor(30 / i) take for i = 1 to 30? python print(len({30 // i for i in range(1, 31)})) What is…
- Problem: How many steps does Euclid's algorithm take on (2024, 748) before the remainder becomes 0? python a, b = 2024, 748 steps = 0…
- Problem: What is the sum of the divisors of 28 (a perfect number)? python print(sum(d for d in range(1, 29) if 28 % d == 0)) What is…
- Problem: How many pairs (i < j) of [1, 2, 3, 4, 5, 7] have an odd product? python a = [1, 2, 3, 4, 5, 7] print(sum(1 for i in…
- Problem: What is the total number of set bits in the binary representations of 1 through 100? python print(sum(bin(i).count('1') for i in…
- Problem: What is the minimum number of jumps needed to reach the last index of [1, 2, 1, 1, 1] when a[i] is the maximum jump length from…