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 C++ code that calculates the Levenshtein distance between two strings?
DSA C++
#include <iostream> #include <vector> #include <string> #include <algorithm> int levenshteinDistance(const std::string& s1, const std::string& s2) { int m = s1.size(); int n = s2.size(); std::vector<std::vector<int>> dp(m + 1, std::vector<int>(n + 1)); for (int i = 0; i <= m; ++i) dp[i][0] = i; for (int j = 0; j <= n; ++j) dp[0][j] = j; for (int i = 1; i <= m; ++i) { for (int j = 1; j <= n; ++j) { if (s1[i - 1] == s2[j - 1]) { dp[i][j] = dp[i - 1][j - 1]; } else { dp[i][j] = 1 + std::min({dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]}); } } } return dp[m][n]; } int main() { std::string a = "kitten"; std::string b = "sitting"; std::cout << levenshteinDistance(a, b) << std::endl; return 0; }
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:
- Substitute 'k' with 's'
- Substitute 'e' with 'i'
- 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 edits needed using three basic operations.
✗ Incorrect
Levenshtein distance uses insertion, deletion, and substitution operations only. Transposition is not included.
🔧 Debug
advancedIdentify the Bug in Levenshtein Distance Code
What error will the following C++ code produce when calculating Levenshtein distance?
DSA C++
#include <iostream> #include <vector> #include <string> #include <algorithm> int levenshteinDistance(const std::string& s1, const std::string& s2) { int m = s1.size(); int n = s2.size(); std::vector<std::vector<int>> dp(m, std::vector<int>(n)); for (int i = 0; i <= m; ++i) dp[i][0] = i; for (int j = 0; j <= n; ++j) dp[0][j] = j; for (int i = 1; i <= m; ++i) { for (int j = 1; j <= n; ++j) { if (s1[i - 1] == s2[j - 1]) { dp[i][j] = dp[i - 1][j - 1]; } else { dp[i][j] = 1 + std::min({dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]}); } } } return dp[m][n]; } int main() { std::cout << levenshteinDistance("abc", "yabd") << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Check the size of the dp vector and the loop boundaries.
✗ Incorrect
The dp vector is initialized with size m x n, but loops run up to m and n inclusive, causing out-of-range access.
❓ Predict Output
advancedLevenshtein Distance Matrix Final State
What is the final state of the dp matrix after running the Levenshtein distance algorithm on strings "abc" and "yabd"?
DSA C++
#include <iostream> #include <vector> #include <string> #include <algorithm> int main() { std::string s1 = "abc"; std::string s2 = "yabd"; int m = s1.size(); int n = s2.size(); std::vector<std::vector<int>> dp(m + 1, std::vector<int>(n + 1)); for (int i = 0; i <= m; ++i) dp[i][0] = i; for (int j = 0; j <= n; ++j) dp[0][j] = j; for (int i = 1; i <= m; ++i) { for (int j = 1; j <= n; ++j) { if (s1[i - 1] == s2[j - 1]) { dp[i][j] = dp[i - 1][j - 1]; } else { dp[i][j] = 1 + std::min({dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]}); } } } for (int i = 0; i <= m; ++i) { for (int j = 0; j <= n; ++j) { std::cout << dp[i][j] << " "; } std::cout << std::endl; } return 0; }
Attempts:
2 left
💡 Hint
Trace the matrix row by row considering matches and minimum edits.
✗ Incorrect
The dp matrix shows the minimum edit distances for substrings. The final matrix is:
0 1 2 3 4 1 1 1 2 3 2 2 2 1 2 3 3 3 2 2
🚀 Application
expertMinimum Edit Distance for Large Strings
Given two strings each of length 1000, what is the time complexity of the classic dynamic programming solution to compute the Levenshtein distance?
Attempts:
2 left
💡 Hint
Consider the size of the dp matrix and the nested loops.
✗ Incorrect
The classic Levenshtein distance algorithm uses a matrix of size n x n and fills it with nested loops, resulting in O(n^2) time complexity.
