Data Structures & AlgorithmsHardProduct Company SDE · DP round
Problem: What is the Levenshtein distance between 'horse' and 'ros'? Run the DP.
Problem: What is the Levenshtein distance between 'horse' and 'ros'? Run the DP.
a, b = 'horse', 'ros'
dp = [[0] * (len(b) + 1) for _ in range(len(a) + 1)]
for i in range(len(a) + 1):
dp[i][0] = i
for j in range(len(b) + 1):
dp[0][j] = j
for i in range(1, len(a) + 1):
for j in range(1, len(b) + 1):
dp[i][j] = dp[i - 1][j - 1] if a[i - 1] == b[j - 1] else 1 + min(dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1])
print(dp[-1][-1])What is printed?- A. 2
- B. 3
- C. 4
- D. 5
Correct answer: B. 3
Replace h with r (rorse), delete the extra r (rose), then delete e (ros): 3 edits, and the DP confirms no 2-edit route exists.
More Coding Interview Problems questions
- Problem: What is the sum of |ai - aj| over all pairs i < j of the sorted array [1, 4, 6]? Run the code. python a = sorted([1, 4, 6])…
- Problem: Which complexity does the sieve of Eratosthenes have for finding all primes up to n?
- Problem: How many 3-digit numbers are palindromes? python print(sum(1 for x in range(100, 1000) if str(x) == str(x)[::-1])) What is printed?
- Problem: How many integers in [1, 200] are divisible by 2, 3 or 5? python print(sum(1 for x in range(1, 201) if x % 2 == 0 or x % 3 == 0…
- Problem: How many subsets of {1, 2, 3, 4, 5, 6} have sum exactly 10? python print(sum(1 for m in range(1 << 6) if sum(i + 1 for i in…
- Problem: What is the sum of the decimal digits of 9^9? python print(sum(int(c) for c in str(9 9))) What is printed?