GATE / CS ARCHIVE

GATE CSE 2020 / QUESTION 16

GATE CSE 2020 Q16

Data Structures · Arrays & Linked Lists · Full GATE CSE 2020 paper

GATE 2020, Q16 1 mark · MCQ
Report issue

Arrays & Linked Lists

Question 16, GATE 2020: Find worst-case time to insert n elements into an initially empty ordinary linked list while keeping it sorted. A Θ(n); B Θ(n log n); C Θ(n²); D Θ(1).
Question 16, GATE 2020: Find worst-case time to insert n elements into an initially empty ordinary linked list while keeping it sorted. A Θ(n); B Θ(n log n); C Θ(n²); D Θ(1).
Tap to enlarge
Your answer
Answer & solution

Official answer: C

Worked explanation

The pointer rewiring for insertion is constant time once the predecessor is known. The expensive part is finding that predecessor. For the ith insertion a singly or doubly linked list can require Θ(i) node visits, since it has no random-access midpoint for binary search. Summing i from 0 to n−1 gives n(n−1)/2, which is Θ(n²). A tail pointer can make some input orders cheap but not all possible insertion positions; the question asks for worst case. Sorting the complete input first changes the online-maintenance task and is not the ordinary operation being analyzed.

Derivation and answer

Maintaining the list in sorted order after each online insertion can require scanning all current nodes to locate the insertion point. Summing worst-case costs 0+1+…+(n−1) givesΘ(n²), C. This assumes the ordinary linked-list representation without an auxiliary search index or presorting all future inputs.

Original paper, page 8 · Answer key