ProgrammingHardAmazon OA · Goldman Sachs OA
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer a[6] = {1, 2, 4, 5, 7, 9}
Integer i = 0, j = 5, c = 0
while (i < j)
if (a[i] + a[j] < 10)
c = c + (j - i)
i = i + 1
else
j = j - 1
end if
end while
Print c- A. 8
- B. 5
- C. 10
- D. 6
Correct answer: A. 8
This two-pointer loop counts pairs with sum < 10 in a sorted array: once a[i] + a[j] < 10, all j − i partners of a[i] work. Pairs: 1 with 2, 4, 5, 7 (4); 2 with 4, 5, 7 (3); 4 with 5 (1): total 8.
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[7] = {2, -1, 4, 3, -2, 5, 1} Integer s = a[0] + a[1] + a[2], best = s for…
- What will be the output of the following pseudocode? Integer a[3] = {-3, -1, -4} Integer cur = 0, best = 0 for (each i from 0 to 2) cur =…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Integer r = 1, b = 3, e = 13…
- What will be the output of the following pseudocode? Integer c[0..5] = {1, 0, 0, 0, 0, 0} for (each i from 1 to 5) for (each j from 1 to…
- What will be the output of the following pseudocode? (C-style switch: a case without break falls through to the next one) Integer x = 2, s…
- What will be the output of the following pseudocode? Integer i = 1, c = 0 while (i <= 100) i = i 2 + c c = c + 1 end while Print c, i