Data Structures & AlgorithmsHardAccenture · Infosys OA

Problem: Which is the correct worst-case time complexity for inserting into a balanced BST (like a Red-Black or AVL tree) with n nodes, and why is it better than a plain (unbalanced) BST?

  1. A. O(n log n), since every insertion re-sorts all elements
  2. B. O(n) for both, since insertion always rebuilds the tree
  3. C. O(1), since balanced trees skip comparisons entirely
  4. D. O(log n), because self-balancing rotations keep the tree height at O(log n); a plain BST can degrade to a O(n)-height chain in the worst case (e.g. inserting sorted data)

Correct answer: D. O(log n), because self-balancing rotations keep the tree height at O(log n); a plain BST can degrade to a O(n)-height chain in the worst case (e.g. inserting sorted data)

A balanced BST actively maintains height O(log n) via rotations after insertion/deletion, keeping all operations O(log n); an unbalanced BST has no such guarantee and can become a linked list in height (O(n)) if, say, sorted data is inserted in order.

More Coding Interview Problems questions

Exams that ask this topic