Question 51
MediumWhat is the time complexity of converting a sorted array of n elements into a balanced BST?
- A.
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Data Structures and Algorithms
What is the time complexity of converting a sorted array of n elements into a balanced BST?
O(log n)
O(n log n)
O(n²)
O(n)
Sign up free to see the answer and a step-by-step explanation.
Which operation is simpler in a BST than in a hash table?
Finding all keys in a range, in order
Exact-match lookup on average
Insertion on average
Deletion on average
Sign up free to see the answer and a step-by-step explanation.
How many different BSTs can be formed from the keys 1, 2, 3, 4?
24
10
16
14
Sign up free to see the answer and a step-by-step explanation.
Which is true for the minimum key in a BST?
It is always a right child
It is found by following left children from the root until there is none
It is always the root
It needs a full traversal
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
class Node:
def __init__(self, v, l=None, r=None):
self.v, self.l, self.r = v, l, r
def valid(n, lo=float('-inf'), hi=float('inf')):
if not n:
return True
if not lo < n.v < hi:
return False
return valid(n.l, lo, n.v) and valid(n.r, n.v, hi)
good = Node(50, Node(30, Node(20), Node(40)), Node(70))
bad = Node(50, Node(30, Node(20), Node(60)), Node(70))
print(valid(good), valid(bad))
False False
True False
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
class Node:
def __init__(self, v, l=None, r=None):
self.v, self.l, self.r = v, l, r
def insert(n, v):
if not n:
return Node(v)
if v < n.v:
n.l = insert(n.l, v)
else:
n.r = insert(n.r, v)
return n
root = None
for v in [50, 30, 70, 20, 40, 60, 80]:
root = insert(root, v)
def lca(n, a, b):
while n:
if a < n.v and b < n.v:
n = n.l
elif a > n.v and b > n.v:
n = n.r
else:
return n.v
print(lca(root, 20, 40))
40
20
Sign up free to see the answer and a step-by-step explanation.
A BST is built by inserting 10, 5, 15, 3, 7, 12, 18. What is its postorder traversal?
3 7 5 12 18 15 10
3 5 7 10 12 15 18
3 7 12 18 5 15 10
10 5 3 7 15 12 18
Sign up free to see the answer and a step-by-step explanation.
What is the maximum height of an AVL tree with n nodes?
n
About 1.44 log₂ n, i.e. O(log n)
√n
log₂ n exactly
Sign up free to see the answer and a step-by-step explanation.
Which property does a red-black tree guarantee?
Every node has two children
All leaves are at the same depth
The tree is complete
The longest root-to-leaf path is at most twice the shortest
Sign up free to see the answer and a step-by-step explanation.
Given only the preorder traversal of a BST with distinct keys, can the tree be reconstructed uniquely?
Only if it is balanced
No, a second traversal is always required
Yes, because the inorder traversal is just the sorted keys
Only for 3 nodes or fewer
Sign up free to see the answer and a step-by-step explanation.
True True
False True
30
50