3. What is the output of the following Rust code snippet calculating Levenshtein distance between "kitten" and "sitting"?
fn main() {
let s1 = "kitten";
let s2 = "sitting";
let m = s1.len();
let n = s2.len();
let mut dp = vec![vec![0; n + 1]; m + 1];
for i in 0..=m {
dp[i][0] = i;
}
for j in 0..=n {
dp[0][j] = j;
}
for i in 1..=m {
for j in 1..=n {
if s1.as_bytes()[i - 1] == s2.as_bytes()[j - 1] {
dp[i][j] = dp[i - 1][j - 1];
} else {
dp[i][j] = 1 + std::cmp::min(
dp[i - 1][j - 1],
std::cmp::min(dp[i][j - 1], dp[i - 1][j]),
);
}
}
}
println!("{}", dp[m][n]);
}
medium
A. 4
B. 3
C. 5
D. 6
Solution
Step 1: Understand the strings and DP setup
Strings are "kitten" (length 6) and "sitting" (length 7). The DP table is correctly initialized and filled using Levenshtein logic.
Step 2: Calculate Levenshtein distance manually
Transform "kitten" to "sitting" requires 3 substitutions and 1 insertion, total 4 edits.
4. Identify the bug in this Rust code snippet for Levenshtein distance:
for i in 1..m {
for j in 1..n {
if s1.as_bytes()[i - 1] == s2.as_bytes()[j - 1] {
dp[i][j] = dp[i - 1][j - 1];
} else {
dp[i][j] = 1 + std::cmp::min(
dp[i - 1][j - 1],
std::cmp::min(dp[i][j - 1], dp[i - 1][j]),
);
}
}
}
medium
A. Not initializing dp[0][j] and dp[i][0] before loops
B. Indexing s1 and s2 with i and j instead of i-1 and j-1
C. Loop ranges should be 1..=m and 1..=n to include last indices
D. Using std::cmp::max instead of std::cmp::min in calculation
Solution
Step 1: Check loop ranges for DP filling
Loops use 1..m and 1..n which exclude the last index (m and n). They should be inclusive (1..=m and 1..=n) to cover all characters.
Step 2: Verify other code parts
Indexing with i-1 and j-1 is correct, min is used properly, and initialization is assumed done outside snippet.
Final Answer:
Loop ranges should be 1..=m and 1..=n to include last indices -> Option C
Quick Check:
Inclusive loops needed for full DP table [OK]
Hint: Use inclusive ranges 1..=m and 1..=n for full coverage [OK]
Common Mistakes:
Using exclusive ranges missing last character
Incorrect indexing causing panic
Skipping DP initialization
5. Given two strings s1 = "intention" and s2 = "execution", what is the Levenshtein distance computed by the Rust algorithm?
Use the standard operations: insert, delete, substitute with cost 1 each.
hard
A. 7
B. 8
C. 5
D. 6
Solution
Step 1: Analyze the strings and possible edits
"intention" (i n t e n t i o n) to "execution" (e x e c u t i o n). Minimal edits: substitute i->e, n->x, t->e, e->c, n->u, plus one insertion or deletion needed, total 6 edits.
Step 2: Confirm with Levenshtein algorithm logic
The DP computes the minimum cost as 6.
Final Answer:
6 -> Option D
Quick Check:
Levenshtein("intention", "execution") = 6 [OK]
Hint: Count minimal edits stepwise between strings [OK]
Common Mistakes:
Ignoring insertions or deletions
Miscounting substitutions
Assuming equal length means zero edits
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.