Company: goldman sachs

Difficulty: medium

Problem Statement

Eldest Ancestor and Descendant Count You are given `N` distinct father-child pairs that form one family tree. Find the eldest ancestor (the only person who never appears as a child). Also count everyone who is a grandchild of that ancestor or a descendant of such a grandchild; equivalently, count all people at depth at least two below the eldest ancestor. Input Format The first line contains `N`. Each of the next `N` lines contains a father name and a child name separated by one space. Output Format Print the eldest ancestor's name on the first line and the count on the second line. Constraints `1 <= N <= 1000`. Names contain only letters and have length at most 100. Every child has exactly one father and the pairs form one rooted tree. Notes The source asks for grandchildren and their descendants. Children of the eldest ancestor are not included in the count; every node at depth two or more is.

More goldman sachs OA questionsInterview experiences