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 = "ABBCCCDDDD"
Integer best = 0, run = 1
for (each i from 1 to length(s) - 1)
if (s[i] == s[i - 1])
run = run + 1
else
if (run > best)
best = run
end if
run = 1
end if
end for
Print best- A. 4
- B. 3
- C. 10
- D. 1
Correct answer: B. 3
best is updated only when the character changes. The final run DDDD (length 4) ends with the string, so it is never compared, and best stays at the CCC run: 3. The fix is one more comparison after the loop.
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 M[2][2] = {{1, 1}, {1, 0}}, R[2][2] = {{1, 0}, {0, 1}}, T[2][2] for (each k…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Integer s = 0 for (each i from…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Integer n = 360, d = 2, s = 0…
- What will be the output of the following pseudocode? Integer count = 0, s for (each n from 2 to 500) s = 1 for (each d from 2 to n - 1) if…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Integer count = 0, n, s, d for…
- What will be the output of the following pseudocode? Integer c = 0 for (each y from 1890 to 2010) if ((y mod 4 == 0 AND y mod 100 != 0) OR…