DSA-VerseLearn by watching code run
DashboardData StructuresAlgorithmsVisualizerCORECoding PlaygroundInterview QuestionsComplexity AnalyzerLearning NotesBookmarksLearning ProgressSettings
All data structures

Linear Data Structures

Arrays

A fixed-size block of same-type elements stored next to each other in memory, accessed by index in O(1).

Interactive4 visualizer(s)

1Introduction

Arrays are the foundation of almost every other data structure. Strings, dynamic arrays, heaps, hash tables and matrices are all built on top of them, so understanding how an array is laid out in memory explains why some operations are instant and others are slow.

2What is it?

An array is an ordered collection of elements of the same type stored in one contiguous block of memory. Every element has an index starting at 0, and the address of element i can be computed directly: address(i) = base + i × elementSize.

3Why do we use it?

  • Instant (O(1)) access to any element by index.
  • Excellent cache performance because elements sit next to each other.
  • Very low memory overhead — no pointers per element.
  • The natural representation for sequences, tables, images and matrices.

4Real-world examples

  • Pixels of an image stored row by row.
  • Daily temperatures for a month, or marks of every student in a class.
  • A music playlist where you jump straight to song #7.
  • Lookup tables such as days-in-month[12].

5Internal working

When an array is created, the runtime reserves size × elementSize bytes in one piece. The variable holds the base address.

Reading arr[i] multiplies i by the element size, adds the base address and reads that location — one arithmetic step, regardless of the array's length.

Inserting or deleting in the middle requires shifting every later element by one position to keep the block contiguous, which costs O(n).

6Memory representation

A 5-element int array (4 bytes per int) starting at address 1000:

index:     0      1      2      3      4
         +------+------+------+------+------+
value:   |  5   |  3   |  8   |  1   |  2   |
         +------+------+------+------+------+
address: 1000   1004   1008   1012   1016

address(i) = 1000 + i * 4   ->   arr[3] lives at 1012

7Operations

OperationHow it works
Access arr[i]Compute the address directly.
Update arr[i] = xOverwrite one slot.
Search (unsorted)Check elements one by one.
Search (sorted)Binary search halves the range.
Insert / delete at endNo shifting needed (if space exists).
Insert / delete in middleShift later elements right / left.
ReverseTwo pointers swap from both ends.

8Visual explanation

Reverse an Arrayinteractive Open full visualizer
1
2
3
4
5
6
0
1
2
3
4
5
ComparingSwapping / movingCurrentSorted / finalFound / placed

Reverses an array in place with two pointers: swap the outermost pair, move both pointers inward, and stop when they meet.

1 / 17
Two SumOpen the step-by-step visualizerBubble SortOpen the step-by-step visualizerBinary SearchOpen the step-by-step visualizer

9Code implementation

Complete, runnable programs (verified in all four languages). JavaScript and Python can be executed in the Coding Playground.

function reverseArray(arr) {
  let left = 0;
  let right = arr.length - 1;
  while (left < right) {
    [arr[left], arr[right]] = [arr[right], arr[left]];
    left++;
    right--;
  }
  return arr;
}

const arr = [1, 2, 3, 4, 5, 6];
console.log("Reversed:", reverseArray(arr));

10Time complexity

OperationTime
Access arr[i]O(1)
Update arr[i] = xO(1)
Search (unsorted)O(n)
Search (sorted)O(log n)
Insert / delete at endO(1)
Insert / delete in middleO(n)
ReverseO(n)

11Space complexity

An array of n elements uses O(n) space. Most array algorithms (reverse, two-pointer scans, in-place sorts) need only O(1) extra space; techniques like hashing or prefix sums trade O(n) extra memory for speed.

12Advantages

  • O(1) random access.
  • Cache-friendly sequential access.
  • Simple and memory-efficient.
  • Easy to sort and binary search.

13Disadvantages

  • Fixed size (static arrays) — must know the capacity up front.
  • Inserting/deleting in the middle is O(n).
  • Resizing means allocating a new block and copying everything.
  • Can waste memory if allocated larger than needed.

14Common mistakes

  • Off-by-one errors: the last index is n − 1, not n.
  • Using `<=` instead of `<` in loop conditions and reading past the end.
  • Modifying an array while iterating over it.
  • Assuming arrays in JavaScript/Python are fixed-size (they are dynamic arrays).
  • Forgetting that `b = a` copies the reference, not the elements.

15Interview questions

Two SumEasyMove ZeroesEasyMaximum Subarray (Kadane's Algorithm)MediumBinary SearchEasy

16Practice exercises

  1. Find the second largest element in one pass.
  2. Rotate an array to the right by k steps in O(1) extra space.
  3. Remove duplicates from a sorted array in place.
  4. Merge two sorted arrays.
  5. Compute prefix sums and answer range-sum queries in O(1).

Try them in the Coding Playground.

On this page

  1. 01Introduction
  2. 02What is it?
  3. 03Why do we use it?
  4. 04Real-world examples
  5. 05Internal working
  6. 06Memory representation
  7. 07Operations
  8. 08Visual explanation
  9. 09Code implementation
  10. 10Time complexity
  11. 11Space complexity
  12. 12Advantages
  13. 13Disadvantages
  14. 14Common mistakes
  15. 15Interview questions
  16. 16Practice exercises