Data Structures & AlgorithmsHardProduct company OA

A recursive segment tree is built over an array of n = 6 elements (each internal node splits its range into two halves; leaves are single elements). How many nodes does it have?

  1. A. 12
  2. B. 16
  3. C. 7
  4. D. 11

Correct answer: D. 11

Every internal node has exactly two children, so it is a full binary tree with n = 6 leaves and n − 1 = 5 internal nodes: 2n − 1 = 11. The ARRAY used to store it is usually sized 4n (or 2·next power of two = 16), but most of those slots are unused.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic