Data Structures & AlgorithmsHardFlipkart/Uber OA · placement
What does this loop-counting code print? (Python)
What does this loop-counting code print? (Python)
n, c = 20, 0
i = n
while i > 0:
for j in range(i):
c += 1
i //= 2
print(c)- A. 39
- B. 110
- C. 100
- D. 38
Correct answer: D. 38
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: 38.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- Rabin–Karp with base 10 and a deliberately small modulus counts true matches and spurious hits (hash equal, text different). What is…
- Using a sliding window with a last-seen index map, what is the length of the longest substring of "beeebdddcbadad" with all characters…
- What is the length of the smallest substring of "bcdadddaacbac" that contains every distinct character of the string at least once?…
- Which lower bound holds for every comparison-based sorting algorithm in the worst case?
- What does this loop-counting code print? (Python) python n, c = 12, 0 for i in range(1, n + 1): j = 1 while j <= n: c += 1 j += i print(c)
- Product of all elements except self, using prefix and suffix products (no division). What is printed? python a = [1, 1, 0, 2, -1] n =…