Dsa Mock3
Dsa Mock3
concepts better.
4. **What is a palindrome?**
- A palindrome is a sequence that reads the same forward and backward, such as "madam" or
"racecar." Palindromes are often checked using stacks or recursion in programming.
Sure! Let's continue with detailed explanations for the remaining questions.
5. **How do you delete a node from the end of a doubly linked list?**
- To delete a node from the end of a doubly linked list:
1. Traverse the list to find the last node.
2. Update the previous node's next pointer to null, severing the connection to the last node.
3. Optionally, set the last node's previous pointer to null for proper memory deallocation.
4. Free the memory of the last node (if manual memory management is required).
This operation has a time complexity of O(n) because you need to traverse the list to reach the last
node.
10. **What is the advantage of a doubly linked list over a singly linked list?**
- The primary advantage of a doubly linked list over a singly linked list is that it allows traversal in both
directions, forward and backward, making operations like deletion of a node or traversal to the previous
node more efficient. Additionally, operations that require access to the previous node are simpler to
implement in a doubly linked list.
11. **What is the difference between a circular linked list and a linear linked list?**
- In a **linear linked list** (singly or doubly), the last node points to null, marking the end of the list. In a
**circular linked list**, the last node points back to the first node, creating a continuous loop. Circular
linked lists are useful when you need to cycle through the elements repeatedly without resetting.
9. **What is a heap?**
- A heap is a special tree-based data structure that satisfies the heap property:
- In a **max-heap**, for any given node `i`, the value of `i` is greater than or equal to the values of its
children, with the maximum value at the root.
- In a **min-heap**, for any given node `i`, the value of `i` is less than or equal to the values of its
children, with the minimum value at the root.
- Heaps are commonly implemented as binary heaps and are used in algorithms like heapsort and
priority queues.
10. **How do you insert an element into a binary search tree (BST)?**
- To insert an element into a BST:
1. Start at the root of the tree.
2. Compare the element to be inserted with the current node's value.
3. If the element is smaller, move to the left child; if larger, move to the right child.
4. Repeat the process until you find a null reference where the element can be inserted.
5. Insert the new node at the null position.
- The time complexity of insertion is O(log n) for a balanced BST and O(n) in the worst case (skewed
tree).
11. **How do you delete an element from a binary search tree (BST)?**
- Deleting an element from a BST involves three cases:
1. **Node to be deleted has no children (leaf node):** Simply remove the node.
2. **Node to be deleted has one child:** Replace the node with its child.
3. **Node to be deleted has two children:** Find the inorder successor (smallest node in the right
subtree) or inorder predecessor (largest node in the left subtree) to replace the node’s value, and then
delete the successor/predecessor node using one of the first two cases.
- The time complexity is O(log n) for a balanced BST and O(n) in the worst case.
1. **What is a graph?**
- A graph is a data structure consisting of a set of nodes (or vertices) and a set of edges (or arcs) that
connect pairs of nodes. Graphs can be directed or undirected, weighted or unweighted. They are used to
model relationships between objects, where the objects are represented by vertices and the relationships
by edges. Common applications of graphs include social networks, transportation networks, and
dependency resolution.