Question 31
MediumWhat is the output of this code?
x, y = "abcde", "ace"
dp = [[0] * ((y) + ) _ ((x) + )]
i (, (x) + ):
j (, (y) + ):
dp[i][j] = dp[i-][j-] + x[i-] == y[j-] (dp[i-][j], dp[i][j-])
(dp[-][-])
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Data Structures and Algorithms
What is the output of this code?
x, y = "abcde", "ace"
dp = [[0] * ((y) + ) _ ((x) + )]
i (, (x) + ):
j (, (y) + ):
dp[i][j] = dp[i-][j-] + x[i-] == y[j-] (dp[i-][j], dp[i][j-])
(dp[-][-])
3
4
5
2
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
a = [2, 7, 9, 3, 1]
prev = cur = 0
for x in a:
prev, cur = cur, max(cur, prev + x)
print(cur)
11
12
Sign up free to see the answer and a step-by-step explanation.
What is the time complexity of the standard 0/1 knapsack DP with n items and capacity W?
O(n × W)
O(n log n)
O(W)
O(2ⁿ)
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
r, c = 3, 3
dp = [[1] * c for _ in range(r)]
for i in range(1, r):
for j in range(1, c):
dp[i][j] = dp[i-1][j] + dp[i][j-1]
print(dp[-1][-1])
4
3
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
def lis(a):
d = [1] * len(a)
for i in range(len(a)):
for j in range(i):
if a[j] < a[i]:
d[i] = max(d[i], d[j] + 1)
return max(d)
print(lis([10, 9, 2, 5, 3, 7, 101, 18]))
3
8
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
def lcs(a, b):
d = [[0] * (len(b) + 1) for _ in range(len(a) + 1)]
for i in range(1, len(a) + 1):
for j in range(1, len(b) + 1):
d[i][j] = d[i-1][j-1] + 1 if a[i-1] == b[j-1] else max(d[i-1][j], d[i][j-1])
return d[-1][-1]
print(lcs("AGGTAB", "GXTXAYB"))
6
3
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
def ways(n, coins):
d = [1] + [0] * n
for c in coins:
for s in range(c, n + 1):
d[s] += d[s - c]
return d[n]
print(ways(5, [1, 2, 5]))
6
4
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
def min_coins(n, coins):
INF = float("inf")
d = [0] + [INF] * n
for s in range(1, n + 1):
for c in coins:
if c <= s:
d[s] = min(d[s], d[s - c] + 1)
return d[n]
print(min_coins(11, [1, 5, 6, 9]))
4
2
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
def knap(W, wt, val):
d = [0] * (W + 1)
for w, v in zip(wt, val):
for c in range(W, w - 1, -1):
d[c] = max(d[c], d[c - w] + v)
return d[W]
print(knap(7, [1, 3, 4, 5], [1, 4, 5, 7]))
10
12
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
def climb(n):
a, b = 1, 1
for _ in range(n):
a, b = b, a + b
return a
print(climb(10))
55
89
Sign up free to see the answer and a step-by-step explanation.
10
15
6
10
5
4
4
5
3
5
1
3
8
9
144
10