LearnThatStack Ace your next interview
Arrays, Strings & Hash Tables · question
Question 4 of 85

Why is accessing an array element by its index O(1)?

beginner
← All Arrays, Strings & Hash Tables questions
Re-explain

Indexing is a single arithmetic step, not a search. Because an array is a contiguous block of equal-size elements, the computer locates any element with one formula.

address = base_address + index * element_size

Give it the index and it computes the exact memory location directly. It never scans or walks through earlier elements first. Element 0 and element 999 take the same amount of work to reach.

This constant-time access holds no matter how large the array grows, which is why arrays back so many other structures. The one requirement is that every element be the same size, so the multiplication always gives the element's exact address.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

The diagram below the answer is the concept . Jump to it ↓

Tailored explanation · switch back to · ·
What should the new diagram focus on?
Guided practice for Arrays, Strings & Hash Tables One question at a time. Answer out loud, get graded, see what you missed.
How well did you know this?
AI:

Saved in this browser - sign in to keep your review list.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Interview lens How interviewers actually use this question

Likely follow-ups

  • If indexing is O(1), why does checking whether an array contains a value cost O(n)? Indexing starts from a known position. A search knows only the value, so it compares elements one at a time until it finds a match.
  • Why do array indexes start at zero? Tie it to the formula. The index is an offset from the base address, so the first element sits at offset 0.
  • How can an array hold items of different sizes, like strings, and still index in O(1)? Store fixed-size references in the slots. The formula finds the reference, then one more step follows it to the data.
  • Why can't a linked list give you O(1) access by index? Nodes are scattered and each one knows only the next, so reaching position i means following i links from the head.
  • How is a 2D array laid out in memory, and how does that affect the order you traverse it? Name row-major vs column-major. In row-major, element (r, c) sits at base + (r * cols + c) * size, so row-by-row loops read memory in order.

What you can say

  1. Indexing an array is one arithmetic step, not a search, so it costs the same no matter which element you ask for.
  2. An array is one contiguous block where every element has the same size, so each element sits at a predictable offset from the start.
  3. To find element i, the computer takes the base address and adds i times the element size, and that gives the exact memory location.
  4. It never walks past earlier elements, so element 0 and element 999 take the same work, however large the array grows. That's why so many other structures are built on arrays.
  5. The one requirement is equal-size elements. If sizes varied, the multiplication would no longer land on the right address.

Weak answers to avoid

  • Says arrays are just fast, with no mechanism That restates the claim. Give the formula, base address plus index times element size, and say the computer works it out in one step.
  • Forgets the equal-size requirement Contiguous memory alone isn't enough. Without same-size elements the multiplication misses, so say it and show you know why the formula works.
  • Mixes up access by index with search by value Claiming find or contains is also O(1) is wrong. Looking for a value still scans the array. Only a known position is constant time.
  • Only says RAM is random access That's half of it. RAM lets you jump to any address, but the array's layout is what lets you compute that address from the index.
Interview lens

Likely follow-ups, what you can say, and the weak answers to avoid.

Sign in free to open it Free account - the lens opens as soon as you're back.

Want a quick review of the fundamentals? See the Arrays, Strings & Hash Tables cheatsheet.

← Back to all Arrays, Strings & Hash Tables questions
Pro · $10/mo

74 of 85 Arrays, Strings & Hash Tables answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime