Facebook Interview Question: Edit distance of words and di... | Glassdoor

Interview Question

Software Development Engineer Interview

Edit distance of words and distributed system questions.


Interview Answer

1 Answer


(edit distance of words)

public static int editDistance(String a, String b)
    if (null == a) return -1;
    if (null == b) return -1;
    if (a.equals("")) return b.length();
    if (b.equals("")) return a.length();
    int editDistance = a.length() + b.length();
    if (a.substring(0, 1).equals(b.substring(0, 1)))
        editDistance = editDistance(a.substring(1, a.length()), b.substring(1, b.length()));
    editDistance = Math.min(editDistance, 1 + editDistance(a, b.substring(1, b.length())));
    editDistance = Math.min(editDistance, 1 + editDistance(b, a.substring(1, a.length())));
    return editDistance;

Rahul on May 2, 2013

Add Answers or Comments

To comment on this, Sign In or Sign Up.