Data Structures & AlgorithmsVery hardFlipkart/Uber OA · placement
What does this loop-counting code print? (Python)
What does this loop-counting code print? (Python)
n, c = 16, 0
i = 1
while i < n:
j = i
while j < n:
c += 1
j *= 2
i += 1
print(c)- A. 28
- B. 26
- C. 25
- D. 64
Correct answer: B. 26
For each i the inner loop runs about log₂(n/i) + 1 times; summing over i gives Θ(n). Exact count: 26.
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
- For the sorted array [1, 3, 4, 6, 6, 9, 9, 11, 13, 14, 14, 15] (0-based), what are lowerbound(14) (first index with value ≥ 14) and…
- Records are sorted with a composite key. What is printed? python recs = [('kabir', 80), ('dev', 70), ('meera', 70), ('ravi', 70), ('zoya'…
- Stock prices over 8 days are [21, 87, 49, 49, 28, 34, 81, 65]. The span of day i is the number of consecutive days ending at day i…
- You must check whether a singly linked list of n nodes is a palindrome using O(1) extra space. Which approach works in O(n) time?
- The intervals [4, 8], [13, 15], [5, 8], [8, 11], [12, 16], [8, 11] are sorted by start and merged (intervals that touch, like [2, 4] and…
- Find the leaders (elements strictly greater than everything to their right) by scanning from the right. What is printed? python a = [17…