Learn to Code
All tracks
Learn to Code

Free lessons from scratch to pro — every language, databases and DevOps.

AboutContactPrivacy PolicyTerms of Use

Your progress is saved in this browser. © 2026 Learn to Code

DSA
Data Structures & Algorithms
0/15 completed
Beginner
Big-O and complexityArrays and stringsHash maps and setsTwo pointers and sliding windows
Intermediate
Linked listsStacks and queuesRecursion and backtrackingSorting algorithmsBinary search
Advanced
Trees and binary search treesHeaps and priority queuesGraphs: BFS and DFSDynamic programming
Pro
Shortest paths, topological sort and union-findSolving problems in coding interviews
Tracks / Data Structures & Algorithms / Beginner

Arrays and strings

The array is the most fundamental data structure: items stored side by side in memory, each reachable by its index. Python's list is a dynamic array, and strings are arrays of characters. Most interview problems start here.

How arrays work

An array is one contiguous block of memory. To find item i, the computer calculates start + i × item_size, so reading any index is O(1), however long the array is.

Operation Python Cost
Read or write by index a[i], a[i] = x O(1)
Append at the end a.append(x) O(1) amortised
Remove from the end a.pop() O(1)
Insert or remove at the front or middle a.insert(0, x), a.pop(0) O(n): everything after it shifts
Search for a value x in a, a.index(x) O(n)
Slice a[i:j] O(j − i): it copies
Python · editable

When you need fast operations at both ends, use collections.deque (next lessons).

Dynamic arrays grow by doubling

A list reserves extra room. When it fills up, it allocates a bigger block (about 1.125–2× larger) and copies everything over. Copies are rare, so appends are amortised O(1):

Python · editable

Classic array techniques

Prefix sums: range sums in O(1)

Precompute running totals once (O(n)), then any range sum is a subtraction:

Python · editable

In-place reversal and rotation

Python · editable

Kadane's algorithm: maximum subarray

Find the contiguous run with the largest sum, in one pass. At each position, either extend the current run or start fresh:

Python · editable

O(n) instead of the O(n²) of checking every subarray.

Strings

Python strings are immutable: every "change" makes a new string. Building a long string with += in a loop can cost O(n²) in total; collect pieces in a list and "".join() them once:

Python · editable

Counting characters

Many string problems are really counting problems:

Python · editable

Palindromes

Python · editable

2D arrays (matrices)

Grids appear everywhere: images, game boards, maps. Rotate a square matrix 90° clockwise by transposing and then reversing each row:

Python · editable

Watch out for a classic Python trap: [[0] * 3] * 3 creates three references to the same row. Use [[0] * 3 for _ in range(3)].

Practice

  1. Write move_zeros(nums) that moves all zeros to the end in place, keeping the order of the other numbers (O(n), O(1) space).
  2. Return the product of all elements except nums[i] for each i, without using division (hint: prefix and suffix products).
  3. Write compress("aaabccdddd") → "a3b1c2d4".
✎ Check your understanding

1.Why is reading a[500000] from a Python list O(1)?

2.What does inserting at the front of a list cost?

3.What does Kadane's algorithm find?

← Big-O and complexity