DSA quick reference

Which structure to reach for, and its cost.

If the question saysTry
Sorted arrayBinary search or two pointers
Longest or best stretchSliding window
Have I seen this before?Hash set or map
Matching brackets, undoStack
Shortest path, fewest stepsBFS
Top k itemsHeap
Count the ways, best totalDynamic programming
All combinationsBacktracking
StructureTypical cost
ArrayIndex O(1), search O(n)
Hash mapInsert and lookup O(1) on average
Balanced treeInsert and lookup O(log n)
HeapInsert O(log n), peek O(1)
Linked listAdd at front O(1), search O(n)
โ† Dynamic programmingData Structures & Algorithms learning path โ†’