๐ 7. How do you handle collisions in a hash table?
A collision happens when two keys generate the same index.
๐น Example:
hash("abc") = 5
hash("xyz") = 5
Both want index 5.
๐น Collision Handling Techniques
1๏ธโฃ Chaining
Store multiple values in a linked list.
Index 5: abc โ xyz
2๏ธโฃ Open Addressing
Find another empty slot.
Methods: Linear probing, Quadratic probing, Double hashing
๐น Linear Probing Example
Index occupied? Move to next slot.
๐น Interview Tip
Most interviewers expect Chaining and Linear probing to be explained clearly.
๐ 8. What is a binary tree and a binary search tree (BST)?
๐น Binary Tree
A tree where each node has at most 2 children.
10
/ \
5 20
๐น Binary Search Tree (BST)
Special binary tree where: Left < Root < Right
10
/ \
5 20
๐น BST Advantages
Fast searching, Sorted traversal, Efficient insert/delete
๐น Complexity
Operation | Average
Search | O(log n)
Insert | O(log n)
Delete | O(log n)
Worst case: O(n)
๐น Interview Tip
BST questions are among the most asked DSA interview topics.
๐ 9. How do you traverse a tree (inorder, preorder, postorder)?
Tree traversal means visiting all nodes.
๐น Inorder Traversal
Left โ Root โ Right
def inorder(root):
if root:
inorder(root.left)
print(root.val)
inorder(root.right)
โก๏ธ Used in BST to get sorted order.
๐น Preorder Traversal
Root โ Left โ Right
Used for: Tree copying, Serialization
๐น Postorder Traversal
Left โ Right โ Root
Used for: Deletion, Bottom-up processing
๐น Complexity
All traversals: Time O(n), Space O(h)
๐ 10. What is recursion and when is it useful?
Recursion is when a function calls itself.
๐น Example:
def factorial(n):
if n == 0:
return 1
return n * factorial(n - 1)
๐น Recursive Flow
factorial(4) = 4 ร factorial(3) = 4 ร 3 ร factorial(2)...
๐น Key Components
1. Base case
2. Recursive case
๐น Where Recursion is Useful
Trees, Graphs, DFS, Backtracking, Divide & Conquer
๐น Interview Tip
Always explain: Base condition, Stack usage, Time complexity
๐น Common Mistake
Missing base case causes: Stack Overflow Error
๐ฅ Double Tap โค๏ธ For Part-2
Post #2952
1.2K
- โค 5