Interview's MateDSA Visualizer
Interview's Mate

DSA Visualizer

Learn by watching algorithms run

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
All topics
FOUNDATIONS

Arrays

Contiguous memory with O(1) access by index - the base most patterns build on.

Beginner10 problems3 interactive subtopicsO(1) access · O(n) insert
Practice in AI DSA
WalkthroughOverviewRecognize itMistakes & edge casesKnowledge check (3)Practice
Algorithms & custom examples (3)Open in full playground →
STEP 1/5Start the writer at 0
write = 0
01234
0
1
0
3
12
write
  • active this step
  • result
  • not reached yet
Space Play←→ StepR Replay
1 / 5
PYTHONnums = [0, 1, 0, 3, 12]
1def move_zeroes(nums):
▶2 write = 0
3 for read in range(len(nums)):
4 if nums[read] != 0:
5 nums[write], nums[read] = nums[read], nums[write]
6 write += 1
7 return nums
STEP 1 OF 5Setup

Start the writer at 0

Everything left of write will be the finished, non-zero part.

VARIABLES

read
-
write
0
nums
[0, 1, 0, 3, 12]
02

Intuition & complexity

The core mental model, trade-offs, and Big-O cost.

What it is

An array stores elements side by side, so any index is read in constant time. Python lists are dynamic arrays that grow as you append.

Why it matters

Fast indexed access and cache-friendly scans make arrays the default container for sequences.

Think of it like this

A row of numbered lockers: you can open locker 42 directly, but adding one in the middle means shifting everyone after it.

Use it when

  • You need fast access by position
  • You scan the data in order once or a few times
  • You can update values in place

Avoid it when

  • Frequent inserts or deletes at the front or middle - each is O(n)
  • Membership checks on large data - use a set or dict

Complexity cheatsheet

OperationTimeSpace
Read or write nums[i]O(1)O(1)
Append to the endO(1) amortizedO(1)
Insert or delete at the frontO(n)O(1)

Often confused with: linked lists - cheap inserts once you hold the node, but O(n) access by index.

03

How to recognize it

Clues in interview prompts that point straight to this pattern.

subarrayin placeindexrotaterunning total
  • O(1) extra space usually means rewriting the array in place
  • Values in the range 1..n hint that indices can double as markers
04

Mistakes & edge cases

Catch common off-by-one bugs and corner cases before submitting.

Common mistakes

  • Removing items from a list while looping over it - indices shift under you
  • Calling insert(0, x) or pop(0) in a loop, turning O(n) into O(n²)
  • Off-by-one errors such as range(len(nums) - 1)

Edge cases to test

  • Empty array or a single element
  • All zeros, or no zeros at all
  • Negative numbers and duplicates
Interview tip: Ask whether you may modify the input - in-place answers often save O(n) memory.
05

Knowledge check

Quick check to lock in the intuition.

Question 1 of 3

What does nums.pop(0) cost on a Python list of n items?

06

Practice

Apply what you just watched on real interview problems.

Generate Array problem in AI DSA Practice →
  • BeginnerMove ZeroesLeetCode 283 ↗ (opens in a new tab)
  • IntermediateRotate ArrayLeetCode 189 ↗ (opens in a new tab)
  • AdvancedFirst Missing PositiveLeetCode 41 ↗ (opens in a new tab)
Time & Space ComplexityStrings