Free sample lesson · Google SWE Internship Coding Prep

Arrays

How do computers store items in continuous memory blocks?

Lesson 1 of the Wondering course Google SWE Internship Coding Prep

What an Array Really Is

An array stores a list of values in order, one after another.

Each value sits in a numbered slot called an index. The computer places these slots side by side in one continuous block of memory.

An array as a row of side-by-side memory slots

Notice that the slots touch with no gaps, and each one carries its own index number, which is what lets the computer jump to any position.

That layout is the whole trick. Because slots are neighbors, the computer can jump straight to any position.

Arrays sit underneath most coding interview questions, so this idea is worth knowing cold.

Indexes Start at Zero

Most languages, including Python, Java, and C++, number slots starting at 0.

Take scores = [7, 3, 9, 4]. The value 7 lives at index 0, and the value 4 lives at index 3.

scores = [7, 3, 9, 4]

So an array of length $$n$$ has valid indexes from $$0$$ to $$n - 1$$.

Make it a reflex: the last element is always at length minus one.

Why Access Is So Fast

Every slot in an array takes the same amount of memory.

So the computer finds any slot with simple arithmetic: the start address plus the index times the slot size.

$$address = base + i \times size$$

Reading scores[2]
  • Find base: Start from the address where the array begins.
  • Multiply: Multiply the index 2 by the slot size.
  • Add: Add that offset to the start address.
  • Read: Read the value at that address, which is 9.

One calculation, no scanning. That is why reading scores[2] takes constant time, written $$O(1)$$, whether the array holds four items or four million.

By Position vs by Value

Fast access only works when you already know the position.

If you ask, "Where is the value 9?", the computer has no shortcut. It checks slots one by one until it finds a match.

That is a linear search, which takes $$O(n)$$ time in the worst case.

Which question are you asking?
  • Access by index
  • Search by value

Interviewers love this gap. Always know which question you are asking: by index or by value.

Staying Inside the Bounds

An index outside 0 to n - 1 points past the edge of the array.

Java throws ArrayIndexOutOfBoundsException and Python raises IndexError. C and C++ may silently read garbage memory.

Most of these bugs are off-by-one errors, such as looping with i <= n instead of i < n.

Before you index
  • Have index i
  • Is 0 ≤ i < n?
  • Read arr[i]: Safe, constant-time access.
  • Out of bounds: Error or garbage memory, depending on language.
  • Yes
  • No

Before you index, check the range.

Arrays in Your Interviews

An array is ordered values in one continuous block of memory, each at a numbered index.

That layout gives you $$O(1)$$ access by position. Finding a value without knowing its position still costs $$O(n)$$.

Array cheat sheet
  • What you get
  • Ordered values: Each sits at a numbered index.
  • Continuous memory: Slots are neighbors with no gaps.
  • O(1) access by index: Base plus index times slot size.
  • What to watch
  • Search by value is O(n): No shortcut without the position.
  • Valid range 0 to n - 1: Use i < n in loops.

Know which question you are asking: by index or by value.

Count from zero, stop at n - 1, and check your loop bounds.

Master these basics now. Most interview problems build on them.

Check yourself

An array stores its elements in _____ memory locations.

  • contiguous
  • scattered

Answer: contiguous

An array occupies one continuous block of memory, with each element placed right after the previous one. This layout is what makes jumping straight to any position possible.

In an array, the _____ is a number that marks a position, while the _____ is the value stored at that position.

Answer: In an array, the index is a number that marks a position, while the element is the value stored at that position.

The index tells you where to look (a position number). The element is what you find there (the stored value). In arr[2] = 9, 2 is the index and 9 is the element.

Given the zero-indexed array arr = [7, 3, 9, 4], what does arr[2] return?

  • 9
  • 3
  • 4
  • 7

Answer: 9

Zero-indexed means the positions start at 0: arr[0] = 7, arr[1] = 3, arr[2] = 9, arr[3] = 4. Picking 3 is the common mistake of counting from 1.

Keep going

This is lesson 1 of 11 in Google SWE Internship Coding Prep. Next up:

  1. Hash Maps
  2. Linked Lists
  3. Two Pointers
  4. Sliding Window
  5. Binary Search

Start this course free

Wondering turns any topic into short lessons you can read, watch, or listen to, with practice questions and an AI tutor.