ProgrammingVery hardAmazon OA · Goldman Sachs OA

What will be the output of the following pseudocode? (strings are indexed from 0)

What will be the output of the following pseudocode? (strings are indexed from 0)
String x = "SUNDAY", y = "SATURDAY"
Integer D[0..6][0..8]
for (each i from 0 to 6) D[i][0] = i
for (each j from 0 to 8) D[0][j] = j
for (each i from 1 to 6)
    for (each j from 1 to 8)
        if (x[i - 1] == y[j - 1])
            D[i][j] = D[i - 1][j - 1]
        else
            D[i][j] = 1 + min(D[i - 1][j], D[i][j - 1], D[i - 1][j - 1])
        end if
    end for
end for
Print D[6][8]
  1. A. 2
  2. B. 3
  3. C. 4
  4. D. 5

Correct answer: B. 3

D is the edit-distance table (insert, delete, replace). SUNDAY → SATURDAY needs 3 edits: insert A, insert T, and replace N with R. The length difference alone suggests 2, which is not enough.

More Pseudocode and Code Tracing questions

Exams that ask this topic