Two Pointers
Use it when you see: a sorted array, “find a pair/triplet summing to target,” “remove duplicates in place,” or “is it a palindrome.”
The hard part of a coding interview usually isn’t the algorithm — it’s recognizing which one the problem wants. This is a recognition guide: the 17 core patterns with the exact phrases that signal each one, plus a full Big-O reference. Search it, bookmark it, use it the night before.
Map the wording of the question to a pattern. The “Use it when you see” phrases are the fastest way to narrow down your approach in the first 60 seconds.
Use it when you see: a sorted array, “find a pair/triplet summing to target,” “remove duplicates in place,” or “is it a palindrome.”
Use it when you see: “longest/shortest contiguous substring/subarray,” “max sum of size k,” or “at most K distinct.”
Use it when you see: “detect a cycle in a linked list,” “find the middle node,” or “happy number.”
Use it when you see: “overlapping intervals,” “merge,” “meeting rooms,” or “insert an interval.”
Use it when you see: an array of numbers in the range 1..n, “find the missing/duplicate number,” or “in-place, O(1) space.”
Use it when you see: “reverse a linked list,” “reverse a sublist,” or “reverse every k nodes.”
Use it when you see: “level order traversal,” “shortest path in an unweighted graph,” or “minimum number of steps.”
Use it when you see: “all paths,” “connected components,” “does a path exist,” or “flood fill.”
Use it when you see: “median of a data stream,” “find the median,” or “balance two halves.”
Use it when you see: “all combinations/permutations/subsets,” “generate all,” “N-Queens,” or “Sudoku.”
Use it when you see: a sorted (or rotated sorted) array, “find element/boundary,” or a required O(log n).
Use it when you see: “K largest/smallest/most frequent,” or “the Kth element.”
Use it when you see: “merge K sorted lists/arrays,” or “smallest range covering K lists.”
Use it when you see: “course schedule,” “build order,” “dependencies,” or a DAG.
Use it when you see: “count the ways,” “min/max cost,” “can you reach,” or “longest/shortest with choices.”
Use it when you see: “maximize/minimize” with a single forward pass, “jump game,” or interval scheduling.
Use it when you see: “number of connected components/islands,” “are two nodes connected,” or “grouping.”
No pattern matches that search. Try a phrase from the problem, like “sorted array” or “all permutations.”
Average-case time complexity of the common operations. Reach for the hash map by default; know where the others win.
| Data structure | Access | Search | Insert | Delete | Space |
|---|---|---|---|---|---|
| Array | O(1) | O(n) | O(n) | O(n) | O(n) |
| Dynamic array | O(1) | O(n) | O(1)* | O(n) | O(n) |
| Hash table / map | — | O(1) | O(1) | O(1) | O(n) |
| Stack / Queue | O(n) | O(n) | O(1) | O(1) | O(n) |
| Linked list (singly) | O(n) | O(n) | O(1) | O(1) | O(n) |
| Balanced BST | O(log n) | O(log n) | O(log n) | O(log n) | O(n) |
| Heap / Priority queue | — | O(n) | O(log n) | O(log n) | O(n) |
| Trie | — | O(L) | O(L) | O(L) | O(A·N) |
* amortized · L = key length · A = alphabet size · worst-case for hash table and BST is O(n).
Know these cold — interviewers expect you to state the complexity of what you write.
| Algorithm | Time | Space | Note |
|---|---|---|---|
| Binary search | O(log n) | O(1) | Array must be sorted |
| Merge / Heap sort | O(n log n) | O(n)/O(1) | Guaranteed n log n |
| Quicksort (avg) | O(n log n) | O(log n) | Worst case O(n²) |
| BFS / DFS | O(V+E) | O(V) | Graph traversal |
| Dijkstra (heap) | O((V+E) log V) | O(V) | Non-negative weights |
| Topological sort | O(V+E) | O(V) | DAG only |
| Subsets / Permutations | O(2^n)/O(n!) | O(n) | Exponential by nature |
Read for trigger phrases. “Sorted array” + “find a pair” → Two Pointers. “Longest contiguous substring” → Sliding Window. “Numbers 1 to n” → Cyclic Sort. “All combinations” → Backtracking. “Course schedule / dependencies” → Topological Sort. The cards above list these phrases per pattern — that recognition step is usually the whole game.
O(1) on average, O(n) worst case under heavy collisions. Insert and delete are also O(1) average — which is why a hash map is the default for membership checks and frequency counts.
These 17 cover the large majority of questions asked at most companies. Master the recognition triggers and one clean template per pattern, and you can decompose almost any new problem into something you’ve seen.
Yes — completely free, no signup, works on any device. Bookmark it or link to it. It’s published by CoPilot Interview as a study reference.
Knowing the cheat sheet and performing under a live screen-share are different skills. CoPilot Interview is a desktop AI assistant that gives real-time, structured help during actual coding rounds — approach, code, and Big-O — with a permanent free tier. Honest prep support, not concealment.
See how it works