Data Structures & AlgorithmsHardGATE CS · DSA
What does this loop-counting code print? (Python)
What does this loop-counting code print? (Python)
n, c = 300, 0
i = 2
while i <= n:
c += 1
i = i * i
print(c)- A. 4
- B. 5
- C. 8
- D. 6
Correct answer: A. 4
i goes 2, 4, 16, 256, 65536, … (squaring), so the loop runs Θ(log log n) times. Exact count: 4.
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
- Integer square root by binary search (largest m with m·m ≤ n). What is printed (root, iterations)? python n, lo, hi, it = 1000, 0, 1000, 0…
- A mountain is a contiguous subarray of length ≥ 3 that strictly increases to a single peak and then strictly decreases. What is the length…
- How many inversions (pairs i < j with a[i] > a[j]) does [38, 33, 1, 17, 31, 28, 18, 35] have? (This is also the number of swaps bubble…
- What is the worst-case number of comparisons made by binary search (three-way comparison per probe) on a sorted array of 1,000 elements…
- Keys 110, 52, 100, 101, 53, 74, 96 are inserted in this order into an empty table of size 13 using quadratic probing: probe i goes to (k…
- For the binary array [1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1], replace 0 by −1 and use prefix sums with a first-occurrence map. What is the…