Data Structures & AlgorithmsHardProduct Company SDE · CP practice
Problem: What is the length of the longest common subsequence of 'abcdef' and 'acebdf'? Run the DP.
Problem: What is the length of the longest common subsequence of 'abcdef' and 'acebdf'? Run the DP.
a, b = 'abcdef', 'acebdf'
dp = [[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):
dp[i][j] = dp[i - 1][j - 1] + 1 if a[i - 1] == b[j - 1] else max(dp[i - 1][j], dp[i][j - 1])
print(dp[-1][-1])What is printed?- A. 4
- B. 5
- C. 3
- D. 6
Correct answer: A. 4
The common subsequence 'abdf' has length 4, and the DP finds the maximum.
More Coding Interview Problems questions
- Problem: What is the number of subsets of {1, 2, ..., 8} whose elements sum to a multiple of 8? Run the code. python print(sum(1 for m in…
- Problem: How many pairs (i < j) of [1, 4, 9, 16, 25] have a[j] - a[i] divisible by 3? Run the code. python a = [1, 4, 9, 16, 25]…
- Problem: What is the number of set bits in 2^30 - 1? Run the code. python print(bin(2 30 - 1).count('1')) What is printed?
- Problem: How many integers in [1, 300] are multiples of 6 but not of 9? Run the code. python print(sum(1 for x in range(1, 301) if x % 6…
- Problem: How many ordered triples (a, b, c) of positive integers satisfy a + b + c = 7? Run the code. python print(sum(1 for a in range(1…
- Problem: What is the largest possible value of gcd(a, b) when a + b = 60 and a != b with a, b positive? Run the code. python from math…