Throne Inheritance - Problem

๐Ÿ‘‘ Throne Inheritance System

Imagine you're designing a digital system for a medieval kingdom to track the line of succession to the throne. The kingdom follows a specific inheritance order based on a depth-first traversal of the royal family tree.

How it works:

  • The king is always first in line
  • Children inherit before their aunts/uncles (depth-first)
  • Among siblings, older children inherit before younger ones
  • When someone dies, they're marked as deceased but stay in the family tree

Your task: Implement a ThroneInheritance class that can:

  1. birth(parent, child) - Add a new child to the family
  2. death(person) - Mark someone as deceased
  3. getInheritanceOrder() - Return current inheritance order (excluding dead people)

Example: King โ†’ Alice โ†’ Alice's son Jack โ†’ Bob
If Alice dies, the order becomes: King โ†’ Jack โ†’ Bob

Input & Output

basic_inheritance.py โ€” Python
$ Input: ThroneInheritance("king") birth("king", "andy") birth("king", "bob") birth("king", "catherine") birth("andy", "matthew") birth("bob", "alex") birth("bob", "asha") getInheritanceOrder()
โ€บ Output: ["king", "andy", "matthew", "bob", "alex", "asha", "catherine"]
๐Ÿ’ก Note: DFS traversal: King โ†’ Andy โ†’ Andy's children (Matthew) โ†’ Bob โ†’ Bob's children (Alex, Asha) โ†’ Catherine
with_deaths.py โ€” Python
$ Input: ThroneInheritance("king") birth("king", "andy") birth("king", "bob") birth("andy", "matthew") death("andy") getInheritanceOrder()
โ€บ Output: ["king", "matthew", "bob"]
๐Ÿ’ก Note: Andy dies but stays in family tree. Matthew (Andy's son) inherits Andy's position. Order: King โ†’ Matthew โ†’ Bob
complex_family.py โ€” Python
$ Input: ThroneInheritance("king") birth("king", "alice") birth("king", "bob") birth("alice", "jack") birth("alice", "jill") birth("bob", "charlie") death("alice") death("bob") getInheritanceOrder()
โ€บ Output: ["king", "jack", "jill", "charlie"]
๐Ÿ’ก Note: Both Alice and Bob die, but their children inherit their positions in the family tree structure

Visualization

Tap to expand
๐Ÿ‘‘ Throne Inheritance: DFS Tree Traversal๐Ÿ‘‘King1Alice๐Ÿ’€XBob3Jack2๐Ÿ“Š Data StructuresChildren Map:king โ†’ [Alice, Bob]Alice โ†’ [Jack]Bob โ†’ []๐Ÿ’€ Dead Set{Alice}Skip in traversal๐Ÿ‘‘ Result[King, Jack, Bob]DFS orderSkip AliceVisit order
Understanding the Visualization
1
Build Family Tree
Each birth adds a child to their parent's children list
2
Track Deaths
Dead people stay in tree but are marked in a separate set
3
DFS Traversal
Visit king first, then each child's subtree completely before siblings
Key Takeaway
๐ŸŽฏ Key Insight: Throne inheritance is just a DFS traversal of the family tree, where we maintain parent-child relationships efficiently using a hash map and skip deceased members during traversal.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Each getInheritanceOrder takes O(n) time to visit each person once via DFS

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Space for children hash map and dead set, both proportional to number of people

n
2n
โšก Linearithmic Space

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
  • At most 105 calls will be made to birth and death
  • At most 104 calls will be made to getInheritanceOrder
Asked in
Microsoft 15 Amazon 12 Google 8 Meta 5
42.3K Views
Medium Frequency
~25 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen