✅ Coding Interview Questions with Answers Part-2 🧠💻
11. What is a hash table? How hashing works
A hash table stores key-value pairs. It uses a hash function to map keys to an index.
• Key goes into hash function
• Hash function returns index
• Value stores at that index
Why interviewers like it:
• Average lookup time is O(1)
• Used in caching and indexing
Real examples:
• Python dict
• Java HashMap
12. What are collisions in hashing? How to handle them
Collision happens when two keys map to the same index.
Common handling methods:
• Chaining: Each index holds a linked list
• Open addressing: Find next empty slot
Types of open addressing:
• Linear probing
• Quadratic probing
• Double hashing
Interview tip:
• Chaining is easier to explain
• Worst case becomes O(n)
13. Difference between HashMap and HashSet
HashMap:
• Stores key and value
• Keys are unique
• Values duplicate allowed
HashSet:
• Stores only keys
• No duplicates
• Internally uses HashMap
Use cases:
• HashMap for lookup with data
• HashSet for uniqueness checks
14. What is a binary tree
A binary tree is a tree where each node has at most two children: left child and right child.
Common types:
• Full binary tree
• Complete binary tree
• Perfect binary tree
Uses:
• Hierarchical data
• Expression trees
15. What is a binary search tree
A binary search tree follows ordering rules.
Rules:
• Left child < root
• Right child > root
Operations:
• Search, insert, delete in O(log n) average
• Worst case O(n) if unbalanced
Interview focus:
• Inorder traversal gives sorted output
16. Difference between BFS and DFS
BFS:
• Level by level
• Uses queue
• Finds shortest path
DFS:
• Goes deep first
• Uses stack or recursion
• Uses less memory
When to use:
• BFS for shortest path
• DFS for traversal problems
17. What is a balanced tree
A balanced tree keeps height minimal.
Why it matters:
• Operations stay O(log n)
• Prevents skewed structure
Examples:
• AVL tree
• Red-Black tree
Interview note:
• Balance improves performance
18. What is heap data structure
Heap is a complete binary tree. It follows heap property.
Properties:
• Complete tree
• Parent follows order rule
Common use:
• Priority queues
• Scheduling tasks
Time complexity:
• Insert and delete take O(log n)
19. Difference between min heap and max heap
Min heap:
• Root holds smallest value
• Used when smallest priority wins
Max heap:
• Root holds largest value
• Used when highest priority wins
Example:
• Job scheduling
• Top K problems
20. What is a graph? Directed vs undirected
Graph contains nodes and edges.
Directed graph:
• Edges have direction
• One-way relationship
Undirected graph:
• No direction
• Two-way relationship
Real examples:
• Directed: Twitter follow
• Undirected: Facebook friends
Double Tap ♥️ For Part-3
Post #2766
1.51K
- ❤ 10