FastPrepCheck a Complete Binary Tree
Problems/Check a Complete Binary Tree

Check a Complete Binary Tree

Goldman Sachs logoGoldman Sachs● MediumFULLTIMEPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: The report tentatively identifies a complete-binary-tree variant and a BFS solution; the exact variant and B-tree follow-ups were not retained. The conventional judged task is an approximately 70% match.

Given the root of a binary tree, return whether it is complete: every level before the last is full, and the nodes on the last level occupy the leftmost positions with no gap.

For this exercise, assume an empty tree is complete. Node values do not affect completeness. The input uses the conventional level-order tree serialization: null marks a missing child, redundant trailing nulls are ignored, and [] is the empty tree.

Interview context

The report tentatively compares the phone-screen task to a variant of checking a complete binary tree and says BFS was used. The exact variant was not retained. This practice task uses the conventional completeness definition above. Later discussion moved toward B-trees and database indexes; no specific B-tree operation is imposed on this binary-tree function.

Function

isCompleteTree(root: TreeNode) → boolean

Examples

Example 1

root = [1,2,3,4,5,6]return = true

The first two levels are full. The three nodes on the last level occupy its first three positions, so the tree is complete.

Example 2

root = [1,2,3,4,5,null,7]return = false

Node 3 has no left child but has right child 7. That occupied position follows a gap, so the tree is not complete.

Example 3

root = []return = true

The empty tree is complete under the stated convention.

Constraints

  • For this exercise, assume the serialized root contains between 0 and 2000 entries after redundant trailing null markers are removed, including interior null markers.
  • Each non-null node value is an integer between -1000000000 and 1000000000.
  • The serialization represents a valid finite binary tree; values may repeat.

More Goldman Sachs problems

  • Palindrome CounterOA · Seen Oct 2026
  • Longest Palindromic SubsequenceOA · Seen Oct 2026
  • Best Average Student ScorePHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
All Goldman Sachs interview questionsSee Goldman Sachs hiring insights
Saved
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public boolean isCompleteTree(TreeNode root) {
        // Write your code here.
    }
}
Checking account…