# Arrays and Lists

Mathematics I · Programming Elements · https://tryals.app/learn/mathematics-i/arrays-and-lists

## Collections and Indexed Access

Programs rarely handle one value at a time; they process **collections**. An **array** (or **list**) stores an ordered sequence of values in a single named structure, accessed by **index**: a position number. 

The defining feature is **indexed access**: element $a[i]$ is retrieved directly by its position in constant time, without scanning from the start. Multi-dimensional arrays (arrays of arrays) represent matrices and grids, indexed by row and column.

| Operation | Description |
| --- | --- |
| **Access / Update** | Read or overwrite $a[i]$ |
| **Length** | Total number of elements |
| **Mutation** | Append, insert, or remove elements |
| **Traverse** | Visit every element to compute or search |

## Zero-Based Indexing

Most languages use **zero-based indexing**: the first element is at index `0`, and the last of an $n$-element array is at index $n - 1$ (not $n$). 

Accessing index $n$ is **out of bounds**: an error, and a frequent source of off-by-one bugs. For a list of length 5, valid indices are `0, 1, 2, 3, 4`. 

Arrays pair perfectly with loops: iterating an index from `0` to $n - 1$ visits each element exactly once.

> **Common pitfall:** Forgetting **zero-based indexing** and going **out of bounds**. Using $n$ instead of $n-1$, or starting at `1` instead of `0`, triggers an out-of-bounds error.

## Practice questions

6 of this lesson's 11 practice questions, with answers. The full set is in the app.

### 1. Arrays are indexed from 0, so a list of length 5 has valid indices 0 to 4 and a[5] is an error. Why has this convention survived, given how many off-by-one bugs it causes?

A. Because it saves one byte of memory per array, which mattered historically.
B. Because mathematicians index sequences from 0, and programming followed their convention.
C. Because the index is an offset from the start of the array.
D. Because computers cannot represent the number 1 in an index register.

**Answer:** C. Because the index is an offset from the start of the array.

**Why:** The index is a displacement rather than a count, so element i sits at base + i·size with no correction term; index from 1 and every access needs a subtraction. Half-open ranges also compose, [0,5) and [5,10) meet with no overlap and no gap, and the length of [a,b) is b − a. The bugs are concentrated at the human interface and the arithmetic underneath is clean.

Page: https://tryals.app/practice/mathematics-i/arrays-and-lists/arrays-are-indexed-from-0-so-a-list-of-length-5-has-valid-indices-0

### 2. A list has length 5 and uses zero-based indexing. Sort each access.

**Answer:**

- Valid: a[0], a[3], a[4]
- Out of bounds: a[5], a[6]

**Why:** Valid indices are 0, 1, 2, 3, 4. a[5] uses the length as an index, which is exactly one past the end, and a[6] is further still. The boundary case a[5] is the one that slips through in real code.

Page: https://tryals.app/practice/mathematics-i/arrays-and-lists/a-list-has-length-5-and-uses-zero-based-indexing-sort-each-access

### 3. Appending to a dynamic array is described as O(1) *amortised*, even though some appends copy the entire array. What does the qualifier buy?

A. It means the copies are performed in the background without blocking
B. It averages the rare expensive copies over the many cheap appends
C. It is a convention for hiding an operation that is genuinely O(n)
D. It means the worst case is O(1) but the average is worse

**Answer:** B. It averages the rare expensive copies over the many cheap appends

**Why:** Doubling means the copies form a geometric series summing to under 2n, so n appends cost O(n) in total and O(1) each on average. Amortised is a real guarantee about the total and not a worst case, which matters in real-time code, where one occasional O(n) pause can still miss a deadline.

Page: https://tryals.app/practice/mathematics-i/arrays-and-lists/appending-to-a-dynamic-array-is-described-as-o-1-amortised-even

### 4. Let a be the list 3, 1, 4, 1, 5 with zero-based indexing. Select every TRUE statement.

A. the length of a is 5
B. a[5] is 5
C. a[0] is 3
D. a[2] is 1
E. a[4] is 5

**Answer:** A. the length of a is 5; C. a[0] is 3; E. a[4] is 5

**Why:** a[0] = 3, a[2] = 4 (not 1), the length is 5, a[4] = 5, and a[5] is out of bounds rather than 5. the second option is tempting precisely because the value 5 does appear in the list, but not at that index.

Page: https://tryals.app/practice/mathematics-i/arrays-and-lists/let-a-be-the-list-3-1-4-1-5-with-zero-based-indexing-select

### 5. Match each array operation to what it does.

**Answer:**

- Access a[i] → Reads the element at position i directly
- Length → Reports how many elements the list holds
- Traverse → Visits every element in turn, usually with a loop
- Out of bounds → Uses an index outside 0 to n-1

**Why:** Indexed access is direct and cheap; traversal costs a full pass. Length and the last valid index differ by one, and mixing them up is precisely what produces the out-of-bounds error.

Page: https://tryals.app/practice/mathematics-i/arrays-and-lists/match-each-array-operation-to-what-it-does

### 6. A list b holds seven values. A loop runs i from 0 to 7 inclusive and reads b[i] each time. How many of those reads are out of bounds?

**Answer:** 1

**Why:** The loop makes 8 accesses (i = 0..7) but only 0..6 are valid, so exactly one read, b[7], is out of bounds. The loop looks nearly right, which is why off-by-one errors survive casual reading.

Page: https://tryals.app/practice/mathematics-i/arrays-and-lists/a-list-b-holds-seven-values-a-loop-runs-i-from-0-to-7-inclusive-and
