Throne Inheritance Without an Initial King
Updated
Problem statement
Maintain a family's inheritance order. Initially the family is empty: there is no king or founder argument. Process the string rows in operations in order.
["BIRTH", parent, child]: if the family is empty, createparentas its founder, then addchildas that parent's first child. Otherwise the parent is an existing living family member. The child is always a new name. Append the child after the parent's previously born children.["DEATH", name]: mark an existing family member as dead. Repeating this operation has no further effect.["ORDER"]: record the current inheritance order of living members.
Return one array of names for each ORDER row, in query order. Births and deaths do not add result rows. A query before the first birth returns an empty array.
To determine an order, begin with the founder, then visit each child and that child's entire descendant branch before moving to the next child. Visit siblings in birth order, not alphabetical order. Omit dead people from the returned names, but still visit their descendants in their original positions.
The founder is never replaced, even after death. A death never deletes a branch or changes parent relationships. A later birth can occur to any living member, including a descendant of a dead founder. There is no operation to create an unrelated second family.
Function
inheritanceOrders(operations: String[][]) → String[][]Examples
Example 1
operations = [["ORDER"],["BIRTH","rhea","zoe"],["BIRTH","rhea","amy"],["BIRTH","zoe","leo"],["ORDER"],["DEATH","zoe"],["ORDER"]]return = [[],["rhea","zoe","leo","amy"],["rhea","leo","amy"]]The first birth creates founder rhea and child zoe. Zoe is older than amy despite alphabetical order; leo stays ahead of amy even after zoe dies.
Example 2
operations = [["BIRTH","orin","bea"],["DEATH","orin"],["BIRTH","bea","cal"],["ORDER"]]return = [["bea","cal"]]The deceased founder stays in the family structure. Living descendant bea can have a child, and both living names remain in order.
Constraints
0 <= operations.length <= 100; at most20rows areORDERqueries.- Each row has exactly the arguments required above. All operation names are uppercase as shown.
- Names contain only lowercase English letters and have length from
1through10. - A child's name has never been used before, including for dead members; in the first birth it differs from the new founder's name.
- Every non-initial birth names an existing living parent, and every death names an existing member. There are at most
100distinct members.
More Snowflake Phone Screen questions
- Connect Four from the Last MovePHONE SCREEN · Seen Nov 2026
- Minimum Edits to a Perfect Binary TreePHONE SCREEN · Seen Nov 2026
- Configurable Per-Client Token BucketsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Find Valid SET Card TriplesOA · PHONE SCREEN · Seen Nov 2026
- Minimum Index Distance Between Person and CakeOA · PHONE SCREEN · Seen Nov 2026
- Minimum N-ary Tree Depth DeletionsOA · PHONE SCREEN · Seen Nov 2026
- Sum of Binary Tree Boundary NodesOA · PHONE SCREEN · Seen Nov 2026
- Greedy Person-Cake Matching DistancePHONE SCREEN · Seen Nov 2026
- Tree Height After Child PromotionPHONE SCREEN · Seen Oct 2026
- Minimum StackPHONE SCREEN · Seen Oct 2026
Similar questions at other companies
- N-ary Index-Path Encoder and DecoderBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Nested Todo ListNotion · PHONE SCREEN · ONSITE INTERVIEWNotion interview questions
- Serialize and Deserialize an N-Ary TreeUber · ONSITE INTERVIEWUber interview questions
- Evaluate a Parsed Expression for Many Values of xGoogle · ONSITE INTERVIEWGoogle interview questions
- N-Ary Tree Codec OperationsLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- Serialize and Deserialize a Binary TreeMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- House Robber IIIOkta · PHONE SCREENOkta interview questions
- Render a Task Tree from CSV RowsStripe · PHONE SCREEN · ONSITE INTERVIEWStripe interview questions
- Validate Binary Search TreeKotak Mahindra Bank · PHONE SCREENKotak Mahindra Bank interview questions
- House Robber IIIGoldman Sachs · PHONE SCREENGoldman Sachs interview questions