Skip to main content
All Questions

Find a valid root for a tree-shaped graph.

Unlock detailed company stats for this questionUpgrade

Given a connected acyclic graph where every node has at most 3 neighbours, find a valid node that forms a binary tree as root. Follow-ups, about 10 minutes each: 1) nodes are black or white and colors must alternate at every depth; 2) the graph can be disconnected; 3) nodes are red, black or white and depths must follow the sequence RWB RWB..., root of any color. The final follow-up has an O(n) solution proving only one unique root is possible.

Expect follow-ups like

  • Now every node is either black or white, and at every depth in the tree colors must alternate. Find a valid node.
  • Now the graph can be disconnected.
  • Now every node is red, black or white, and the only allowed depth sequence is RWB RWB..., with the root of any color. Find a valid root.

Format · 45-minute DSA round; solve, code and discuss complexity per follow-up.

Community answers

No answers contributed by the community yet.

Related courses

Course

Software Engineering Interview Prep

Land your dream software engineering role at Google, Amazon, Microsoft, Meta, Apple, and other top companies. Learn from mock interviews, frameworks, and advice from senior candidates—practice data structures, algorithms, system design, people management, behavioral interviews, and more.

Course

System Design Interviews

Learn how to answer the latest system design questions across product, infrastructure, and AI domains. Features in-depth video examples, written breakdowns, and helpful reference patterns. Watch senior engineers and managers answer these questions in mock interview videos.