← Google Interview Insights

Google·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
May 2026

Summary

Google coding screen for a software engineer role. One tree problem, pretty focused session. I spent a decent chunk of time just asking clarifying questions before writing a single line.

Questions Asked (1)

Q1

Given a list of edges representing a tree, write a function that takes two node values as input and returns the distance between them.

Algorithms & Data Structures
Author's notes

I front-loaded a bunch of clarifying questions before touching the code: can edges repeat, is the input guaranteed to actually form a valid tree, are both nodes guaranteed to exist, what if they're the same node.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify that the tree is unrooted and edges are undirected, then choose an approach: either BFS from one node to find the other (O(N) time) or preprocess for LCA to answer multiple queries efficiently. For a single query, BFS is simpler; for multiple queries, LCA with binary lifting is better.

Pro tip: Mention that if the tree is large and multiple queries are expected, preprocessing for LCA is worth the O(N log N) setup, but for a single query BFS is optimal. Also, discuss handling edge cases like when the two nodes are the same or when one is an ancestor of the other.

1. Clarify the problem

Ask whether the tree is rooted or unrooted, if multiple queries will be made, and the expected input size. Confirm that edges are undirected and node values are unique.

2. Choose an approach

For a single query, use BFS from one node to find the other, tracking distances. For multiple queries, preprocess the tree to answer LCA queries in O(1) or O(log N) after O(N log N) preprocessing.

3. Implement the algorithm

If using BFS: build an adjacency list, perform BFS from the start node, and return the distance when the target is found. If using LCA: root the tree, compute depths and binary lifting table, then distance = depth[u] + depth[v] - 2*depth[lca(u,v)].

4. Analyze complexity

State time and space complexity: BFS is O(N) time and O(N) space; LCA preprocessing is O(N log N) time and space, with O(log N) per query (or O(1) with Euler tour + RMQ).

5. Test and handle edge cases

Test with small trees, same node, adjacent nodes, and nodes far apart. Discuss handling of large trees and potential recursion limits if using DFS for LCA preprocessing.

Key Points to Mention

  • Tree properties: unique path between any two nodes, so distance is the number of edges on that path.
  • BFS for single query: O(N) time, simple to implement.
  • LCA approach: preprocess for multiple queries, distance formula using depths.
  • Binary lifting for LCA: O(N log N) preprocessing, O(log N) per query.
  • Edge cases: same node (distance 0), one node is ancestor of the other.
  • Space-time trade-offs and when to choose each approach.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.