Data Structures & AlgorithmsVery hardGoogle · Flipkart interview
A COMPLETE binary tree has n nodes. Counting them by comparing the leftmost and rightmost depths at each node and recursing into only one subtree takes how long?
- A. O(log² n)
- B. O(log n)
- C. O(n)
- D. O(n log n)
Correct answer: A. O(log² n)
Each depth check walks O(log n) edges, and at every level one of the two subtrees is perfect (its size is 2^h − 1 by formula), so only one recursive call continues. That is O(log n) levels × O(log n) work = O(log² n).
More Coding Interview Problems questions
- Problem: Rebuild a BST from its preorder traversal (using value bounds) and print its postorder. python def postfrompre(pre): i, out = 0…
- Problem: For every node of a tree, compute the sum of distances to all other nodes in O(n) with re-rooting: ans[child] = ans[parent] −…
- Problem: Walk along buildings; climbing up by d costs d bricks or one ladder (going down is free). Use ladders on the largest climbs (a…
- Problem: Return the k pairs (u from a, v from b) with the smallest sums, using a heap seeded with (a[i], b[0]). python import heapq def…
- Problem: Median of two sorted arrays in O(log(min(m, n))) by binary searching the partition of the shorter array. python def median(a, b)…
- Problem: Find the k-th positive integer missing from a strictly increasing array, by binary search on 'missing before index i' = a[i] − (i…