← Google Interview Insights

Google·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Apr 2026

Summary

Google coding interview, graph problem that looks straightforward until you actually try to enumerate everything. Not a lot of context to go on but the problem itself is the kind that trips people up if they haven't seen it before.

Questions Asked (1)

Q1

Given a directed acyclic graph, find all possible topological orderings of its nodes.

Algorithms & Data Structures
Author's notes

My first instinct was to just run Kahn's algorithm and call it done.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify that enumerating all topological orderings is exponential in the worst case, then present a backtracking algorithm that repeatedly selects any node with in-degree zero, recurses, and backtracks. Discuss complexity and optimizations like using a priority queue for lexicographic order or pruning based on constraints.

Pro tip: Mention that the number of topological orderings can be exponential (e.g., an independent set of n nodes has n! orderings), so you should ask whether the interviewer wants all orderings or just a count, and if the graph is large, propose a randomized sampling approach.

1. Clarify requirements and constraints

Ask whether the graph is guaranteed acyclic, whether nodes are labeled, and if the output should be all orderings or just a count. Also discuss input size limits to choose between exact enumeration and approximation.

2. Explain the backtracking algorithm

Describe maintaining in-degree counts and a set of available nodes (in-degree zero). At each step, pick an available node, add it to the current ordering, decrement in-degrees of its neighbors, and recurse; then backtrack.

3. Analyze complexity and optimizations

State that the time complexity is O(V+E) per ordering and O(V! ) in the worst case. Mention optimizations: using a priority queue for lexicographic order, pruning if only a subset is needed, or using DP for counting.

4. Discuss alternative approaches

For counting only, mention DP over subsets (O(2^V * V)) or inclusion-exclusion. For sampling, mention random topological orderings via random selection of available nodes.

5. Test with examples and edge cases

Walk through a small DAG (e.g., 3 nodes) to demonstrate the algorithm. Discuss edge cases: empty graph, single node, disconnected components, and graphs with multiple valid orderings.

Key Points to Mention

  • Topological ordering definition and relationship to DAGs
  • Backtracking with in-degree tracking
  • Exponential worst-case complexity and why
  • Optimizations for lexicographic order or counting
  • Handling disconnected graphs and multiple components
  • Real-world applications (e.g., build systems, task scheduling)

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