Data Structures & AlgorithmsModerateTCS NQT · GATE CS
Problem: A complete binary tree has all levels full with depths 0 to 4 (root at depth 0). How many nodes does it have in total?
- A. 16
- B. 63
- C. 32
- D. 31
Correct answer: D. 31
Levels 0..4 hold 1 + 2 + 4 + 8 + 16 = 2⁵ − 1 = 31 nodes. 16 counts only the bottom level, 63 adds a depth-5 level, and 2⁵ = 32 forgets the − 1.
More Coding Interview Problems questions
- Compute the sum of gcd(i, j) over all pairs 1 ≤ i < j ≤ 50. python from math import gcd print(sum(gcd(i,j) for i in range(1,51) for j in…
- How many integers n with 1 ≤ n ≤ 10000 have exactly four positive divisors? Count them with a divisor-count sieve. python N=10000…
- Array a = [3, 8, 2, 9, 5, 7, 6]. Count the index triples i < j < k such that the three values, sorted, are equally spaced (an arithmetic…
- Count the binary strings of length 10 that contain no two adjacent 1s and also contain a run of at least three consecutive 0s. Brute-force…
- Count the pairs (a, b) with 1 ≤ a < b ≤ 20 such that a OR b (bitwise) equals 31. Run the code. python print(sum(1 for a in range(1,21) for…
- Floyd–Warshall is run on the 4-vertex directed weighted graph with matrix rows [0,3,∞,7], [8,0,2,∞], [5,∞,0,1], [2,∞,∞,0]. Sum the final…