Facebook | Phone | Tree with multiple parents

Anonymous User
4932
Dec 13, 2020
Dec 15, 2020

Two questions: 45 Mins

Question 1
Given a list of pairs in the format [(parent, child)] find the nodes with zero parent and one parent. On probing, the nodes are unique positive integers

find_zero_and_one_parent(list_of_parent_child_pairs) -> [list, list]

Question 2
As an extension to the first question, given a similar list of parent child pairs and two nodes find if a common ancestor exists.

find_if_common_ancestor_exists(list_of_parent_child_pairs, node_1, node_2) -> bool

list_of_parent_child_pairs = [(11,7), (11,1), (1,2), (1,4), (3,5)...]

image

Had to run the code for a few test cases, the interviewer had handy.

  1. Does it make it harder having to read inputs from test cases and show a successful output vs just writing up production quality code and not having to run test cases.
  2. Are these medium level questions or easy-mediums or medium-hard (considering one has to pass the test cases?
Comments (13)