Data Structures & AlgorithmsHardProduct Company SDE · CP practice
Problem: What is the length of the longest subsequence of [3, 1, 4, 1, 5, 9, 2, 6] whose elements are strictly increasing? Run the code.
Problem: What is the length of the longest subsequence of [3, 1, 4, 1, 5, 9, 2, 6] whose elements are strictly increasing? Run the code.
a = [3, 1, 4, 1, 5, 9, 2, 6]
L = [1] * len(a)
for i in range(len(a)):
for j in range(i):
if a[j] < a[i]:
L[i] = max(L[i], L[j] + 1)
print(max(L))What is printed?- A. 5
- B. 3
- C. 6
- D. 4
Correct answer: D. 4
A strictly increasing subsequence 1, 4, 5, 9 has length 4, and no longer one exists.
More Coding Interview Problems questions
- Problem: What is the number of trailing zeros in 30!? Run the code. python n = 30 c = 0 p = 5 while p <= n: c += n // p p = 5 print(c)…
- Problem: How many integers in [1, 500] are perfect squares or multiples of 7? Run the code. python print(sum(1 for x in range(1, 501) if…
- Problem: What is the minimum number of operations to make all elements of [4, 7, 1] equal to their minimum by decrementing? Run the code…
- Problem: What is the number of paths from (1,1) to (3,3) moving only right or up in a grid with all cells free? Run the DP. python g =…
- Problem: How many distinct values does a XOR b take for a, b in {0, 1, 2, 3}? Run the code. python print(len({a ^ b for a in range(4) for…
- Problem: What is the sum of all integers in [1, 200] that are both odd and multiples of 5? Run the code. python print(sum(x for x in…