Panda Guru LogoPanda
Guru

Google L3 Phone screen Problems

Round 1

Questions: 1️⃣ Binary Tree Root (No Color Constraints):
Given an undirected acyclic connected graph where each node has at most 3 edges, find a node which, when made the root, converts the graph into a binary tree.
Approach: Just return any node with degree less than 3.

2️⃣ Binary Tree with Alternating Colors:
Given an undirected connected acyclic graph where each node has at most 3 edges, and nodes are colored Black or White, find a root such that:

3️⃣ Binary Tree with Fixed Color Order (R, B, W):
Given an undirected connected acyclic graph where each node has at most 3 edges, and nodes are colored Red (R), Blue (B), or White (W), find a root such that:

  1. Understanding Parent and Child Colors:
    • The color sequence (RBWRBW) implies that for any node with a given color, we know the required color for its parent and children:
      • If the node is Red (R), its parent must be White (W), and its children must be Blue (B).
      • If the node is Blue (B), its parent must be Red (R), and its children must be White (W).
      • If the node is White (W), its parent must be Blue (B), and its children must be Red (R).
  2. Candidate Root Identification:
    • For a node to be a valid root, it should not have a neighbor with the color required for its parent (since a root node has no parent).
    • Conversely, the other neighbors of this node must align with the colors of its children.
    • A candidate root is any node that lacks a neighbor with the required parent color.
  3. Uniqueness of Root:
    • If there are multiple nodes that qualify as candidate roots (i.e., more than one node without a parent color neighbor), it is impossible to construct a valid binary tree with the given color order. This is because:
      • Suppose one of these nodes is chosen as the root. The other candidate root would inevitably need a parent, but its parent wouldn’t have the required parent color, violating the color sequence constraint.
      • Therefore, there must be exactly one root with this property. If more than one candidate root exists, return -1.

Intuition Behind the Approach:

Candidate's Approach

Explained and coded all 3 problems within 40 minutes yesterday and got a call from the recruiter today that I have cleared the round.

Interviewer's Feedback

No feedback provided.