Question 1
EasyIn a binary search tree, where are values smaller than a node stored?
- 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
In a binary search tree, where are values smaller than a node stored?
In its left subtree
In its right subtree
In its parent
Anywhere
Answer: A
Left subtree < node < right subtree, at every node.
Where is the minimum value of a BST?
At the root
At any leaf
At the leftmost node
At the rightmost node
Answer: C
Keep following left children until there are none.
What is the worst-case time complexity of searching an unbalanced BST?
O(1)
O(n)
O(log n)
O(n log n)
Answer: B
Inserting sorted data creates a chain (a skewed tree) of height n.
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)
n = root
while n.l:
n = n.l
print(n.v)
20
50
Answer: A
The minimum is found by going left from the root: 50 → 30 → 20.
Inserting the values 1, 2, 3, 4, 5 in this order into an empty BST produces what shape?
A right-skewed chain
A left-skewed chain
A balanced tree
A complete binary tree
Answer: A
Each new value is larger than all before it, so it always goes to the right.
Which of these is a self-balancing binary search tree?
Binary heap
AVL tree
Trie
Segment tree
Answer: B
AVL and red-black trees rotate after insertions and deletions to keep their height O(log n).
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 find(n, v):
while n and n.v != v:
n = n.l if v < n.v else n.r
return n is not None
print(find(root, 60))
False
True
Answer: B
Searching for 60: 60 > 50 goes right to 70, 60 < 70 goes left to 60 — found.
Which tree traversal of a valid Binary Search Tree (BST) produces nodes in strictly ascending (sorted) order?
Pre-order Traversal
In-order Traversal
Post-order Traversal
Level-order Traversal
Answer: B
In a BST, for every node , all keys in the left subtree are smaller than , and all keys in the right subtree are greater than .
An In-order traversal visits: , which naturally yields keys in ascending order.
The values 50, 30, 70, 20, 40 are inserted in that order into an empty binary search tree. Which value is at the root?
20
70
30
50
Answer: D
The first value inserted becomes the root and never moves.
The values 50, 30, 70, 20, 40 are inserted in that order into an empty binary search tree. What is the left child of 30?
20
40
None
50
Answer: A
20 < 50 goes left to 30, then 20 < 30 goes to 30's left.
80
30
None
Error