Data Structures & AlgorithmsHardTCS Digital · Infosys SP

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

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

Correct answer: B. 5

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

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

Exams that ask this topic