Data Structures & AlgorithmsVery hardProduct placement · Amazon/Microsoft
What does this loop-counting code print? (Python)
What does this loop-counting code print? (Python)
n, c = 33, 0
i = 1
while i < n:
j = i
while j < n:
c += 1
j *= 2
i += 1
print(c)- A. 65
- B. 272
- C. 63
- D. 64
Correct answer: C. 63
For each i the inner loop runs about log₂(n/i) + 1 times; summing over i gives Θ(n). Exact count: 63.
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
- Evaluate an infix expression of single digits with two stacks (operands and operators, usual precedence, left-associative). What is…
- One Lomuto partition (pivot = LAST element; i marks the end of the 'less than pivot' region; when a[j] < pivot, increment i and swap a[i]…
- Insertion sort (ascending; shift larger elements right, one key comparison per inner-loop test, the loop stops at the first element ≤ the…
- One Hoare partition with the FIRST element as pivot (i moves right while a[i] < pivot, j moves left while a[j] > pivot, swap while i < j…
- How many inversions (pairs i < j with a[i] > a[j]) does [5, 6, 10, 37, 38, 4, 17, 23] have? (This is also the number of swaps bubble sort…
- Keys 112, 95, 54, 144, 21, 31, 32 are inserted in this order into an empty table of size 11 by double hashing: h1(k) = k mod 11, h2(k) = 7…