1600. Throne Inheritance
Asked at Snowflake
Difficulty
Medium
Time
O(1) for birth and death, O(n) f
Space
O(n)
Pattern
Hash Table
Companies
1
Problem
You are asked to design a class that models a royal family tree and answers questions about who inherits the throne.
The constructor gets the king's name. Then two updates arrive over time: birth(parentName, childName) says that parentName had a new child, and death(name) marks a person as dead. A dead person is not removed from the tree — their children still inherit through them — they are just skipped when you list the order.
The inheritance order is a preorder traversal of the family tree: start at the king, then take each child in the order they were born, and fully walk that child's line before moving to the next sibling. getInheritanceOrder() must return this list of names, leaving out everyone who is dead.
So birth and death are cheap bookkeeping, and the real work happens only when getInheritanceOrder() is called.
The family tree and its preorder order
Constraints
- ●
1 <= kingName.length, parentName.length, childName.length, name.length <= 15 - ●
kingName, parentName, childName, and name consist of lowercase English letters only - ●
All arguments childName and kingName are distinct - ●
All name arguments of death will be passed to either the constructor or as childName to birth first - ●
For each call to birth(parentName, childName), parentName is guaranteed to be alive - ●
At most 10^5 calls will be made to birth and death - ●
At most 10 calls will be made to getInheritanceOrder
Examples
["ThroneInheritance", "birth", "birth", "birth", "birth", "birth", "birth", "getInheritanceOrder", "death", "getInheritanceOrder"]
[["king"], ["king", "andy"], ["king", "bob"], ["king", "catherine"], [null, null, null, null, null, null, null, ["king", "andy", "matthew", "bob", "alex", "asha", "catherine"], null, ["king", "andy", "matthew", "alex", "asha", "catherine"]]["ThroneInheritance", "birth", "death", "getInheritanceOrder"]
[["king"], ["king", "andy"], ["king"], [null]][null, null, null, ["andy"]]Asked At
1 companies| Company | Difficulty | |
|---|---|---|
| Snowflake | Medium | View all Snowflake questions → |
How to Think About It
Store the family as a tree: a map from each person to the list of their children, appended in birth order.
Key insight: the order of inheritance is a preorder traversal — the king, then his first child and that child's entire line, then his second child and their line, and so on.
Deaths do not remove people from the tree (their descendants still inherit through them). Keep a dead set and simply skip dead people when building the order.
birth and death are O(1); getInheritanceOrder is O(n) — the cost is pushed to the rare query operation.
Use an iterative DFS with a stack (pushing children in reverse) to avoid recursion depth limits on long family lines.
Optimal Approach
State: king, children map, dead set.
birth(parent, child): append child to children[parent].death(name): add name to dead.getInheritanceOrder(): preorder DFS from king, adding each person not in dead; visit children in the order they were born.
Time: O(1) for birth and death, O(n) for the order. Space: O(n).
Iterative preorder with a stack
What Trips People Up in Real Interviews
Implementing the recursive Successor function from the statement literally. Recognizing it as preorder is the whole point.
Deleting dead people from the tree, which loses their descendants' place in the line.
Pushing children onto the stack in birth order — they must be pushed in reverse so the eldest is popped first.
Recursing on a family line of 10^5 generations in Python without raising the recursion limit.
Solution Code
O(1) for birth and death, O(n) f timeclass ThroneInheritance:
def __init__(self, kingName):
self.king = kingName
self.children = {}
self.dead = set()
def birth(self, parentName, childName):
self.children.setdefault(parentName, []).append(childName)
def death(self, name):
self.dead.add(name)
def getInheritanceOrder(self):
order = []
stack = [self.king]
while stack:
p = stack.pop()
if p not in self.dead:
order.append(p)
stack.extend(reversed(self.children.get(p, [])))
return orderFrequently Asked Questions
What is the Throne Inheritance problem?
Throne Inheritance asks you to model a royal family: record births and deaths, and return the current order of succession. The succession order turns out to be exactly a preorder traversal of the family tree (children in birth order), skipping anyone who has died.
How do you solve Throne Inheritance?
The optimal approach runs in O(1) for birth and death, O(n) f time and O(n) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.
What companies ask Throne Inheritance?
Throne Inheritance is asked at Snowflake. It is a medium difficulty problem.
What are common mistakes on Throne Inheritance?
- Implementing the recursive `Successor` function from the statement literally. Recognizing it as preorder is the whole point.
- Deleting dead people from the tree, which loses their descendants' place in the line.
- Pushing children onto the stack in birth order — they must be pushed in reverse so the eldest is popped first.
- Recursing on a family line of 10^5 generations in Python without raising the recursion limit.