programming beginner 10 sample questions

Linked Lists MCQ Practice Test

Implementing basic operations like insertions and traversals.

Q1. Consider a doubly linked list where each node contains an integer and pointers to the previous and next nodes. A function 'swapPairs' is designed to swap consecutive pairs of nodes. For example, if the list is 1 -> 2 -> 3 -> 4, after calling 'swapPairs', it should become 2 -> 1 -> 4 -> 3. However, there's a bug in the implementation. Given the following code snippet (assuming necessary node structure and functions exist): c++ void swapPairs(Node*& head) { if (!head || !head->next) return; Node* current = head; head = head->next; // Update head while (current && current->next) { Node* nextPair = current->next->next; current->next->next = current; current->next = nextPair; current = nextPair; } } What is the incorrect behavior or edge case this code snippet will fail to handle correctly?

Explanation: The swap loop never links the previously-swapped pair's tail to the newly-swapped pair's head. For 1->2->3->4->5 the result reachable from head is 2->1->3->5; node 4 (and generally the even-positioned new pair heads) becomes unreachable, leaking nodes.

Q2. You have a singly linked list where each node contains an integer and a pointer to the next node. The list is initially empty. You perform the following operations sequentially: insert 5 at the head, insert 10 at the tail, insert 2 at the head. What is the value of the node immediately following the head node after these operations?

Explanation: Inserting 5 at the head gives 5. Inserting 10 at the tail gives 5 -> 10. Inserting 2 at the head gives 2 -> 5 -> 10, so the head is 2 and the node immediately after it holds 5.

Q3. You have a singly linked list where each node contains an integer and a pointer to the next node. You want to insert a new node with the value 77 between the nodes containing 55 and 99. Assuming you have pointers 'before' pointing to the node with 55 and 'after' pointing to the node with 99, what line of code correctly inserts the new node?

Explanation: The correct approach is to first set the 'next' pointer of the 'before' node to point to the new node. Then, set the 'next' pointer of the new node to point to the 'after' node. This correctly inserts the new node between the existing nodes.

Q4. Given a doubly linked list with a sentinel node, what is the time complexity of finding the middle node using the slow and fast pointer approach?

Explanation: The slow and fast pointer approach traverses the linked list once, resulting in a time complexity of O(n), where n is the number of nodes in the list. This is because the fast pointer moves twice as fast as the slow pointer, effectively covering half the list with each step, until they meet at the middle node.

Q5. Given a doubly linked list with a custom implementation of a “skip” operation that moves the cursor to the next node that has a value greater than the current node’s value, what is the time complexity of this operation?

Explanation: The skip operation in a doubly linked list with a custom implementation requires traversing the list to find the next node that has a value greater than the current node’s value. This results in a time complexity of O(n), where n is the number of nodes in the list.

Q6. Consider a linked list with the following structure: 1 -> 2 -> 3 -> 4 -> 5. What is the time complexity of finding the node with value 3 using a recursive function that traverses the list by following the next pointer of each node?

Explanation: The time complexity of finding a node in a linked list using a recursive function is O(n), where n is the number of nodes in the list. This is because each recursive call potentially traverses the entire list, resulting in a linear time complexity.

Q7. Given a doubly-linked list with a sentinel node, what is the time complexity of the operation to rotate the list by $k$ nodes, where $k$ is the number of nodes to rotate, and the rotation is performed in-place?

Explanation: The time complexity of rotating a doubly-linked list by $k$ nodes is O(n), where n is the number of nodes in the list. This is because we need to update the next and previous pointers of each node, which takes O(n) time. The value of k does not affect the time complexity, as we still need to traverse the entire list to update the pointers.

Q8. Given a doubly linked list with a sentinel node, what is the time complexity of deleting a node at a given position k, where k is the index of the node to be deleted?

Explanation: When deleting a node at a given position k, we need to find the node at position k first. If we have a doubly linked list with a sentinel node, we can traverse the list from the head to find the node at position k, which takes O(n) time complexity. The sentinel node does not provide a constant-time access to the head of the list, because we still need to find the node at position k.

Q9. In a doubly linked list, given a node, how do you delete the node itself without changing the next and previous pointers of adjacent nodes?

Explanation: When deleting a node in a doubly linked list, it's essential to maintain the integrity of the list by updating the next and previous pointers of adjacent nodes. The correct approach involves updating the next pointer of the previous node to skip the current node, and then updating the previous pointer of the next node to skip the current node. This ensures that the list remains valid and the next and previous pointers of adjacent nodes are correctly updated.

Q10. Given a doubly linked list with a sentinel node at the head, what is the time complexity of inserting a new node at a specific position in the list, where the position is given as a distance from the end of the list?

Explanation: To insert a new node at a specific position in a doubly linked list, we need to traverse the list from the head to the node before the insertion point. This requires traversing n nodes, where n is the distance from the end of the list. Therefore, the time complexity is O(n).

That was just a sample. Sign up to unlock the full question bank with timed tests and certificates.

Sign Up Free
Linked Lists MCQ Practice Test | Mind Hustle