Question
items are stored in a sorted doubly linked list. For a delete operation, a pointer is provided to the record to be deleted. For a decrease-key operation, a pointer is provided to the record on which the operation is to be performed.
An algorithm performs the following operations on the list in this order: delete, insert, find, and decrease-key. What is the time complexity of all these operations put together?
Official answer and solution
- Official answer
- C
- From
- Official GATE 2016 answer key
Solution
Determine the time complexity of each individual operation on a sorted doubly linked list and sum them up.
Analyze each operation on a sorted doubly linked list of size :
delete: A direct pointer to the target node is provided. Unlinking a node in a doubly linked list requires updatingp->prev->next = p->nextandp->next->prev = p->prev. This takes time per operation.
For delete operations: .
insert: In a sorted doubly linked list, finding the correct position for insertion requires linear traversal from an end. In the worst case, this takes time per insertion.
For insertions: .
find: A doubly linked list does not support random access, so binary search cannot be applied. Searching requires sequential traversal, taking time per search.
For find operations: .
decrease-key: A direct pointer to the node is given. When a key is decreased, its sorted position shifts towards the front of the list. Re-positioning the node requires traversing backwards past smaller elements, which takes time in the worst case.
For decrease-key operations: .
Combine the time complexities of all operations:
Therefore, the overall time complexity is , corresponding to option (C).
C
Assuming find or insert takes by confusing a sorted linked list with a balanced binary search tree.
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2016 Computer Science and Information Technology (CS) Session 2
- Question
- 25 of 65, printed as CS Q.15
- Section
- Computer Science and Information Technology
Report an error in this question
GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.