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
  1. A. 10 10
  2. B. 10 9
  3. C. 9 10
  4. 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.

More Pseudocode and Code Tracing questions

Exams that ask this topic