skip to content

Problem Archetypes

A handful of recurring problem families cover most interview DP. Each family is defined by a recurrence you can state and defend — recognize the family and the state, transition, and complexity follow almost mechanically.

part ofData structures & algorithmsoverview, primer and where to startread it →
on this pageshow

questions

page 2 of 2

Aligning two 100k-character documents needs 10^10 table cells. What do you change before writing the loop?

level: seniorimportance: should knowfreq 38%

basics

~20 s

Attack the cell count, not the memory: quadratic time bites before space does. Trim the shared prefix and suffix, compare coarser units such as fingerprinted lines, and if only k edits matter, fill just the band where |i-j| <= k, which is O(n*k).

open as a page

When does an interval DP table beat O(1)-space center expansion for palindromes in long gene reads?

level: seniorimportance: should knowfreq 40%

basics

~20 s

Only when something else will query the table. Both methods cost O(n^2) time for one longest symmetric run, but the table also costs O(n^2) memory. It pays off when an enclosing computation asks "is i..j symmetric?" many times, or when you need counts over all intervals.

open as a page

After the O(n log n) longest-increasing-subsequence scan, what does the tails array actually contain?

level: seniorimportance: nice to knowfreq 34%

basics

~20 s

The tails array holds at position k the smallest value that can end an increasing subsequence of length k+1. Its final length is the answer, but its contents are usually not a real subsequence of the input.

open as a page

Your exact subset-DP route planner tops out near 20 stops, but dispatch now needs 35 — how do you decide what replaces it?

level: principalimportance: nice to knowfreq 18%

basics

~20 s

Subset DP's ceiling is exponential memory, so no tuning reaches 35 stops — the method must change. The real decision is what the last few percent of route quality is worth against a planner the team can operate and maintain.

open as a page

Your planner parenthesizes a fixed chain of joins with interval DP — when do you abandon the exact search?

level: principalimportance: nice to knowfreq 22%

basics

~20 s

Abandon it when planning cost stops buying execution savings: when quadratic memory or cubic time breaks the per-request budget, or when the size estimates feeding the cost function are so uncertain that the exact optimum is optimal only for a fiction.

open as a page

When is a rerooting tree DP worth its complexity over simply running one traversal per candidate root?

level: principalimportance: nice to knowfreq 20%

basics

~20 s

Rerooting answers every node in two linear passes instead of one traversal per node, turning quadratic work into linear. Take it when the measured input size and recompute frequency actually miss the latency budget; below a few thousand nodes the simpler per-root loop usually wins and is far easier to keep correct.

open as a page

showing 31–37 of 37