StudyDSA logoStudyDSA

Command Palette

Search for a command to run...

Sign InSign Up
Sign Up

Two PointersFast & Slow PointersSliding WindowBinary SearchPrefix SumsRecursionTree TraversalSortingBacktracking1D Dynamic ProgrammingGreedyIntervalsBit OperationsDepth-First SearchBreadth-First Search2D Dynamic ProgrammingTwo-Heaps PatternDijkstra's AlgorithmA* Search AlgorithmFloyd-Warshall's AlgorithmBellman-Ford AlgorithmPrim's AlgorithmKruskal's AlgorithmTopological SortDAG Shortest Path

Definition
StudyDSA

Where complexity meets clarity.
By Armas Zarra.

Topics

  • Data Structures
  • Algorithms
  • Big-O Notation
  • Robotics
  • AI Research
  • Machine Learning

Practice

  • Blind 75
  • LeetCode 75
  • NeetCode 150

Legal

  • Privacy Policy
  • Terms of Service

© 2026 Armas Films LLC

IntroductionAlgorithms

Algorithms

A step-by-step procedure for solving a problem or accomplishing a task.

Definition

The same problem on the same machine can take a second or a week to solve. What separates the two is rarely the hardware. It's the procedure: the exact sequence of steps the program takes to reach an answer. An algorithm is that procedure, a step-by-step method for solving a problem, written precisely enough for a computer to follow. Choosing a good one is the cheapest performance upgrade in computing, and it's a skill you can learn one pattern at a time.

Learning Path

Work through these in order, since each technique leans on the ones that come before it.

Basics

Most of the basics exist to kill a nested loop. Two pointers, fast and slow pointers, and sliding window each collapse a quadratic scan into one pass, while binary search and prefix sums buy their speed from structure the data already has: sorted order in one case, precomputed sums in the other. Recursion is the one idea here bigger than a trick, and tree traversal and sorting are the first places it pays off.

Two Pointers

→

Checks every pair without visiting every pair. Two markers closing in from opposite ends replace an entire nested loop.

1
3
5
7
9
11
L →← R

Fast & Slow Pointers

→

Detect an infinite loop without marking a single node. If the fast runner ever laps the slow one, the list has a cycle.

+1+2ABCDEFslowfast

Sliding Window

→

When one element enters and one leaves, the rest of the work is reusable. That single trick turns quadratic scans linear.

2
1
5
1
3
2
length = 3

Binary Search

→

Find any entry among a billion sorted items in about 30 looks. Each guess throws away half of what remains.

1
3
5
7
9
11
13
L
mid
R

Prefix Sums

→

Pay for one pass up front and the sum of any range becomes a single subtraction, no matter how often you ask.

1
2
3
4
5
↓↓↓↓↓
1
3
6
10
15

Recursion

→

A function that calls itself sounds like it should run forever. Add a base case and it becomes the cleanest way to shrink a problem down to nothing.

f(4)f(3)f(2)return 1

Tree Traversal

→

Every node visited exactly once, none missed, none repeated. The order you choose decides what the walk computes.

1253467

Sorting

→

The setup move for everything else: once data is sorted, searching, merging, and finding duplicates all get cheap.

Intermediate

Single-pass tricks stop working once a problem forces you to make choices. Backtracking explores every choice and unwinds the bad ones, dynamic programming notices it keeps re-solving the same subproblems and stores them, and greedy bets that the locally best choice never needs unwinding at all. Intervals and bit operations are sharper tools for narrower jobs, and depth-first and breadth-first search carry all of this onto graphs.

Backtracking

→

Try a choice, follow it until it dead-ends, undo it, try the next. Brute force with an undo button, and it solves Sudoku.

✕✕

1D Dynamic Programming

→

When each answer builds on the previous few, store them once and stop recomputing. Exponential problems collapse to a single pass.

0
1
1
2
3
5
8
0
1
2
3
4
5
6

Greedy

→

Grab the best option available and never reconsider. For the right problems, that shortcut is provably optimal.

10¢
50¢
1¢
25¢
5¢
pick largest first

Intervals

→

Meetings, bookings, IP ranges. Sort by start time and most overlap problems untangle in a single sweep.

Bit Operations

→

Thirty-two yes/no answers packed into one integer, each readable in a single operation. This is the machine's native language.

A
10110100
B
11010010
&
10010000

Depth-First Search

→

Commit to one path until it bottoms out, then back up and try the next. The recursion stack remembers the way home.

Breadth-First Search

→

Explores in expanding rings, so the first time it touches a node is also the shortest way there. No weights needed.

ABCDEFGL0L1L2
Advanced

Real maps have weights, and weights break plain breadth-first search. Dijkstra repairs it with a priority queue, A* adds a sense of direction, Bellman-Ford pays extra time to survive negative edges, and Floyd-Warshall answers every pair at once. Prim and Kruskal build the cheapest network rather than the shortest route, while topological sort orders a DAG by its dependencies, the same property that lets DAG shortest paths run in linear time. The remaining two, 2D dynamic programming and the two-heaps pattern, stretch the memo table and the heap to problems with more moving parts.

2D Dynamic Programming

→

When a problem has two moving parts, the memo grows into a grid. Edit distance and longest common subsequence live here.

m
a
s
t
e
r
m
i
n
d
m
1
1
1
1
1
1
1
1
1
1
i
1
1
1
1
1
1
1
2
2
2
n
1
1
1
1
1
1
1
2
3
3
d
1
1
1
1
1
1
1
2
3
4

Two-Heaps Pattern

→

Track the running median of a stream without ever sorting it. Two heaps lean against each other and the answer sits where they meet.

531max5.5median689min

Dijkstra's Algorithm

→

The idea behind your GPS: always extend the cheapest path found so far, and the first arrival is guaranteed optimal.

231745SABCD

A* Search Algorithm

→

Dijkstra with a sense of direction. A hint about where the goal lies lets it skip most of the map and still return the best path.

·
·
·
·
·
S
E

Floyd-Warshall's Algorithm

→

Every shortest path between every pair of nodes, computed by three nested loops you can write from memory.

A
B
C
D
A
0
3
∞
7
B
3
0
2
∞
C
∞
2
0
1
D
7
∞
1
0

Bellman-Ford Algorithm

→

Shortest paths even when some edges have negative cost, plus a built-in alarm for cycles that make the question unanswerable.

4235-3SD

Prim's Algorithm

→

Wires every node together for the least total cost by growing one network outward, always taking the cheapest edge.

1232

Kruskal's Algorithm

→

Same goal as Prim, opposite move: sort every edge, then keep adding the cheapest one that doesn't close a cycle.

1234

Topological Sort

→

Orders tasks so every prerequisite comes first, or proves that no valid order exists. Build systems run on this.

123456

DAG Shortest Path

→

With no cycles to fear, shortest paths stop needing Dijkstra. One topological pass and every distance falls out in linear time.

213542SD
Importance

Every program is a pile of algorithms, so "algorithms matter" is almost too obvious to say. What's worth spelling out is exactly what a good one buys you:


Algorithms optimize the efficiency of programs, enabling tasks to be completed quickly and using fewer resources. The gap between O(n) and O(n²) is invisible at ten items, but at ten million it's the difference between a fast response and a request that never finishes.

Complexity

Algorithm complexity measures how much time and space an algorithm needs as its input size n grows. It's usually written in Big-O notation, which describes the upper bound of an algorithm's running time or space requirements in the worst-case scenario.

TimeSpacefastest ↑least memory ↑LookupLookupCacheCacheIn-placeIn-placeStreamStream

These two costs usually trade off against each other. Faster algorithms tend to use more memory, and memory-efficient algorithms tend to take more time.

Problem Solving

Picking the algorithm is half the problem. The right one makes the solution short and fast, and the wrong one fights you the whole way. Four things to weigh before you commit:

1. Problem Type

Different algorithms are suited to different types of problems. Dynamic programming wants optimization problems that break into reusable subproblems, sliding window wants contiguous ranges, and BFS wants shortest paths. Name the problem type and the candidate list shrinks fast.

2. Data Structure

The data structure underneath can make or break an algorithm. Dijkstra's drops from O(V²) to O((V + E) log V) just by swapping an array for a heap, same algorithm, different backing.

3. Space-Time Tradeoff

Speed usually costs memory, and saving memory usually costs speed. A hashmap turns an O(n) search into O(1) by storing an extra copy of everything, and prefix sums and memoization make the same trade. Decide which resource is scarce before you choose.

4. Scalability

Ask how big the input can get before you commit. An O(n²) approach breezes through a sample input of 50 items and times out the moment n hits 100,000, and the constraints in a problem statement usually tell you which case you're in.