ProgrammingVery hardAmazon OA · Goldman Sachs OA
What will be the output of the following pseudocode? (All variables are integers, so / is integer division; strings are indexed from 0)
What will be the output of the following pseudocode? (All variables are integers, so / is integer division; strings are indexed from 0)
String s = "AAAB"
Integer c = 0, l, r
for (each center from 0 to 2 * length(s) - 2)
l = center / 2
r = l + center mod 2
while (l >= 0 AND r < length(s) AND s[l] == s[r])
c = c + 1
l = l - 1
r = r + 1
end while
end for
Print c- A. 4
- B. 10
- C. 6
- D. 7
Correct answer: D. 7
The 2n − 1 centres cover odd and even lengths, and every palindromic substring (counted by position) is found once. In AAAB: 4 single letters, AA twice, AAA once: 7.
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? Function p(Integer n, Integer k) if (n == 0) return 1 end if if (n < 0 OR k == 0)…
- What will be the output of the following pseudocode? Integer calls = 0 Function f(Integer a, Integer b) calls = calls + 1 if (b == 0)…
- What will be the output of the following pseudocode? Integer g[5][5] = {{1,1,0,0,0}, {1,0,0,1,1}, {0,0,1,0,0}, {1,0,1,1,0}, {1,0,0,0,1}}…
- What will be the output of the following pseudocode? Integer m[4][4], s = 0 for (each i from 0 to 3) for (each j from 0 to 3) m[i][j] = 4…
- What will be the output of the following pseudocode? Boolean comp[0..30] = all false Integer s = 0 for (each i from 2 to 30) if (not…
- What will be the output of the following pseudocode? Function isPrime(Integer n) if (n < 2) return false for (each d from 2 to n - 1) if…