Skip to content

Blog, page 2

Plain answers about algorithms, interviews and performance.

44 posts

Posts

Craft

What is the difference between memoization and tabulation

The difference between memoization and tabulation is direction: top-down caching of a recursion versus a bottom-up loop over a table. Here is what that changes in work done, stack depth, memory, and which one to reach for.

9 min read
Craft

What is a monotonic stack

What is a monotonic stack? A stack that stays sorted by popping anything that would break the order, which turns every next-greater-element question into one O(n) pass. Here is how it works, with full traces and the mistakes that cost people offers.

11 min read
Complexity

How to find the time complexity of a recursive function

A working method for finding the time complexity of a recursive function: write the recurrence, draw the call tree, and check it against the master theorem. Includes worked counts for merge sort, naive Fibonacci and a slicing bug.

13 min read
Complexity

Why is quicksort faster than merge sort?

Why is quicksort faster than merge sort when merge sort does fewer comparisons? A worked example with a million integers shows where the time goes: data movement, cache footprint and branch misprediction, not comparison counts.

10 min read
Complexity

Why is quicksort faster than merge sort in practice

Why is quicksort faster than merge sort in practice, when both are O(n log n) on average? Quicksort actually does more comparisons. It wins on memory traffic, footprint and loop tightness, and loses the moment comparisons get expensive or stability matters.

9 min read
Complexity

Why is heapify O(n) and not O(n log n)

Why is heapify O(n) when each sift-down can cost O(log n)? Because the work per node depends on its height, and half the nodes have height zero. Here is the counting argument with real numbers, and the reason repeated insertion does not get the same bound.

9 min read
Craft

When to use prefix sum vs sliding window

Prefix sum vs sliding window is a choice about monotonicity and negative numbers, not about how the problem looks. Here is the test I use, with a worked example, operation counts, and a comparison table.

11 min read
Craft

How to tell if a problem needs greedy or dynamic programming

Choosing between greedy or dynamic programming comes down to whether you can prove the first choice is safe. Here is a repeatable test, a worked coin change example with real numbers, and the signals that point each way.

10 min read
Complexity

Why does Dijkstra not work with negative edges

Why does Dijkstra not work with negative edges? It settles a node the moment it leaves the heap, and a negative edge can make a later path cheaper. Here is a four-node counterexample, the exact failure, and what to use instead.

11 min read
Interviews

Is Python too slow for coding interviews?

Is Python too slow for coding interviews? Almost never, but there are three specific situations where it bites. Here are the real numbers, the operation budgets, and the fixes that matter.

9 min read
Craft

What is the difference between sliding window and two pointers

The difference between sliding window and two pointers is what the pointers represent: a sliding window summarises a contiguous range, while two pointers discard candidate pairs. Here is how to tell them apart, with traces, operation counts and a counterexample.

9 min read
Craft

When to use BFS vs DFS

When to use BFS vs DFS depends on what you need back: shortest steps, full exploration, or dependency order. Here is a decision rule, a worked grid example with real counts, and the memory trade-off that actually bites.

9 min read