Data Structures & AlgorithmsHardFlipkart/Uber OA · placement

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

What does this loop-counting code print? (Python)
n, c = 64, 0
i = n
while i > 0:
    for j in range(i):
        c += 1
    i //= 2
print(c)
  1. A. 1056
  2. B. 448
  3. C. 127
  4. D. 129

Correct answer: C. 127

The inner loop runs i times while i halves: n + n/2 + n/4 + … < 2n, so the total is Θ(n), not Θ(n log n). Exact count: 127.

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

Exams that ask this topic