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 = 10, 0
for i in range(1, n + 1):
j = 1
while j <= n:
c += 1
j += i
print(c)- A. 34
- B. 32
- C. 100
- D. 33
Correct answer: D. 33
For each i the inner loop runs ⌈n/i⌉ times, so the total is the harmonic-like sum Σ⌈n/i⌉ = Θ(n log n). Exact count: 33.
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
- How many characters must be deleted in total from two strings so that they become anagrams of each other? What is printed? python from…
- A hash-map two-sum returns the first index pair (i, j), i < j, found while scanning left to right. What is printed? python def twosum(a…
- Evaluate an infix expression of single digits with two stacks (operands and operators, usual precedence, left-associative). What is…
- Reverse a singly linked list in groups of k (a final group shorter than k is left as it is). What is printed? python def revk(h, k): cnt…
- Integer square root by binary search (largest m with m·m ≤ n). What is printed (root, iterations)? python n, lo, hi, it = 777, 0, 777, 0…
- Merge two sorted linked lists by relinking nodes (ties take the node from the first list). What is printed? python def merge(a, b): d = t…