← Google Interview Insights

Google·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
May 2026

Summary

Google SWE interview with a graph traversal problem involving horse pedigrees. Basically a DAG ancestry question dressed up in a fun theme, but don't let the theme fool you into thinking it's easy.

Questions Asked (1)

Q1

Given a set of parent-child relationships between horses and two horse identifiers, determine whether the two horses are biologically related. Two horses count as related if one is an ancestor of the other, or if they share any common ancestor.

Algorithms & Data Structures
Author's notes

The horse theme threw me off for a second but once I drew it out it's just a DAG reachability problem.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the relationships as a directed graph where edges point from parent to child, then determine if the two horses are connected in the underlying undirected graph. Use BFS/DFS from one horse to see if the other is reachable, or compute connected components to answer multiple queries efficiently.

Pro tip: Clarify whether the relationship is directed (ancestor/descendant) or undirected (shared ancestor) — the problem states both count, so treat it as undirected connectivity. Also discuss handling multiple queries by precomputing connected components or using union-find for near-constant time lookups.

1. Clarify the problem

Confirm that 'related' includes both direct ancestry and shared ancestors, meaning any path in the undirected graph. Ask about input size, number of queries, and whether the graph is a forest (each horse has at most one parent).

2. Choose data structures

Represent the parent-child relationships as an adjacency list for a graph. If multiple queries, consider building a union-find structure or precomputing connected components.

3. Design the algorithm

For a single query, perform BFS/DFS from one horse to check if the other is reachable. For multiple queries, use union-find to group horses into connected components, then answer each query by checking if both horses belong to the same component.

4. Analyze complexity

For BFS/DFS: O(V+E) per query. For union-find: O(α(N)) per query after O(N) preprocessing. Discuss trade-offs and choose based on query frequency.

5. Handle edge cases

Consider cases where horses are the same, no relationship exists, or the graph has cycles (though typically it's a forest). Ensure the solution works for disconnected components.

Key Points to Mention

  • Graph representation: adjacency list from parent to child, but treat as undirected for connectivity.
  • BFS/DFS for single query: O(V+E) time, O(V) space.
  • Union-Find (Disjoint Set Union) for multiple queries: near O(1) per query after O(N) preprocessing.
  • Connected components: precompute components to answer queries in O(1) time.
  • Edge cases: same horse, no common ancestor, multiple roots, cycles (if any).
  • Complexity analysis and trade-offs between approaches.

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