← Google Interview Insights

Google·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jun 2026

Summary

Google SWE interview with three coding problems covering trees, graphs, and sliding window. Nothing too wild but the combo felt like a solid mid-level screen.

Questions Asked (3)

Q1

Given a binary tree, return its zigzag level-order traversal (alternating left-to-right and right-to-left by level).

Algorithms & Data Structures
Author's notes

BFS with a flag to flip direction each level.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use BFS with a queue to process the tree level by level, tracking the current level number to determine direction. For each level, collect node values in a list, then reverse the list if the level is odd (or even, depending on 0-indexing) before adding to the result.

Pro tip: Mention that you can avoid reversing the list by using a deque and appending to the front or back based on the direction, which is more efficient. Also, clarify the indexing convention (0-indexed or 1-indexed) to avoid off-by-one errors.

1. Clarify the problem

Confirm the definition of zigzag traversal: first level left-to-right, second right-to-left, third left-to-right, etc. Ask about edge cases like empty tree or single node.

2. Choose BFS with a queue

Explain that level-order traversal naturally fits BFS. Use a queue to process nodes level by level, and a variable to track the current level index.

3. Process each level with direction

For each level, determine the direction based on level parity. Collect node values in a list, then reverse if needed, or use a deque to insert at front/back efficiently.

4. Handle children and next level

While processing nodes, enqueue their left and right children for the next level. Ensure the queue size is captured before processing to separate levels.

5. Analyze complexity and edge cases

State time complexity O(n) and space O(n) for the queue and result. Discuss edge cases: empty tree, skewed tree, and large tree.

Key Points to Mention

  • BFS vs DFS: BFS is more intuitive for level-order traversal, but DFS can also work with level tracking.
  • Use of a queue (or deque) for level-order traversal.
  • Direction alternation based on level parity (e.g., even levels left-to-right, odd levels right-to-left).
  • Efficient reversal: using a deque to avoid O(k) reversal per level, or reversing the list after collection.
  • Time and space complexity: O(n) time, O(n) space (queue and result).
  • Edge cases: empty tree, single node, and unbalanced tree.

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

Q2

Given a list of course prerequisites, determine whether it's possible to finish all courses (i.e., detect if a cycle exists in the dependency graph).

Algorithms & Data Structures
Author's notes

Topological sort via Kahn's algorithm.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the courses and prerequisites as a directed graph, then detect cycles using either DFS with recursion stack or Kahn's topological sort algorithm. Clearly explain the graph representation, the cycle detection method, and analyze time and space complexity.

Pro tip: Mention that this is a classic topological sort problem and that Kahn's algorithm is often preferred in interviews because it's iterative and avoids recursion depth issues. Also, discuss how to handle edge cases like duplicate prerequisites or disconnected components.

1. Clarify and Model the Problem

Confirm that prerequisites form a directed graph where an edge from course A to course B means A must be taken before B. Ask about input format (e.g., number of courses, list of pairs) and constraints.

2. Choose a Cycle Detection Strategy

Decide between DFS with recursion stack or Kahn's algorithm (BFS-based topological sort). Briefly explain the chosen approach and why it's suitable.

3. Implement the Algorithm

Walk through the implementation: for DFS, track visited and recursion stack; for Kahn's, compute in-degrees and process nodes with zero in-degree. Handle disconnected components.

4. Analyze Complexity and Edge Cases

State time complexity O(V+E) and space complexity O(V+E). Discuss edge cases: empty input, self-loops, duplicate edges, and large graphs.

5. Test with Examples

Validate the solution with a simple acyclic case (e.g., 2 courses, 1 prerequisite) and a cyclic case (e.g., 2 courses with mutual prerequisites).

Key Points to Mention

  • Graph representation: adjacency list for efficiency
  • Cycle detection using DFS with recursion stack or Kahn's algorithm
  • Time and space complexity: O(V+E) time, O(V+E) space
  • Handling disconnected components and duplicate edges
  • Topological sort as an alternative perspective
  • Edge cases: empty input, self-loop, large graphs

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

Q3

Find the length of the longest substring containing at most K distinct characters.

Algorithms & Data Structures
Author's notes

Sliding window with a hashmap tracking character counts.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a sliding window with two pointers to maintain a window that contains at most K distinct characters. Expand the right pointer to include new characters, and when the distinct count exceeds K, shrink the window from the left until it's valid again. Keep track of the maximum window length seen.

Pro tip: Clarify edge cases upfront (e.g., K=0, empty string, K >= unique characters) and discuss the time/space complexity trade-offs. Mention that the sliding window approach is optimal for this problem and can be extended to variations like 'at most K distinct' vs 'exactly K distinct'.

1. Understand the problem and constraints

Restate the problem in your own words and ask clarifying questions about input size, character set, and edge cases. Confirm that the substring must be contiguous and that K is a non-negative integer.

2. Choose the right data structure

Use a hash map (dictionary) to count the frequency of characters in the current window. This allows O(1) updates and quick checks of the number of distinct characters.

3. Implement sliding window

Initialize left and right pointers at 0. Expand right, adding characters to the map. While the map size exceeds K, remove characters from the left and increment left. Update the maximum length at each step.

4. Analyze complexity and test

State that the time complexity is O(n) because each character is processed at most twice (once by right, once by left), and space is O(K) for the map. Walk through a small example to verify correctness.

5. Discuss optimizations and variations

Mention that if the character set is small (e.g., ASCII), an array can replace the hash map for faster access. Also, note how to adapt the solution for 'exactly K distinct' by maintaining a window with at most K and at most K-1 distinct characters.

Key Points to Mention

  • Sliding window technique with two pointers
  • Hash map to track character frequencies
  • Time complexity O(n) and space complexity O(K)
  • Handling edge cases: empty string, K=0, K >= unique characters
  • Comparison with brute force O(n^2) approach
  • Extension to 'exactly K distinct' using two sliding windows

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