FastPrepThrone Inheritance Without an Initial King
Problem · Design

Throne Inheritance Without an Initial King

Learn this problem
MediumSnowflake logoSnowflakeFULLTIMEPHONE SCREEN
See Snowflake hiring insights

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, create parent as its founder, then add child as 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 most 20 rows are ORDER queries.
  • 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 1 through 10.
  • 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 100 distinct members.

More Snowflake problems

drafts saved locally
public String[][] inheritanceOrders(String[][] operations) {
    // Write your code here
}
operations[["ORDER"],["BIRTH","rhea","zoe"],["BIRTH","rhea","amy"],["BIRTH","zoe","leo"],["ORDER"],["DEATH","zoe"],["ORDER"]]
expected[[], ["rhea", "zoe", "leo", "amy"], ["rhea", "leo", "amy"]]
checking account