What is the worst-case time complexity of inserting an element at the beginning of an array that holds n elements and has spare capacity?
Correct answer C. O(n)
Explanation
To make room at index 0, every existing element must shift one position to the right, which takes n moves. Hence insertion at the front is O(n). Appending at the end is O(1) when spare capacity exists, because nothing has to move. A linked list, by contrast, can insert at its head in O(1) by adjusting a single pointer.
Answer checked How we write and check questions
Discussion