Data Structures & AlgorithmsEasyWipro · Accenture pattern
What is printed? (recursion output order)
What is printed? (recursion output order)
out = []
def f(n):
if n == 0: return
out.append(n)
f(n - 1)
out.append(n)
f(3)
print(*out)- A. 3 2 1 1 2 3
- B. 3 2 1
- C. 1 2 3 3 2 1
- D. 3 2 1 3 2 1
Correct answer: A. 3 2 1 1 2 3
Each call prints n on the way down, recurses, then prints n again on the way back up as the stack unwinds: 3 2 1 then 1 2 3. Depth n, 2n prints, O(n).
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
- Solve the recurrences numerically: how many 'work units' does each do for n = 64? python from functools import lrucache @lrucache(None)…
- 0/1 knapsack, capacity 50: items (weight, value) = (10, 60), (20, 100), (30, 120). What do greedy-by-ratio and DP give? python items =…
- Activity selection: activities (start, finish). How many can one person attend, and which? python acts = [(1, 4), (3, 5), (0, 6), (5, 7)…
- Huffman coding: frequencies a:5, b:9, c:12, d:13, e:16, f:45. What is the total number of bits to encode the text? python import heapq f =…
- Longest common subsequence: what is the length and one LCS read back from the table (preferring the upper cell on ties)? python a, b =…
- Edit distance between "kitten" and "sitting", and between "intention" and "execution". What is printed? python def ed(a, b): prev =…