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 p = "ABABCABAB"
Integer f[9], k = 0
f[0] = 0
for (each i from 1 to 8)
while (k > 0 AND p[i] != p[k])
k = f[k - 1]
end while
if (p[i] == p[k])
k = k + 1
end if
f[i] = k
end for
Print f[3], f[4], f[8]- A. 2 3 3
- B. 2 0 4
- C. 1 0 4
- D. 2 0 5
Correct answer: B. 2 0 4
f[i] is the length of the longest proper prefix of p[0..i] that is also its suffix (the KMP failure table). ABAB → 2 (AB), ABABC → 0, and ABABCABAB → 4 (ABAB). Output 2 0 4.
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[4] = {1, 2, 3, 4}, res[4], left = 1, right = 1 for (each i from 0 to 3)…
- What will be the output of the following pseudocode? Integer a[5] = {2, 3, -2, 4, -1} Integer mx = a[0], mn = a[0], best = a[0], t for…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer a[7] = {-7, 1, 5, 2, -4, 3, 0} Integer total = 0…
- What will be the output of the following pseudocode? Integer a[5] = {1, 5, 3, 4, 2}, k = 2, c = 0 for (each i from 0 to 4) for (each j…
- What is the time complexity of the following pseudocode, as a function of n? for (each i from 1 to n) for (each j from 1 to i i) if (j mod…
- What is the extra space (memory) used by the following recursive pseudocode as a function of n, counting the call stack? Function…