← Questions
Manager and peer relationships with is_manager queries
0
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Google's first round (coding), last asked Feb 2026. It tests union-find, trees and hash maps.
Build an in-memory org chart that learns relationships between employees one statement at a time and answers whether one employee is above another. Statements and questions arrive interleaved, and every answer must use everything learned so far, including what can be inferred.
Interface
Unless you agree on a different interface with the interviewer, assume:
-
add_manager(a, b) -> None—ais the direct manager ofb. -
add_peer(a, b) -> None—aandbhave the same direct manager, who may not be known yet. -
is_manager(a, b) -> bool— whetherais a direct or indirect manager ofb. Nobody is their own manager.
Example
add_manager("ana", "ben")
add_peer("ben", "cy")
is_manager("ana", "cy") -> True # cy shares ben's manager
add_peer("dee", "eve") # their manager is not known yet
is_manager("ben", "eve") -> False
add_manager("ben", "dee") # now eve reports to ben too
is_manager("ben", "eve") -> True
is_manager("ana", "eve") -> True # ana -> ben -> eve
is_manager("eve", "ana") -> False
Constraints
- Up to 20,000 operations and 10,000 distinct employees.
- The statements are consistent and never form a cycle. Repeating a statement is allowed.
Clarifying questions
- Can a peer group learn its manager after its members were declared peers, and can two peer groups merge?
- Is
add_peertransitive (peers of peers are peers)? - Which matters more, fast statements or fast
is_managerqueries? - What should happen on a contradiction, for example a second, different manager for the same person?
What a strong answer covers
- Union-find over peer groups, with the direct manager stored on the group's root, so a manager learned later applies to every member at once.
-
is_managerwalks up frombthrough each group's manager, in time proportional to the depth of the chart. - Merging two groups keeps whichever manager is known.
- Edge cases: unknown employees, self-queries, repeated statements, peers declared before anyone has a manager.
- Tests built from small charts where the inference only works through a peer link.
Follow-ups
- Answer
is_managerin near constant time when queries far outnumber statements. - Return the full chain of managers from
bto the top. - Detect and report a statement that contradicts what is already known.
Where it's asked
- Google · First round: coding · Last asked Feb 202645 min · Coding in a Google Doc, no syntax highlighting or autocomplete · No AI
What it tests
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding
