300 Original Practice Questions 2 versions
1. A program stores a list of student names and needs to insert a new
name at the beginning frequently. Which data structure is most
efficient for this requirement?
A. Array-based list
B. Singly-linked list
C. Static array
D. Hash table
B. Singly-linked list
Prepending to a singly-linked list requires only updating the head pointer and
the new node's next reference, which is O(1). Inserting at the beginning of an
array-based list requires shifting all existing elements, which is O(n) .
2. A queue is implemented using a circular array with a maximum
capacity of 8. The front pointer is at index 6 and the length is 3. At
which index is the next element enqueued?
A. Index 0
B. Index 1
C. Index 2
D. Index 7
B. Index 1
In a circular array queue, the next enqueue position is (front + length) %
capacity = (6 + 3) % 8 = 9 % 8 = 1 .
,*3. What is the result of evaluating the postfix expression "5 3 + 2 "?
A. 16
B. 13
C. 11
D. 10
A. 16
Postfix evaluation: push 5, push 3, encounter + so pop 3 and 5, compute
5+3=8, push 8. Push 2. Encounter * so pop 2 and 8, compute 82=16 .*
4. Which traversal of a binary search tree visits nodes in descending
sorted order if the right subtree is visited before the left?
A. Inorder
B. Reverse inorder
C. Preorder
D. Postorder
B. Reverse inorder
Reverse inorder traversal visits the right subtree, then the root, then the left
subtree. For a BST, this produces keys in descending order .
5. A binary search tree contains keys 40, 20, 60, 10, 30, 50, 70. Which
key is the successor of 30?
A. 20
B. 40
C. 50
D. 60
B. 40
,The successor of a node is the next larger key. In this BST, 30's successor is 40,
its parent, since 30 has no right child.
6. What is the height of a complete binary tree with 15 nodes?
A. 2
B. 3
C. 4
D. 5
B. 3
A complete binary tree with 15 nodes has 4 levels (depth 0 to 3), and height
is the largest depth, which is 3.
7. In an array-based heap with 10 elements, what is the index of the
last internal (non-leaf) node?
A. 3
B. 4
C. 5
D. 6
B. 4
For a heap with n elements stored in an array, the last internal node is at
index ⌊n/2⌋ - 1 = ⌊10/2⌋ - 1 = 4 .
8. What is the primary purpose of a hash function in a hash table?
A. To sort keys in ascending order
B. To map keys to array indices
C. To compare keys for equality
D. To encrypt keys for security
, B. To map keys to array indices
A hash function computes an index from a key, distributing keys across the
array. Sorting, comparison, and encryption are separate concerns .
9. A hash table has 100 buckets and stores 75 items. What is the load
factor?
A. 0.25
B. 0.75
C. 1.33
D. 75
B. 0.75
Load factor = number of items / number of buckets = = 0.75 .
10. Which collision resolution method stores colliding items in a linked
list at the same index?
A. Linear probing
B. Quadratic probing
C. Chaining
D. Double hashing
C. Chaining
Chaining resolves collisions by maintaining a linked list (or other container)
at each array index for all items that hash to that index. Linear probing,
quadratic probing, and double hashing are open addressing methods .
11. In linear probing, what happens when a collision occurs?
A. The item is stored in a linked list
B. The table is resized