ProgrammingHardAmazon OA · Goldman Sachs OA
What will be the output of the following pseudocode? (substring(s, i, len) is len characters starting at index i; a Set keeps one copy of each value)
What will be the output of the following pseudocode? (substring(s, i, len) is len characters starting at index i; a Set keeps one copy of each value)
String s = "ABAB"
Set seen = empty
for (each i from 0 to length(s) - 1)
for (each len from 1 to length(s) - i)
add substring(s, i, len) to seen
end for
end for
Print size(seen)- A. 10
- B. 8
- C. 7
- D. 6
Correct answer: C. 7
There are 10 substrings by position, but repeats are stored once: A, B, AB, BA, ABA, BAB, ABAB — 7 distinct ones.
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? (All variables are integers, so / is integer division; strings are indexed from 0)…
- 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…