Bird
Raised Fist0
DSA Goprogramming~10 mins

Edit Distance Problem Levenshtein in DSA Go - Interactive Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to initialize the 2D slice for dynamic programming.

DSA Go
dp := make([][]int, len(s1)+1)
for i := range dp {
    dp[i] = make([]int, [1])
}
Drag options to blanks, or click blank then click option'
Alen(s1)
Blen(s1)+1
Clen(s2)
Dlen(s2)+1
Attempts:
3 left
💡 Hint
Common Mistakes
Using len(s1)+1 for inner slice length causes index errors.
Forgetting the +1 for empty string cases.
2fill in blank
medium

Complete the code to fill the first row of the dp table.

DSA Go
for j := 0; j <= len(s2); j++ {
    dp[0][j] = [1]
}
Drag options to blanks, or click blank then click option'
Aj
B0
Clen(s1)
Dlen(s2)
Attempts:
3 left
💡 Hint
Common Mistakes
Setting dp[0][j] to 0 causes wrong base case.
Using len(s2) instead of j.
3fill in blank
hard

Fix the error in the condition to check if characters are equal.

DSA Go
if s1[i-1] [1] s2[j-1] {
    dp[i][j] = dp[i-1][j-1]
} else {
    dp[i][j] = min(dp[i-1][j]+1, dp[i][j-1]+1, dp[i-1][j-1]+1)
}
Drag options to blanks, or click blank then click option'
A=
B==
C!=
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using single '=' causes syntax error.
Using '!=' reverses logic.
4fill in blank
hard

Fill both blanks to complete the min function call for edit operations.

DSA Go
dp[i][j] = min(dp[i-1][j][1]1, dp[i][j-1][2]1, dp[i-1][j-1]+1)
Drag options to blanks, or click blank then click option'
A+
B-
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' subtracts cost incorrectly.
Using '*' or '/' causes wrong calculations.
5fill in blank
hard

Fill all three blanks to complete the min function that returns the smallest of three integers.

DSA Go
func min(a, b, c int) int {
    if a [1] b && a [2] c {
        return a
    } else if b [3] c {
        return b
    }
    return c
}
Drag options to blanks, or click blank then click option'
A<=
B<
C>
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' returns maximum instead of minimum.
Using '>=' or '<=' can cause incorrect ties.

Practice

(1/5)
1. What does the Edit Distance (Levenshtein distance) between two strings measure?
easy
A. The number of matching characters in the same positions
B. The number of characters in the longer string
C. The minimum number of single-character edits to change one string into the other
D. The length difference between the two strings

Solution

  1. Step 1: Understand the definition of Edit Distance

    Edit Distance counts how many changes (insert, delete, replace) are needed to convert one string to another.
  2. Step 2: Identify what is measured

    It measures the minimum number of single-character edits required.
  3. Final Answer:

    The minimum number of single-character edits to change one string into the other -> Option C
  4. Quick Check:

    Edit Distance = minimum edits [OK]
Hint: Edit Distance counts minimal edits to match strings [OK]
Common Mistakes:
  • Confusing edit distance with string length
  • Thinking it counts only matching characters
  • Assuming it measures length difference only
2. Which of the following Go function signatures correctly represents a function to compute Levenshtein distance between two strings s1 and s2?
easy
A. func Levenshtein(s1, s2 string) string
B. func Levenshtein(s1, s2 string) int
C. func Levenshtein(s1 int, s2 int) string
D. func Levenshtein(s1, s2 int) int

Solution

  1. Step 1: Identify input types for strings

    The function must accept two strings as input parameters.
  2. Step 2: Identify output type

    The output is an integer representing the edit distance.
  3. Final Answer:

    func Levenshtein(s1, s2 string) int -> Option B
  4. Quick Check:

    Input: strings, Output: int [OK]
Hint: Function inputs are strings, output is int distance [OK]
Common Mistakes:
  • Using int instead of string for inputs
  • Returning string instead of int
  • Mixing input types incorrectly
3. What is the output of the following Go code snippet computing Levenshtein distance between "kitten" and "sitting"?
package main
import "fmt"
func main() {
    s1 := "kitten"
    s2 := "sitting"
    dist := Levenshtein(s1, s2)
    fmt.Println(dist)
}

func Levenshtein(a, b string) int {
    m, n := len(a), len(b)
    dp := make([][]int, m+1)
    for i := range dp {
        dp[i] = make([]int, n+1)
    }
    for i := 0; i <= m; i++ {
        dp[i][0] = i
    }
    for j := 0; j <= n; j++ {
        dp[0][j] = j
    }
    for i := 1; i <= m; i++ {
        for j := 1; j <= n; j++ {
            cost := 0
            if a[i-1] != b[j-1] {
                cost = 1
            }
            dp[i][j] = min(dp[i-1][j]+1, dp[i][j-1]+1, dp[i-1][j-1]+cost)
        }
    }
    return dp[m][n]
}

func min(x, y, z int) int {
    if x < y {
        if x < z {
            return x
        }
        return z
    }
    if y < z {
        return y
    }
    return z
}
medium
A. 6
B. 4
C. 5
D. 3

Solution

  1. Step 1: Understand the strings and operations

    "kitten" to "sitting" requires substitutions k->s, e->i, plus insert g.
  2. Step 2: Calculate minimum edits

    Minimum edits are 2 substitutions + 1 insertion = 3 edits total.
  3. Final Answer:

    3 -> Option D
  4. Quick Check:

    Levenshtein("kitten", "sitting") = 3 [OK]
Hint: Count substitutions and insertions carefully [OK]
Common Mistakes:
  • Counting only substitutions
  • Ignoring insertions
  • Miscounting character positions
4. Identify the bug in this Go function computing Levenshtein distance:
func Levenshtein(a, b string) int {
    m, n := len(a), len(b)
    dp := make([][]int, m)
    for i := range dp {
        dp[i] = make([]int, n)
    }
    for i := 0; i < m; i++ {
        dp[i][0] = i
    }
    for j := 0; j < n; j++ {
        dp[0][j] = j
    }
    for i := 1; i < m; i++ {
        for j := 1; j < n; j++ {
            cost := 0
            if a[i-1] != b[j-1] {
                cost = 1
            }
            dp[i][j] = min(dp[i-1][j]+1, dp[i][j-1]+1, dp[i-1][j-1]+cost)
        }
    }
    return dp[m-1][n-1]
}
medium
A. dp slice sizes should be m+1 and n+1, not m and n
B. The cost calculation should be outside the loops
C. The min function is missing
D. The return statement should be dp[0][0]

Solution

  1. Step 1: Check dp slice initialization

    dp must have size m+1 by n+1 to include empty string cases, but code uses m and n only.
  2. Step 2: Understand impact of wrong sizes

    Using m and n causes index out of range errors and incorrect base cases.
  3. Final Answer:

    dp slice sizes should be m+1 and n+1, not m and n -> Option A
  4. Quick Check:

    dp size = m+1, n+1 [OK]
Hint: dp array must cover empty prefixes with +1 size [OK]
Common Mistakes:
  • Initializing dp with wrong sizes
  • Misplacing cost calculation
  • Returning wrong dp cell
5. Given two strings s1 = "intention" and s2 = "execution", what is the minimum edit distance using Levenshtein algorithm? Consider insertions, deletions, and substitutions only.
hard
A. 6
B. 5
C. 8
D. 7

Solution

  1. Step 1: Analyze differences between "intention" and "execution"

    Compare characters and count edits: substitutions and insertions/deletions as needed.
  2. Step 2: Calculate minimal edits

    Minimum edits calculated are 6 total.
  3. Final Answer:

    6 -> Option A
  4. Quick Check:

    Levenshtein("intention", "execution") = 6 [OK]
Hint: Count substitutions plus insertions/deletions carefully [OK]
Common Mistakes:
  • Underestimating substitutions
  • Ignoring insertions/deletions
  • Confusing character positions
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard