← Questions

Manager and peer relationships with is_manager queries

0
GoogleCodingLast asked Feb 2026

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 — a is the direct manager of b.
  • add_peer(a, b) -> None — a and b have the same direct manager, who may not be known yet.
  • is_manager(a, b) -> bool — whether a is a direct or indirect manager of b. 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_peer transitive (peers of peers are peers)?
  • Which matters more, fast statements or fast is_manager queries?
  • 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_manager walks up from b through 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_manager in near constant time when queries far outnumber statements.
  • Return the full chain of managers from b to 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

union-find · trees · hash maps

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