This is a verified interview question from De-shaw. Candidates reporting seeing this problem in recent Online Assessments (OAs) and onsite rounds. Mastering "Minimum Time to Complete Tasks in a Tree - De Shaw MNIT Jaipur" covers key patterns like Graphs.
"You are given a rooted tree consisting of N vertices numbered from 1 to N, where vertex 1 is the root. Some vertices contain tasks that must be completed. Whenever you visit a vertex containing a task, the task is completed instantly. You start at the root (vertex 1) and must finish all tasks. After completing every task, you must end your journey at a given vertex T. Moving along any edge takes 1 second. Find the minimum time required. ### Input Format * The first line contains two integers N and K — the number of vertices and the number of task vertices. * The next N - 1 lines each contain two integers u and v, denoting an edge of the tree. * The next line contains K distinct integers denoting the vertices containing tasks. * The last line contains an integer T, the required ending vertex. ### Output Format Print a single integer — the minimum time required. Explanation The tasks are located at vertices 4 and 6. One optimal route is: 1 → 2 → 4 → 2 → 1 → 3 → 6 → 3 → 7 All tasks are completed, and the journey ends at vertex 7. The total time taken is 8 seconds."
Join thousands of developers practicing for De-shaw.