A. Missing update of q.tail to new node after adding
B. Should check q.tail == nil instead of q.head == nil
C. Node should be created without pointer
D. Should assign q.head.next = node instead of q.tail.next
Solution
Step 1: Analyze enqueue logic
Function sets head if empty, else links new node after tail.
Step 2: Check tail pointer update
Function does not update q.tail to new node, so tail remains old node.
Final Answer:
Missing update of q.tail to new node after adding -> Option A
Quick Check:
Update tail pointer after enqueue [OK]
Hint: Always update tail pointer after adding new node [OK]
Common Mistakes:
Not updating tail pointer after enqueue
Checking wrong pointer for empty queue
Creating node without pointer
Linking new node to head instead of tail
5. You want to implement a queue using a linked list in Go that supports enqueue and dequeue efficiently. Which of the following changes will improve the enqueue operation's time complexity from O(n) to O(1)?
hard
A. Add new nodes at the head instead of the tail
B. Use a doubly linked list instead of singly linked list
C. Maintain a tail pointer to the last node and update it on enqueue
D. Traverse the list to find the last node each time before enqueue
Solution
Step 1: Understand enqueue time complexity
Without tail pointer, enqueue requires traversal to last node, O(n).
Step 2: Using tail pointer
Maintaining tail pointer allows direct access to last node, making enqueue O(1).
Final Answer:
Maintain a tail pointer to the last node and update it on enqueue -> Option C
Quick Check:
Tail pointer reduces enqueue to O(1) [OK]
Hint: Keep tail pointer to avoid traversal on enqueue [OK]
Common Mistakes:
Thinking doubly linked list alone improves enqueue
Adding at head breaks queue order
Traversing list each time causes O(n) complexity
Ignoring tail pointer update
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.