← Google Interview Insights

Google·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
Apr 2026

Summary

Google SWE coding round, one problem, tree traversal. Pretty standard stuff but I fumbled around more than I should have before landing on the right approach.

Questions Asked (1)

Q1

Given a binary tree, return the node values visible from the right side (i.e., the rightmost node at each level).

Algorithms & Data Structures
Author's notes

I knew it was a level-order traversal thing pretty quickly, but I wasted maybe two minutes half-explaining DFS before catching myself.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a level-order traversal (BFS) with a queue, and for each level, record the last node's value. Alternatively, use DFS with depth tracking, prioritizing the right child, and record the first node encountered at each depth. Both approaches yield O(n) time and O(h) space (where h is the height for DFS, or O(w) for BFS where w is max width).

Pro tip: Clarify with the interviewer whether the tree can be empty or have only one node, and discuss the trade-offs between BFS and DFS in terms of space complexity and implementation simplicity. Mention that BFS uses a queue and processes level by level, while DFS uses recursion (or stack) and can be more space-efficient for skewed trees.

1. Understand the problem

Confirm that the right side view includes the rightmost node at each depth, even if it's not the right child of its parent. Clarify edge cases: empty tree, single node, skewed tree.

2. Choose an approach

Decide between BFS (level-order traversal) and DFS (pre-order with right-first). BFS is intuitive: process each level and take the last node. DFS is elegant: track depth and record the first node seen at each depth when traversing right-first.

3. Implement the algorithm

For BFS: use a queue, for each level, iterate through all nodes, and after the loop, add the last node's value to the result. For DFS: use recursion with a depth parameter, traverse right child first, and if depth equals result size, add node's value.

4. Analyze complexity

State time complexity O(n) since each node is visited once. Space complexity: BFS O(w) where w is maximum width, DFS O(h) where h is height (due to recursion stack).

5. Test with examples

Walk through a sample tree (e.g., [1,2,3,null,5,null,4]) to verify the output [1,3,4]. Also test edge cases: empty tree returns [], single node returns [value].

Key Points to Mention

  • Level-order traversal (BFS) using a queue to process nodes level by level.
  • DFS with depth tracking and right-first traversal to capture the rightmost node at each depth.
  • Time complexity O(n) and space complexity O(h) for DFS or O(w) for BFS.
  • Handling edge cases: empty tree, single node, skewed trees.
  • Trade-offs between BFS and DFS: BFS may use more memory for wide trees, DFS may use more for deep trees.
  • The right side view is not simply the right child of each node; it's the rightmost node at each level.

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