Data Structures & AlgorithmsVery hardProduct company OA
Exact count of the innermost statement for n = 30 (j advances by i, k advances by j). What is printed?
Exact count of the innermost statement for n = 30 (j advances by i, k advances by j). What is printed?
c = 0
n = 30
for i in range(1, n + 1):
j = 1
while j <= n:
k = j
while k <= n:
c += 1
k += j
j += i
print(c)- A. 111
- B. 133
- C. 900
- D. 1118
Correct answer: D. 1118
For a fixed j the k-loop runs ⌊30/j⌋ times; the j-loop visits 1, 1 + i, 1 + 2i, …. For i = 1 that alone is Σⱼ ⌊30/j⌋ = 111 (≈ n ln n); for large i only j = 1 survives, still contributing 30. Summing over all i gives 1118. 133 is the number of (i, j) pairs – the count without the innermost loop – and 900 is n² as if every loop ran n times.
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- A memoised recursion f(n) = f(n//2) + f(n//3) + f(n//6) is evaluated for n = 10¹². How many distinct calls (cache misses) are made? python…
- Patience-sorting tails array for the longest STRICTLY increasing subsequence, and the length for the non-decreasing variant. What is…
- Weighted interval scheduling (start, end, value): DP over jobs sorted by end time with binary search for the last compatible job, versus…
- Broken-profile DP: number of ways to tile a 3 × n board with 1 × 2 dominoes, column by column with a 3-bit mask of cells already covered…
- Digit DP (position, running sum, tight flag) counts integers in [0, N] whose digits sum to 10, cross-checked by brute force. What is…
- All suffixes of 'banana' are inserted into a character trie (a suffix trie). What is printed (trie nodes including the root, distinct…