Challenge - 5 Problems
Levenshtein Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Levenshtein Distance Calculation
What is the output of the following Rust code that calculates the Levenshtein distance between two strings?
DSA Rust
fn levenshtein(a: &str, b: &str) -> usize { let mut costs = vec![0; b.len() + 1]; for j in 0..=b.len() { costs[j] = j; } for (i, ca) in a.chars().enumerate() { let mut last_cost = i; costs[0] = i + 1; for (j, cb) in b.chars().enumerate() { let new_cost = if ca == cb { last_cost } else { 1 + last_cost.min(costs[j]).min(costs[j + 1]) }; last_cost = costs[j + 1]; costs[j + 1] = new_cost; } } costs[b.len()] } fn main() { let dist = levenshtein("kitten", "sitting"); println!("{}", dist); }
Attempts:
2 left
💡 Hint
Think about the minimum number of edits (insertions, deletions, substitutions) to convert 'kitten' to 'sitting'.
✗ Incorrect
The Levenshtein distance between "kitten" and "sitting" is 3. The edits are: substitute 'k' with 's', substitute 'e' with 'i', and insert 'g' at the end.
🧠 Conceptual
intermediateUnderstanding Levenshtein Distance Operations
Which of the following operations are considered when calculating the Levenshtein distance between two strings?
Attempts:
2 left
💡 Hint
Levenshtein distance counts the minimum number of single-character edits.
✗ Incorrect
Levenshtein distance counts the minimum number of insertions, deletions, and substitutions needed to change one string into another.
🔧 Debug
advancedIdentify the Error in Levenshtein Distance Implementation
What error will the following Rust code produce when calculating the Levenshtein distance?
DSA Rust
fn levenshtein(a: &str, b: &str) -> usize { let mut costs = vec![0; b.len()]; for j in 0..=b.len() { costs[j] = j; } for (i, ca) in a.chars().enumerate() { let mut last_cost = i; costs[0] = i + 1; for (j, cb) in b.chars().enumerate() { let new_cost = if ca == cb { last_cost } else { 1 + last_cost.min(costs[j]).min(costs[j + 1]) }; last_cost = costs[j + 1]; costs[j + 1] = new_cost; } } costs[b.len()] } fn main() { let dist = levenshtein("abc", "yabd"); println!("{}", dist); }
Attempts:
2 left
💡 Hint
Check the size of the vector and the indexing inside the loop.
✗ Incorrect
The vector 'costs' is initialized with length b.len(), but the code accesses costs[b.len()] which is out of bounds, causing a runtime panic.
📝 Syntax
advancedCorrect the Syntax Error in Levenshtein Distance Code
Which option fixes the syntax error in this Rust code snippet for Levenshtein distance?
DSA Rust
let new_cost = if ca == cb { last_cost } else 1 + last_cost.min(costs[j]).min(costs[j + 1]) };
Attempts:
2 left
💡 Hint
In Rust, multi-line else blocks need braces.
✗ Incorrect
The else block spans multiple lines and must be enclosed in braces to be syntactically correct.
🚀 Application
expertMinimum Edit Distance for Real-World Spell Correction
Given the words "intention" and "execution", what is the Levenshtein distance calculated by the Rust function below?
DSA Rust
fn levenshtein(a: &str, b: &str) -> usize { let mut costs = vec![0; b.len() + 1]; for j in 0..=b.len() { costs[j] = j; } for (i, ca) in a.chars().enumerate() { let mut last_cost = i; costs[0] = i + 1; for (j, cb) in b.chars().enumerate() { let new_cost = if ca == cb { last_cost } else { 1 + last_cost.min(costs[j]).min(costs[j + 1]) }; last_cost = costs[j + 1]; costs[j + 1] = new_cost; } } costs[b.len()] } fn main() { let dist = levenshtein("intention", "execution"); println!("{}", dist); }
Attempts:
2 left
💡 Hint
Count the minimum edits: substitutions, insertions, deletions to convert "intention" to "execution".
✗ Incorrect
The Levenshtein distance between "intention" and "execution" is 5, representing the minimum edits needed.
