Data Structures & AlgorithmsHardIBM · Deloitte pattern
What does this loop-counting code print? (Python)
What does this loop-counting code print? (Python)
n, c = 45, 0
i = 1
while i < n:
j = i
while j < n:
c += 1
j *= 2
i += 1
print(c)- A. 123
- B. 85
- C. 84
- D. 90
Correct answer: B. 85
For each i from 1 to n − 1, the inner loop doubles j from i until it reaches n, running about log₂(n/i) + 1 times; summing this over all i gives Θ(n), and the exact count for n = 45 is 85.
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
- Array [83, 87, 90, 43, 45, 48, 62, 75, 82] is a sorted array rotated at pivot 6. Using the standard O(log n) rotated-array binary search…
- Using a sliding window of fixed size with a character-frequency count, how many substrings of "AAABAAABCB" are anagrams of "BA"?
- Heights [6, 3, 1, 5, 9, 8, 9, 6, 4] represent vertical lines at each index. What is the maximum water area (min(height) × horizontal…
- Kadane's algorithm runs on the array [-9, 1, 7, 5, 7, -3, 0, -7, 1] to find the maximum sum of a contiguous subarray. What sum does it…
- QuickSort (last element as pivot, Lomuto-style partition) sorts [57, 6, 37, 10, 15, 58]. Counting one comparison per element checked…
- Using a sliding window (shrink from the left whenever more than k distinct characters are in the window), what is the length of the…