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?
- A. O(n log n), since every insertion re-sorts all elements
- B. O(n) for both, since insertion always rebuilds the tree
- C. O(1), since balanced trees skip comparisons entirely
- 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
- Problem: What is the time complexity of the standard recursive merge sort, including the merge step, expressed via its recurrence T(n) =…
- Problem: Which statement about the time complexity of checking if two strings are anagrams is most efficient and correct?
- Problem: For the 'minimum number of platforms needed for a railway station given arrival and departure times' problem, what is the…
- Problem: Which correctly describes why a hash set gives O(1) average-case membership testing, but a balanced BST set gives O(log n)?
- Problem: Which of these correctly explains why BFS (not DFS) is used to find the shortest path in an unweighted graph?
- Problem: What is the time complexity of detecting a cycle in an undirected graph using Union-Find, processing E edges?