Data Structures & AlgorithmsHardProduct Company SDE pattern
Edit distance between "kitten" and "sitting", and between "intention" and "execution". What is printed?
Edit distance between "kitten" and "sitting", and between "intention" and "execution". What is printed?
def ed(a, b):
prev = list(range(len(b) + 1))
for i, x in enumerate(a, 1):
cur = [i]
for j, y in enumerate(b, 1):
cur.append(prev[j-1] if x == y else 1 + min(prev[j-1], prev[j], cur[-1]))
prev = cur
return prev[-1]
print(ed("kitten", "sitting"), ed("intention", "execution"))- A. 3 4
- B. 2 5
- C. 4 5
- D. 3 5
Correct answer: D. 3 5
kitten → sitten (k→s) → sittin (e→i) → sitting (+g) = 3. intention → execution needs 5 (one deletion, three substitutions and one insertion in the standard alignment). Two rolling rows make the space O(n). Time O(mn).
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- Matrix chain order for dimensions 5 × 10, 10 × 3, 3 × 12, 12 × 5, 5 × 50, 50 × 6. What is the minimum number of scalar multiplications?…
- What is printed? (level-order traversal of the BST built from these keys) python def insert(t, k): if t is None: return [k, None, None] i…
- What is printed? (inorder successor in a BST) python def insert(t, k): if t is None: return [k, None, None] i = 1 if k < t[0] else 2 t[i]…
- What is printed? (heap sort on a small array, showing the array after the first extraction) python def sift(a, i, n): while 2 i + 1 < n: c…
- What is printed? (trie: count distinct prefixes / nodes) python words = ["tea", "ten", "to", "inn", "in", "i"] root, nodes = {}, 0 for w…
- What is printed? (count connected components with DFS on an adjacency list) python n = 8 edges = [(0, 1), (1, 2), (3, 4), (5, 6), (6, 7)…