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]- A. 2
- B. 3
- C. 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.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- What will be the output of the following pseudocode? Integer a[6] = {5, 5, 10, 100, 10, 5} Integer incl = 0, excl = 0, t for (each i from…
- An array of n elements is ALREADY sorted in increasing order. What is the time complexity of this pseudocode on it? for (each i from 1 to…
- What is the time complexity of the following pseudocode, as a function of n? Procedure f(Integer n) if (n <= 1) return end if for (each i…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Integer n = 7086351, s = 0, d…
- What will be the output of the following pseudocode? Procedure upd(Integer arr[], Integer n) // arrays are passed by reference arr[0] =…
- What will be the output of the following pseudocode? (& is bitwise AND) Integer x = 255, y = 256, c1 = 0, c2 = 0 while (x > 0) x = x & (x…