Google | Phone | Time to reach message to all the employees
Anonymous User
2894

There is a company consisting of N people, each with a manager who may have one or more other reports.

At the top of the company’s hierarchy is the CEO, who emails a message to all of his reports, asking them to forward the message to all of their reports.

Assuming every manager takes their own characteristic amount of time to forward the message to their reports, how long does it take for the message to get to everyone in the company?

Example Hierarchy:
CEO(t = 0)

├──Manager1 (t=3)
│ ├──Employee1 (path sum 3)

├──Manager2 (t=4)
│ ├──Manager3 (t=2)
│ ├──Employee2

├──Employee3
Result: 6

My solution
enum PersonType {
	EMPLOYEE, MANAGER
}

class Person {
	int time;
	PersonType personType;
	List<Person> reports;

	public Person(int time, PersonType type) {
		this.time = time;
		this.personType = type;
		reports = new ArrayList<>();
	}
}

public int getMaxMessageTime(Person root) {
	if(root == null || root.reports.size() == 0) {
		return 0;
	}
	int thisMax = 0;
	for(Person person : root.reports) {
		thisMax = Math.max(thisMax, helper(person));
	}
	return thisMax + root.time;
}
Comments (3)