ProgrammingHardGoldman Sachs OA · Amazon OA
What will be the output of the following pseudocode? (All variables are integers, so / is integer division)
What will be the output of the following pseudocode? (All variables are integers, so / is integer division)
Integer lo = 1, hi = 1000, mid, steps = 0
while (lo < hi)
mid = (lo + hi) / 2
if (mid * mid * mid >= 1000)
hi = mid
else
lo = mid + 1
end if
steps = steps + 1
end while
Print lo, steps- A. 10 10
- B. 10 9
- C. 9 10
- D. 11 10
Correct answer: A. 10 10
The search finds the smallest k with k³ ≥ 1000, which is 10. The range 1..1000 is halved each step: 1000 → 500 → 250 → 125 → 63 → 32 → 16 → 8 → 4 → 2 → 1 values, i.e. 10 steps. Output 10 10.
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 bitwise XOR) Integer x = 0 for (each i from 1 to 10) x = x ^ i end for Integer…
- What will be the output of the following pseudocode? (^ is XOR, & is AND, >> is right shift) Integer x = 21, z = 22 Integer y = x ^ (x >>…
- What will be the output of the following pseudocode? (code(c) is the ASCII code: 'A' = 65, 'a' = 97) Function cmp(String a, String b)…
- What will be the output of the following pseudocode? Integer a[8] = {3, 1, 4, 1, 5, 9, 2, 6} Integer best = a[0], c = 1 for (each i from 1…
- What will be the output of the following pseudocode? Integer a[6] = {100, 4, 200, 1, 3, 2} Set s = all values of a Integer best = 0, len…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer m[4][4] = {{1, 4, 7, 11}, {2, 5, 8, 12}, {3, 6…