Array Examples: 47 Problems with Answers

Worked examples of Array to try before you open each solution, then practice problems with answers.

Worked Examples

Example 1

easy
An array `scores` contains [85, 92, 78, 95, 88]. What is scores[0]? What is scores[3]? How many elements are there?
Show solution

Answer

scores[0] = 85, scores[3] = 95, length = 5.

First step

1.
Step 1: Arrays use zero-based indexing: scores[0] = 85.

Full solution

  1. 2.
    Step 2: scores[3] = 95 (the 4th element).
  2. 3.
    Step 3: The array has 5 elements (length = 5).
Arrays store multiple values in a single variable, accessed by index. Most programming languages use zero-based indexing, so the first element is at index 0.

Example 2

medium
Write pseudocode to find the sum of all elements in an array `nums = [4, 7, 2, 9]`.

Example 3

medium
Write pseudocode to count how many elements of `nums = [3, 7, 2, 9, 4]` are greater than 5. What is the count?

Example 4

hard
Given `a = [4, 9, 1, 7, 3]`, trace finding the min: keep `best = a[0]`, then compare to each later element. What is the final `best`?

Practice Problems

Try each one, then open the solution to compare your method.

Example 1

medium
Given array `names = ['Alice', 'Bob', 'Charlie']`, what is names[1]? What happens if you access names[3]?

Example 2

medium
Given `scores = [12, 15, 9, 20]`, what are `scores[0]` and `scores[3]`, and what happens if code tries to read `scores[4]`?

Example 3

easy
Array `a = [10, 20, 30, 40]`. What is `a[0]`?

Example 4

easy
Array `a = [10, 20, 30, 40]`. What is `a[2]`?

Example 5

easy
Array `a = [5, 6, 7, 8, 9]` has 5 elements. What is the last valid index?

Example 6

easy
Array `a = [1, 2, 3]`. What happens with `a[3]`?

Example 7

easy
How many elements are in `a = [4, 8, 15, 16, 23, 42]`?

Example 8

easy
Array `a = [3, 1, 4, 1, 5]`. After `a[1] = 9`, what is the array?

Example 9

easy
To loop over every element of an array of length n with indices, what range of indices do you use?

Example 10

easy
Why are arrays useful instead of separate variables x1, x2, x3, ...?

Example 11

medium
Array `a = [2, 4, 6, 8, 10]`. What is `a[a[0]]`?

Example 12

medium
Sum every element of `a = [3, 5, 7]` by looping indices 0 to length-1. What is the sum?

Example 13

medium
Array `a = [9, 7, 5, 3, 1]`. What is `a[len(a) - 1]`?

Example 14

medium
Reverse `a = [1, 2, 3, 4]` by swapping a[i] with a[n-1-i]. What is `a[3]` paired with for swapping when i=0?

Example 15

medium
Array `a = [10, 20, 30]`. Loop `for i in 0..3 (inclusive): print(a[i])`. What goes wrong?

Example 16

medium
To insert a value at the front of an array of length n, how many existing elements must shift right?

Example 17

medium
Array `a = [5, 3, 8, 1]`. After sorting ascending, what is `a[0]`?

Example 18

medium
Array of length n. How many valid index values are there?

Example 19

challenge
A 2D array (matrix) is stored as `m` with rows of length 4. Element at row r, column c maps to flat index `r*4 + c`. What flat index is row 2, column 3?

Example 20

challenge
Array `a = [1, 2, 3, 4, 5]`. You delete `a[1]` while iterating forward by index. Why might you skip an element?

Example 21

challenge
To find the max of an unsorted array of length n, what is the minimum number of comparisons needed?

Example 22

medium
Array `a = [7, 14, 21, 28]`. What is `a[1] + a[3]`?

Example 23

easy
Array `a = [11, 22, 33, 44, 55]`. What is `a[0]`?

Example 24

easy
Array `a = [11, 22, 33, 44, 55]`. What is `a[4]`?

Example 25

easy
Array `a = [9, 8, 7]`. What does `a[3]` produce?

Example 26

easy
How many elements are in `a = [1, 1, 2, 3, 5, 8, 13]`?

Example 27

easy
Array `a = [5, 5, 5, 5]`. After `a[2] = 9`, what is the array?

Example 28

easy
Array `a = [4, 4, 4]`. What is the sum of all elements?

Example 29

medium
Array `a = [3, 1, 4, 1, 5, 9]`. What is `a[1] + a[4]`?

Example 30

medium
Array `a = [10, 20, 30, 40, 50]`. What is `a[a[0]/10]`?

Example 31

medium
Array `a = [6, 2, 8, 4]`. What is `max(a)`?

Example 32

medium
Reverse `a = [1,2,3,4,5]` by swapping `a[i]` with `a[n-1-i]`. After processing i=0 and i=1, what is the array?

Example 33

medium
Array `a = [2, 4, 6, 8]`. Loop `for i in 0..3 (inclusive)` prints `a[i]`. How many values are printed and what are they?

Example 34

medium
To delete the LAST element of an array of length n, how many existing elements must shift?

Example 35

medium
Array `a = [9, 3, 7, 1, 5]`. After ascending sort, what is `a[2]`?

Example 36

medium
Sum every element of `a = [10, 20, 30, 40]` using indices 0 to length-1. What is the sum?

Example 37

medium
Array `a = [3, 6, 9, 12]`. What is `a[len(a) - 2]`?

Example 38

medium
To insert a value at index 1 in an array of length 6 (no resize cost), how many existing elements must shift right?

Example 39

hard
A 2D array (matrix) stored row-major with row length 5. What flat index corresponds to row 3, column 2?

Example 40

hard
Linear search needs at most how many comparisons to find a value in an array of length n (worst case)?

Example 41

hard
Array `a = [1, 2, 3, 4, 5, 6]`. Iterating forward by index and deleting `a[i]` whenever `a[i]` is even — which elements remain after one full pass?

Example 42

hard
You want to swap the FIRST and LAST elements of an array of length 6 without overwriting either. What is the minimum number of assignments needed?

Example 43

challenge
An array stores a 2D 4x4 grid in row-major order. The value at flat index 11 corresponds to which (row, column)?

What to focus on

Core idea: Arrays store multiple related values under one name, making it easy to iterate over them all.

Common stuck point: Most languages index from 0, not 1. The last element of a 3-item array is at index 2.

Ask yourself: Are many values of the same kind kept under one name and picked out by a position number?

Concept recap

An ordered collection of values stored together under a single name and accessed by their numeric index position. Arrays allow you to store, retrieve, and manipulate multiple related values efficiently using loops and index-based access.

An array behaves like the wall of numbered mailboxes in an apartment lobby: one building address, one box per apartment, and the mail carrier goes straight to box 4 without opening boxes 0 to 3. Two differences: an array's numbering starts at 0, and there is no box past the last one.

Read the full lesson on Array

Background knowledge

These ideas may be useful before you work through the harder examples.

variabledata types