GATE DA 2024 Q38: Output of a recursive count over a child dictionary
- Programming, Data Structures and Algorithms
- 2 marks
- MCQ
- −2/3 if wrong
Question
Consider the following Python code:
def count(child_dict, i):
if i not in child_dict.keys():
return 1
ans = 1
for j in child_dict[i]:
ans += count(child_dict, j)
return ans
child_dict = dict()
child_dict[0] = [1,2]
child_dict[1] = [3,4,5]
child_dict[2] = [6,7,8]
print(count(child_dict,0))Which ONE of the following is the output of this code?
Try it: multiple choice: exactly one correct option
Wrong answer costs 2/3 mark in GATE.
Show answer and explanation
Answer: D (official GATE 2024 final answer key)
Concept used: Recursion counting the nodes of a tree stored as a parent → children dictionary.
First step: count(i) is 1 for node i plus the counts of its children: the tree has root 0, children 1 and 2, and leaves 3–8.
Full step-by-step solution
Detailed working, common mistakes and linked mock tests will be available with a free GATEiit account. Log-in is coming soon.
Share this question
Source: official GATE 2024 DA question paper and final answer key, organised by IISc Bangalore. Question paper · Answer key
More Programming, Data Structures and Algorithms PYQs
- 2026 · Q15Expected-time recurrence for randomised-input quicksort
- 2026 · Q16Python mutable default argument output
- 2026 · Q25Binary tree from preorder and inorder traversals
- 2026 · Q31Worst-case comparisons in binary search of 1000 elements
- 2026 · Q39Number of calls made by a Fibonacci-like recursion
- 2026 · Q40Reachability in a graph and its reverse