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 s = "BANANAS"
Integer best = 1, l, r, n = 7
for (each c from 0 to n - 1)
l = c
r = c
while (l >= 0 AND r < n AND s[l] == s[r])
if (r - l + 1 > best)
best = r - l + 1
end if
l = l - 1
r = r + 1
end while
end for
Print best- A. 5
- B. 3
- C. 7
- D. 4
Correct answer: A. 5
Expanding around each centre finds odd-length palindromes. Around the middle A (index 3) it grows A → NAN → ANANA, and then B ≠ S stops it. So best = 5.
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? (& is AND, | is OR, << and >> are shifts; 15 = 0000 1111 and 240 = 1111 0000) Integer…
- What will be the output of the following pseudocode? Integer a[7] = {4, 1, 2, 1, 2, 5, 4} Integer x = 0 for (each i from 0 to 6) x = x ^…
- What will be the output of the following pseudocode? Integer a = 29, b = 15, x, c = 0 x = a ^ b while (x > 0) c = c + (x & 1) x = x >> 1…
- What will be the output of the following pseudocode? (integers are two's complement; ~ is bitwise NOT; >> is an arithmetic shift that…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Integer n = 19, steps = 0, s…
- What will be the output of the following pseudocode? Integer u[1..10], i2 = 1, i3 = 1, i5 = 1, nx u[1] = 1 for (each k from 2 to 10) nx =…