Skip to content

Latest commit

 

History

History
123 lines (106 loc) · 3.04 KB

File metadata and controls

123 lines (106 loc) · 3.04 KB

Question #29. Monarchy

Implement the following interface thatrepresents the order of succession in a Monarchy:

  • Monarchy(king) Initializes the Monarchy object with the name of the current king.
  • void birth(String childName, String parentName) Gives birth to a child given the name the parent.
  • void death(String name) Removes the name of the dead person from the order of succession.
  • List<String> getOrderOfSuccession() Returns the order of succession based on the living members in the family tree.

Examples

  • const mon = new Monarchy("Jake");
    mon.birth("Catherine", "Jake");
    mon.birth("Tom", "Jake");
    mon.birth("Celine", "Jake");
    mon.birth("Peter", "Celine");
    mon.birth("Jane", "Catherine");
    mon.birth("Farah", "Jane");
    mon.birth("Mark", "Catherine");
    mon.getOrderOfSuccession();
    // should return ["Jake", "Catherine", "Jane", "Farah", "Mark", "Tom", "Celine", "Peter"]
  • mon.death("Jake");
    mon.death("Jane");
    mon.getOrderOfSuccession();
    // should return ["Catherine", "Farah", "Mark", "Tom", "Celine", "Peter"]

Solution

class Person {
  /**
   * Initialize a helper interface
   * that stores some basic info about a person
   * @param {string} name
   */
  constructor(name) {
    this.name = name;
    this.isAlive = true;
    this.children = [];
  }
}

class Monarchy {
  /**
   * Initialize the Monarchy interface,
   * with the name of the current king
   * @param {string} king
   */
  constructor(king) {
    this.king = new Person(king);
    this._members = {
      [this.king.name]: this.king,
    };
  }

  /**
   * Give birth to a child,
   * given the name the parent
   * @param {string} childName
   * @param {string} parentName
   * @return {void}
   */
  birth(childName, parentName) {
    const parent = this._members[parentName];

    if (parent) {
      const newChild = new Person(childName);

      parent.children.push(newChild);
      this._members[childName] = newChild;
    }
  }

  /**
   * Remove the name of the dead person
   * from the order of succession
   * @param {string} name
   * @return {void}
   */
  death(name) {
    const person = this._members[name];

    if (person) {
      person.isAlive = false;
    }
  }

  /**
   * Get the order of succession
   * based on the living members in the family tree
   * @return {string[]}
   */
  getOrderOfSuccession() {
    //  Initialize an array to store the order of succession
    const orderOfSuccession = [];

    //  Create a recursive helper function
    //  to help traverse the family tree
    //  and populate the order of succession
    function traverseFamilyTree(parent) {
      //  If parent is still alive:
      if (parent.isAlive) {
        //  Add the parent's name to the order of succession
        orderOfSuccession.push(parent.name);
      }

      //  Make a recursive call for every child
      for (let child of parent.children) {
        traverseFamilyTree(child);
      }
    }

    //  Invoke the recursive helper function and return
    traverseFamilyTree(this.king);
    return orderOfSuccession;
  }
}