Time Needed to Inform All Employees

MediumTreesdfs

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

A company has n employees with IDs 0 to n - 1. The head of the company is headID. For every employee i, manager[i] is their direct manager; manager[headID] = -1. The reporting structure is guaranteed to form a tree. The head wants to share urgent news. Once employee i learns it, they spend informTime[i] minutes to tell all of their direct reports at once, who then start passing it on in the same way. Employees with no reports have informTime[i] = 0. Return the number of minutes until every employee has been informed.

Examples

Input: n = 7, headID = 0, manager = [-1,0,0,1,1,2,2], informTime = [2,3,6,0,0,0,0]
Output: 8
News reaches employees 1 and 2 after 2 minutes. Employee 2 needs 6 more minutes to reach 5 and 6, finishing at minute 8.
Input: n = 4, headID = 3, manager = [3,3,3,-1], informTime = [0,0,0,5]
Output: 5
The head informs all three reports together in 5 minutes.

Constraints

  • 1 <= n <= 10^5
  • 0 <= headID < n
  • manager.length == n and informTime.length == n
  • 0 <= informTime[i] <= 1000; informTime[i] == 0 if employee i has no reports
  • The management structure is a tree rooted at headID

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More dfs problems