Practice mode Data Structures

What is the time complexity of accessing the element at a given index in an array?

Correct answer C. O(1)

Explanation

Array elements are stored contiguously and are all the same size, so the address of element i is computed directly as base address plus i times element size. This arithmetic takes constant time regardless of the array's length. O(n) is the cost of reaching the i-th node of a linked list, where you must follow pointers one by one.

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.