Dsa
Recursion
Medium
The Prime Multiplier Tree
A tree is represented in a flat format: 'value num_children [child_nodes...]'. A node's 'Total Wealth' is calculated as its own 'value' plus the sum of its children's Total Wealth. However, if a node's 'value' is a prime number, its children's wealth contributions are doubled.
Prime numbers for this task are 2, 3, 5, 7, 11, 13, 17, 19.
Input Format: A single line of space-separated integers.
Example: '3 2 4 0 5 0'
- Root is 3. It has 2 children.
- Child 1 is 4, which has 0 children (Total Wealth = 4).
- Child 2 is 5, which has 0 children (Total Wealth = 5).
- Root 3 is prime, so children wealth is doubled: 3 + (2 * 4) + (2 * 5) = 21.
Output: A single integer representing the Total Wealth of the root.
Key concepts
recursiontree traversalmath
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now