Google Interview Question
Software EngineersCountry: United States
Interview Type: In-Person
class Employee {
int id;
private String name;
//...other personal information
private Employee manager;
private List<Employee> subordinates; //direct subordinates
public Employee(int id, String name) {
this.id = id;
this.name = name;
subordinates = new ArrayList<>();
}
boolean isManager(Employee manager) {
Employee upperLevel = this.manager;
while(upperLevel != null && upperLevel != manager) {
upperLevel = upperLevel.manager;
}
return upperLevel.manager == manager;
}
void beColleague(Employee p) {
p.setManager(this.manager);
}
void setManager(Employee m) {
if(manager != null) { //remove from subordinate's list of current manager
manager.deleteSubordinate(this);
}
manager = m;
m.addSubordinate(this); //add to new manager's subordinate's list
}
private void deleteSubordinate(Employee m) {
subordinates.remove(m);
}
private void addSubordinate(Employee m) {
subordinates.add(m);
}
}
public class Employment {
private Employee admin;
private Map<Integer, Employee> employees; //id: employee
public Employment() {
admin = new Employee(0, "ADMINISTRATOR"); //root of the employment tree, the highest level supervisor
employees = new HashMap<>();
employees.put(0, admin);
}
public void assignManager(int p1, int p2) {
employees.get(p2).setManager(employees.get(p1));
}
public void beColleague(int p1, int p2) {
employees.get(p2).beColleague(employees.get(p1));
}
public boolean isManager(int p1, int p2) {
return employees.get(p2).isManager(employees.get(p1));
}
//public void addEmployee(int p);
//public void deleteEmployee(int p);
}
Looking for interview experience sharing and coaching?
Visit aonecode.com for private lessons by FB, Google and Uber engineers
Our ONE TO ONE class offers
SYSTEM DESIGN Courses (highly recommended for candidates for FLAG & U)
ALGORITHMS (conquer DP, Greedy, Graph, Advanced Algos & Clean Coding),
latest interview questions sorted by companies,
mock interviews.
Our students got hired from G, U, FB, Amazon, LinkedIn and other top tier companies after weeks of training.
Feel free to email us aonecoding@gmail.com with any questions. Thanks!
Hello!!! I have been reading the code and I am confused with the function
void beColleague(Employee p);
Why is it basically a renaming of setManager?? Shouldn't be something like
void beColleague(Employee p) {
if (manager == p) { // p is our manager
manager = p.manager;
p.deleteSubordinate(this);
}
else if (p.manager == this) { // we are the manager of p
p.manager = manager;
deleteSubordinate(p);
}
}
Cheers :)
I assume that a manager can have their manager too, and that the hierarchy is acyclic (i.e. you can't be a manager of an upper of yours).
class Employee {
public:
Employee(int id)
{
id_ = id;
manager_ = NULL;
}
Employee *manager_;
unordered_set<Employee *> subordinates_;
int id_;
};
class Employment {
public:
void AssignManager(Employee *manager, Employee *subordinate)
{
if (manager &&
subordinate)
{
if (subordinate->manager_) {
subordinate->manager_->subordinates_.erase(subordinate);
}
subordinate->manager_ = manager;
subordinate->manager_->subordinates_.insert(subordinate);
}
}
void BeColleagues(Employee *e1, Employee *e2)
{
if (e1 &&
e2 &&
e1->manager_ != e2->manager_)
{
if (e1->manager_ == NULL) {
AssignManager(e2->manager_, e1);
} else if (e2->manager_ == NULL) {
AssignManager(e1->manager_, e2);
} else {
if (HierarchyLevel(e1) < HierarchyLevel(e2)) {
AssignManager(e1->manager_, e2);
} else {
AssignManager(e2->manager_, e1);
}
}
}
}
bool IsManager(Employee const *manager, Employee const *subordinate) const
{
if (manager &&
subordinate)
{
for (Employee const *upper = subordinate->manager_; upper != NULL; upper = upper->manager_) {
if (upper == manager) {
return true;
}
}
}
return false;
}
private:
int HierarchyLevel(Employee const *e) const
{
int level = 0;
if (e) {
for (Employee const *upper = e->manager_; upper != NULL; upper = upper->manager_) {
++level;
}
}
return level;
}
};
Hi Fernando,
- aonecoding August 06, 2017I interpret the problem this way - making someone my colleague means having him into my team (assigning my manager to him). Since it is a design problem it's open-ended and your solution would be good as well.
Thanks for the reply!