Stop 21 · Data Structures & AlgorithmsTopics · 1/17

Arrays

Part of the Frontend Developer Roadmap

In short

Open any coding round and the first question is very likely about an array. Get comfortable with indexes and two pointers, and half the easy problems stop feeling scary.

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

The jaadu ticket checker points at one seat in a neat row of train seats.

Think of it like this

Seats in a train coach

Seat 42 is always in the same spot, so the TT walks straight to it. That's array indexing: instant. But if a new passenger must sit at seat 1, everyone has to shift one seat back. That's why inserting at the front is slow.

Watch it happen

Two pointers hunting for a sum of 10

1
0
left
3
1
4
2
6
3
8
4
11
5
right

1/5Target is 10. left points at 1, right points at 11. Their sum is 12, too big.

The big ideas

What you need to know

Reading by index is O(1)

arr[i] jumps straight to the item, no matter how long the array is.

Front inserts are O(n)

unshift or splice at the start moves every other item. push and pop at the end are cheap.

Two pointers

On a sorted array, one pointer at each end moving inwards often turns an O(n²) idea into O(n).

Prefix sums

Store running totals once, and any range sum becomes a single subtraction.

See it in code

Walk through the code

js
1// arr is sorted in ascending order2function hasPairWithSum(arr, target) {3  let left = 0;4  let right = arr.length - 1;5 6  while (left < right) {7    const sum = arr[left] + arr[right];8    if (sum === target) return [left, right];9    if (sum < target) left++;10    else right--;11  }12  return null;13}

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

Each step moves one pointer, so it runs at most n times. Time O(n), extra space O(1). The brute force with two loops is O(n²).

Save yourself the pain

Common mistakes

Oops

Using two pointers on an unsorted array and wondering why it misses pairs.

Do this instead

Two pointers needs sorted data. For unsorted input, use a HashMap or sort first (O(n log n)).

Oops

Forgetting that arr.includes inside a loop is another loop.

Do this instead

includes, indexOf and find are O(n) each. Inside a loop, that's O(n²) without you noticing.

Your mission

Three array warm-ups

~40 min

Quick check

What's the time complexity of arr.unshift(x) on an array of n items?

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