Question

A developer is choosing between an array and a linked list to store a collection of elements. If the primary operations will be frequent insertions and deletions at arbitrary positions, and infrequent random access, which data structure is generally more suitable?

A Array Correct Answer Incorrect Answer
B Linked List Correct Answer Incorrect Answer
C Hash Table Correct Answer Incorrect Answer
D Stack Correct Answer Incorrect Answer
E Queue Correct Answer Incorrect Answer

Solution

Linked List: Insertion and deletion at arbitrary positions (once the position is found) are O(1) because only pointers need to be updated. However, random access (finding an element at a specific index) is O(N) as it requires traversing from the beginning.     Array: Insertion and deletion at arbitrary positions are O(N) because elements need to be shifted. Random access is O(1).     Given the emphasis on frequent insertions/deletions at arbitrary positions and infrequent random access, a linked list is more suitable.

Practice Next
ask-question