Google Interview Question for Software Engineers


Country: United States
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
2
of 2 vote

Hi Fernando,

I 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!

- aonecoding August 06, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

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!

- aonecoding August 03, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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 :)

- Fernando August 03, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

mlkmlk
m;l

- Anonymous August 21, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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;
		}
};

- Alex September 01, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

IMO, this question seems to be asking the union-find algorithm, which allows us to identify disjoint-set relationship.

- sec October 10, 2017 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More