Data Structures & AlgorithmsHardProduct placement · Amazon/Microsoft

What does this loop-counting code print? (Python)

What does this loop-counting code print? (Python)
n, c = 16, 0
i = 2
while i <= n:
    c += 1
    i = i * i
print(c)
  1. A. 3
  2. B. 5
  3. C. 4
  4. D. 2

Correct answer: A. 3

i goes 2, 4, 16, 256, 65536, … (squaring), so the loop runs Θ(log log n) times. Exact count: 3.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic