solution of week -3
solution of week -3
Solution of Assignment: 3
0
1
2 42
3 23
4 34
5 52
6 46
7 33
8
9
Which one of the following choices gives a possible order in which the
key values could have been inserted in the table?
Answer - (c)
Explanation :
- The sequence (A) doesn’t create the hash table as the element 52
appears before 23 in this sequence.
- The sequence (B) doesn’t create the hash table as the element 33
appears before 46 in this sequence.
-The sequence (C) creates the hash table as 42, 23 and 34 appear before
52 and 33, and 46 appears before 33.
- The sequence (D) doesn’t create the hash table as the element 33
appears before 23 in this sequence.
2. Consider we insert the keys 5, 28, 19, 15, 20, 33, 12, 17, 10 into a hash ta-
ble with collisions resolved by chaining. Let the table have 9 slots, and
let the hash function be h(k) = k mod 9. Let the slots be numbered
as 0, 1, ..., 8. Then which of the following is FALSE ?
1
(a) 0th slot has no keys
(b) 1st slot has 3 keys
(c) 5th slot has 2 keys
(d) 6th slot has 2 keys
Answer - (c)
Explanation: 5th slot has 1 keys
2
Dynamic programming (DP) and the greedy method are different ap-
proaches to solving optimization problems.
- Dynamic Programming Characteristics:
1. Breaks problems into smaller subproblems: DP divides a problem
into smaller, overlapping subproblems.
2. Stores results to avoid recomputation: Known as memoization or
using a table to store intermediate results.
3. Used for problems with overlapping subproblems: DP is particularly
effective when subproblems are reused multiple times.
- Greedy Method: This approach makes the best possible choice at
each step, aiming for a local optimum in the hope that it leads to a
global optimum. Unlike DP, the greedy method doesn’t involve solving
overlapping subproblems or storing intermediate results.
Thus, ”It uses the greedy method for optimization” is not a character-
istic of dynamic programming.
3
2. Overlapping Subproblems: DP problems also have overlapping sub-
problems, which are solved multiple times in naive approaches. DP
avoids recomputation by storing these results.
Incorrect Options:
- Greedy decision-making: This is a characteristic of greedy algorithms,
not DP.
- Iterative approach: While DP often uses an iterative method to fill
tables, this is not the defining characteristic.
- Divide and conquer: Though DP involves breaking a problem into
subproblems, the solutions are reused (overlapping subproblems), un-
like divide-and-conquer, where subproblems are independent.
Thus, optimal substructure is the primary characteristic of dynamic
programming problems.
6. Given a hash table T with 92 slots that stores 2024 elements, the load
factor α for T is .
(a) 11
(b) 14
(c) 22
(d) 24
Explanation:
The load factor (α) of a hash table is calculated using the formula:
2024
α= = 22
92
Correct Answer:
4
c) 22
7. The time complexity of BST sort in the worst case occurs when the
tree becomes:
(a) Balanced.
(b) A full binary tree.
(c) A complete binary tree.
(d) A skewed binary tree.
5
(c) Post-order traversal.
(d) Level-order traversal.
Left Subtree Nodes < Current Node < Right Subtree Nodes.
When performing in-order traversal (Left, Root, Right), the nodes are
visited in ascending order of their values. This makes in-order traversal
the standard method to retrieve elements of a BST in sorted order.
Other Traversal Methods:
1. Pre-order traversal (Root, Left, Right): Produces a sequence based
on root-first exploration, not sorted.
2. Post-order traversal (Left, Right, Root): Produces a sequence based
on root-last exploration, not sorted.
3. Level-order traversal: Visits nodes level by level (breadth-first),
which is not sorted for a BST.
Thus, in-order traversal is used to get elements in sorted order.
9. What data structure can be used to ensure that BST Sort does not
degrade to O(n2 )?
(a) Max-Heap.
(b) Linked List.
(c) AVL Tree.
(d) Binary Search Tree.
6
become skewed, leading to a worst-case time complexity of O(n2 ) for
operations like search, insert, and traversal.
To avoid this degradation, a self-balancing binary search tree like an
AVL Tree can be used.
Characteristics of an AVL Tree:
1. Self-balancing Property: After every insertion or deletion, the tree
ensures that the height difference (balance factor) between the left and
right subtrees of any node is at most 1.
2. Guaranteed Height: The height of an AVL tree is always O(log n),
ensuring O(log n) for insert, delete, and search operations.
3. BST Sort: When used for sorting, the traversal time remains
O(n log n) even in the worst case.
Why Other Options Are Incorrect:
1. Max-Heap: A heap does not maintain a sorted structure; it is de-
signed for priority-based operations.
2. Linked List: A linked list is not suitable for efficient sorting due to
sequential traversal, leading to O(n2 ) in the worst case.
3. Binary Search Tree: A regular BST can degrade to O(n2 ) if not
balanced.
Thus, an AVL Tree ensures that BST sort does not degrade to O(n2 ).
10. What is the advantage of using a self-balancing binary search tree for
sorting?
7
of the tree remains O(log n), regardless of the order of insertion or
deletion of elements.
Advantages for Sorting:
1. Guaranteed O(log n) Height: - Ensures that operations like insert,
delete, and search are performed in O(log n) time.
- When used for sorting, the in-order traversal retrieves the sorted
elements in O(n), making the overall complexity O(n log n).
2. Avoids Worst-Case Scenarios:
- Unlike an unbalanced binary search tree, a self-balancing tree prevents
degradation to O(n2 ) in cases of skewed structures.
Why Other Options Are Incorrect:
1. Reduced space complexity: Self-balancing trees have similar space
complexity (O(n)) as other binary search trees. This is not a unique
advantage.
2. Simple implementation: Self-balancing trees (e.g., AVL, Red-Black)
are more complex to implement compared to regular BSTs.
3. Randomized data structure: Self-balancing trees are deterministic,
not randomized.
Thus, the key advantage of using a self-balancing binary search tree for
sorting is the guaranteed O(log n) height.