Karat OA
Anonymous User
6503

There were some System Design scenario based questions and then a problem to solve.
Given a set of relationship between parent and child , create two lists where one presents children with one parent while another one where children with no known parents.
[1,3][2,3][3,4][4,5][6,5] --> would be the answer in this case {4} & {1,2,6}

Modified the problem after this : Given a set of parent child pairs , find if there are any common ancestory between any two given input like hasCommonAns(int a, int b) should return true or false based.
For this part , i went with the approach of preparing an adjlist of child to parent and then traversing them in dfs to collect all ancestors in a set. Similarly for another input b and then compare the two sets for any common ancestor -- any suggestions please ?

Comments (7)