Practice mode Data Structures

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.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.