Dsa
Graphs Dfs
Easy
The Recipe Tree
You are organizing a cooking contest. A complex dish is made by combining raw ingredients and intermediate products. Given the steps, find the longest sequence of transformations required to reach a final dish. Every item has at most one parent (the item it is used to create).
Input: The first line contains N, the number of items. The next N-1 lines contain two strings: 'A B', meaning ingredient A is used to create product B. One item is the final dish (it is not used to create anything).
Output: The integer length of the longest chain of ingredients leading to the final dish (number of items in the chain).
Example:
Input:
4
Flour Dough
Dough Bread
Wheat Flour
Output:
4
Key concepts
graphs_dfsmax_depth
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