🚀 Coding Interview Questions with Answers (Part 8)
7️⃣1️⃣ What is a Sparse Matrix?
Answer:
A sparse matrix is a matrix in which most of the elements are 0. Instead of storing every element, only the non-zero elements are stored to save memory.
Applications:
• Machine Learning
• Graph representations
• Scientific computing
• Image processing
Advantages:
• Saves memory
• Improves computational efficiency
7️⃣2️⃣ What is a Dynamic Array?
Answer:
A dynamic array is an array that can automatically resize itself when more elements are added.
Unlike a fixed-size array, it allocates additional memory when its capacity is reached.
Examples:
• ArrayList in Java
• vector in C++
• list (dynamic array implementation) in Python
Advantages:
• Flexible size
• Fast random access
• Easy insertion at the end
7️⃣3️⃣ What is Load Factor?
Answer:
Load factor is the ratio of the number of stored elements to the total number of buckets in a hash table.
Formula:
Load Factor = Number of Elements / Number of Buckets
A high load factor increases the likelihood of collisions, while a low load factor improves performance but uses more memory.
7️⃣4️⃣ What is Collision Resolution?
Answer:
Collision resolution refers to the techniques used to handle situations where multiple keys are mapped to the same location in a hash table.
Common Methods:
• Separate Chaining
• Linear Probing
• Quadratic Probing
• Double Hashing
The goal is to maintain efficient search, insertion, and deletion operations.
7️⃣5️⃣ What is the Difference Between Linear Probing and Chaining?
Answer:
Linear Probing
• Stores collided elements in the next available slot.
• Uses open addressing.
• Requires less memory.
• Performance decreases as the table becomes full.
Separate Chaining
• Stores collided elements in a linked list at the same bucket.
• Easier to handle many collisions.
• Requires additional memory for linked lists.
7️⃣6️⃣ What is Tree Traversal?
Answer:
Tree traversal is the process of visiting every node in a tree exactly once in a specific order.
Common Types:
• Preorder
• Inorder
• Postorder
• Level-order
Tree traversal is used for searching, printing, and processing tree data.
7️⃣7️⃣ What is the Difference Between Preorder, Inorder, and Postorder Traversal?
Answer:
• Preorder: Root → Left → Right
• Inorder: Left → Root → Right
• Postorder: Left → Right → Root
Applications:
• Preorder: Copying a tree
• Inorder: Produces sorted output in a Binary Search Tree
• Postorder: Deleting or freeing a tree
7️⃣8️⃣ What is Level-Order Traversal?
Answer:
Level-order traversal visits the nodes of a tree level by level, starting from the root.
It uses a Queue and is also known as Breadth-First Traversal (BFS) for trees.
Applications:
• Printing trees level by level
• Finding the shortest path in unweighted trees
• Binary tree serialization
7️⃣9️⃣ What is the Recursion Stack?
Answer:
The recursion stack is the memory area used by the system to keep track of active recursive function calls.