Dsa
Recursion
Warm-up
Generations to Root
In a bureaucracy, every employee has exactly one supervisor. The top-most supervisor is always named 'ROOT'. Given a list of employee-supervisor pairs and a starting employee name, find out how many steps it takes to reach 'ROOT'.
Input Format:
Line 1: An integer K, the number of pairs.
Next K lines: Two strings 'Employee Supervisor' separated by a space.
Last line: The name of the employee to start from.
Output Format: An integer representing the number of steps to reach 'ROOT'.
Example:
Input:
3
Alice Bob
Bob Charlie
Charlie ROOT
Alice
Output: 3
Key concepts
recursiondictionaries
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now