Stop 21 · Data Structures & AlgorithmsTopics · 1/17

Arrays

Part of the Backend Developer Roadmap

In short

Half of all coding rounds open with "given an array of numbers…". Get fluent with indexes, loops and two pointers, and a big slice of LeetCode starts to feel familiar.

The most fundamental data structure — a contiguous, indexed collection of elements — and the starting point for a huge share of coding interview questions.

The purple blob walks past a long row of identical exam desks and points at one.

Think of it like this

Desks in an exam hall row

Every desk is the same size and sits right after the previous one. The invigilator doesn't count her way to desk 7; she walks straight to it. An array works the same way: jump to any index instantly.

Watch it happen

Two pointers: find a pair that adds to 10

1
0
L
3
1
4
2
6
3
8
4
11
5
R

1/5The array is sorted. Put L at the start and R at the end. 1 + 11 = 12, too big.

The big ideas

What you need to know

Reading by index is O(1)

arr[5000] is as fast as arr[0], because the computer works out the exact memory spot from the index.

Inserting in the middle is O(n)

Adding or removing near the front shifts every element after it. That's why splice and shift inside loops get slow.

Two pointers

On sorted data, one pointer from each end can replace a nested loop and bring O(n²) down to O(n).

Prefix sums

Store running totals once, then any range sum is just prefix[j] - prefix[i]. Great for "sum of subarray" questions.

See it in code

Walk through the code

ts
1function pairWithSum(nums: number[], target: number): [number, number] | null {2  let left = 0;3  let right = nums.length - 1;4 5  while (left < right) {6    const sum = nums[left] + nums[right];7    if (sum === target) return [left, right];8    if (sum < target) left++;   // need a bigger sum9    else right--;               // need a smaller sum10  }11  return null;12}13 14console.log(pairWithSum([1, 3, 4, 6, 8, 11], 10)); // [2, 3]

1/4Start one pointer at each end of the sorted array.

This only works because the array is sorted. On unsorted data, use a HashMap instead (see the HashMap lesson).

Save yourself the pain

Common mistakes

Oops

Looping with i <= arr.length and reading arr[i] on the last step.

Do this instead

The last index is arr.length - 1. Use i < arr.length, or you read undefined and get NaN bugs.

Oops

Calling arr.shift() or splice() inside a loop over a big array.

Do this instead

Each call shifts the whole array. Walk with an index instead, or build a new array in one pass.

Your mission

Two-pointer warm-up

~35 min

Quick check

Why does reading arr[90000] take about the same time as arr[0]?

Go deeper

  1. 1Solve 10-15 easy array problems (two-sum, reverse array, remove duplicates).
  2. 2Learn common patterns: two-pointer, prefix sum, in-place manipulation.
  3. 3Practice explaining your approach's time and space complexity out loud.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge