Skip to content

Blog

Plain answers about algorithms, interviews and performance.

44 posts

Posts

Latest in Complexity

What is the difference between Big O and Big Theta

The difference between Big O and Big Theta is the difference between a ceiling and a measurement. Here is what each claims, worked on real step counts, and why almost everyone says Big O when they mean Big Theta.

By Vishy9 min read
Craft

How to optimize a brute force solution

How to optimize a brute force solution: audit its loops for repeated and discarded work, then fix each kind with the matching tool. A worked example takes a 1.3 trillion operation scan down to 20,000 steps.

10 min read
Complexity

Is slicing O(n) in Python?

Is slicing O(n) in Python? Yes: lists, strings, tuples and bytes copy the slice, so the cost is O(k) for k elements. Here is how that quietly turns fast algorithms slow, and how to avoid it.

10 min read
Craft

Why do I get maximum recursion depth exceeded

Seeing maximum recursion depth exceeded in Python means your calls nested past the interpreter's cap of 1000, from a missing base case or a genuinely deep input. Here is how to tell which, and the four fixes that actually work.

10 min read
Craft

What is a stable sort and why does it matter?

What is a stable sort? It is a sort that keeps equal-keyed records in their original relative order. Here is why that one guarantee powers multi-key sorting and radix sort, which algorithms have it, and how to get it when yours does not.

10 min read
Complexity

How to reduce space complexity in dynamic programming

How to reduce space complexity in dynamic programming: find the window of earlier states each state reads, then keep only that window. Worked numbers for LCS and knapsack, the iteration-order trap, and what you lose when you shrink the table.

9 min read
Performance

Why is list.pop(0) slow in Python?

Why is list.pop(0) slow in Python? A list is a contiguous array, so removing the first item shifts every other item left, costing O(n) per call. Here is the arithmetic, the fix with deque, and when it truly does not matter.

10 min read
Craft

How to detect a cycle in a linked list

How to detect a cycle in a linked list: use two pointers at different speeds, or a hash set of visited nodes. This essay proves why the fast and slow pointers must meet, traces a 10-node example, and compares the costs.

10 min read
Data structures

What is union find and when should you use it

What is union find? It is a structure that tracks which items share a group, with near-constant amortized cost for merging groups and checking membership. Here is how it works, why it is fast, and when it beats BFS or DFS.

10 min read
Data structures

What is the difference between a heap and a binary search tree

The difference between a heap and a binary search tree comes down to what order each one promises. This essay walks through the invariants, a worked example with a million elements, and a simple rule for choosing between them.

10 min read
Craft

What is the difference between backtracking and DFS

The difference between backtracking and DFS comes down to three things: whether the tree already exists, whether state is undone on the way back, and whether branches are pruned. Worked numbers for permutations and eight queens show what each choice costs.

10 min read
Craft

What is the difference between backtracking and dynamic programming

The difference between backtracking and dynamic programming is what each remembers: backtracking keeps the whole path, DP keeps only a small state. Here is how to tell which one a problem wants, with a worked example that shrinks 155 million calls to 256 states.

10 min read
Data structures

When to use a trie

When to use a trie: when your questions are about prefixes, not whole keys. Here is how it compares with a hash set and a sorted array, with memory and operation counts for a 100,000 word dictionary.

11 min read