Programming Interview Cheat Sheet

crackr.dev

1Problem-Solving Framework

Follow this in every interview — interviewers grade on process, not just code

1
Clarify2-3 min

Inputs, outputs, constraints, edge cases. Ask about size of n.

2
Examples2 min

Walk through 2-3 examples. Include an edge case. Write them down.

3
Brute Force1 min

State the obvious O(n²) or O(n!) approach. Don't code it — just say it.

4
Optimize5-8 min

Identify the pattern. Pick the right data structure. State time/space.

5
Code15-20 min

Write clean code. Talk while you code. Use meaningful names.

6
Test3-5 min

Trace through your code with an example. Check edge cases.

2Pattern Recognition

See a signal in the problem → pick the right pattern

Sorted array / find targetBinary Search
Contiguous subarray / substringSliding Window
Two values that sum/meet conditionTwo Pointers
Top K / Kth largestHeap
Tree/graph traversalBFS / DFS
All permutations/combinationsBacktracking
Optimal substructure + overlapDynamic Programming
Ordering / dependenciesTopological Sort
Find cycle in linked listFast & Slow Pointers
Intervals overlap/mergeSort + Merge Intervals
String prefix matchingTrie
Connected components / unionUnion-Find
Range sum queriesPrefix Sum
Next greater/smaller elementMonotonic Stack
Shortest path (weighted)Dijkstra

3Data Structure Picker

What do you need? → Use this

What do you need?O(1) lookupOrderingPriorityLIFO/FIFOHashMapHashSetBSTSortedArrMin HeapMax HeapStackQueueAlso consider:Trie → prefixesGraph → relationshipsUnion-Find → groups

Quick Rules

Need O(1) access? → HashMap or Array

Need sorted order? → BST or sorted array + binary search

Need min/max fast? → Heap

Need undo/backtrack? → Stack

Need level-order? → Queue (BFS)

4Template: BFS & DFS

BFS — Level-order / shortest path

queue = [start]

visited = set(start)

while queue:

node = queue.popleft()

for neighbor in adj[node]:

if neighbor not in visited:

visited.add(neighbor)

queue.append(neighbor)

DFS — Explore all paths / backtrack

def dfs(node, visited):

if node in visited: return

visited.add(node)

# process node

for neighbor in adj[node]:

dfs(neighbor, visited)

5Template: Sliding Window

1
3
2
6
4
1
8
5
leftwindowright

left = 0

for right in range(len(arr)):

# expand: add arr[right] to window

while window is invalid:

# shrink: remove arr[left]

left += 1

# update answer

Two Pointers

left, right = 0, len(arr) - 1

while left < right:

if condition met:

# found answer

elif need bigger: left += 1

else: right -= 1

6Template: Binary Search

1left3579mid11131517right

left, right = 0, len(arr) - 1

while left <= right:

mid = (left + right) // 2

if arr[mid] == target:

return mid

elif arr[mid] < target:

left = mid + 1

else:

right = mid - 1

Use when: sorted array, monotonic function, answer space is bounded

Time: O(log n)   Space: O(1)

7Dynamic Programming Patterns

Most DP problems fall into one of these categories

1D Linear

Climbing stairs, house robber, coin change

dp[i] = f(dp[i-1], dp[i-2])
2D Grid

Unique paths, min path sum, dungeon game

dp[i][j] = f(dp[i-1][j], dp[i][j-1])
String

Edit distance, LCS, palindrome

dp[i][j] = f(s1[i], s2[j], neighbors)
Knapsack

0/1 knapsack, subset sum, partition

dp[i][w] = max(skip, take)
Interval

Matrix chain, burst balloons, palindrome

dp[i][j] = f(dp[i][k], dp[k][j])
Tree DP

Max path sum, house robber III, diameter

dfs returns (take, skip) for subtree

DP Checklist

Can I define the answer in terms of smaller subproblems?

Are there overlapping subproblems?

What's my base case?

What order do I fill the table?

8Edge Cases Checklist

Always check these before saying "I'm done"

Arrays

Empty [], single element, all same, already sorted, reverse sorted, duplicates

Strings

Empty "", single char, all same char, palindrome, spaces, unicode, case sensitivity

Numbers

0, negative, MAX_INT, MIN_INT, overflow, odd/even, floating point

Linked Lists

Empty, single node, cycle, two nodes, head/tail operations

Trees

Empty, single node, left-skewed, right-skewed, balanced, complete

Graphs

Disconnected, self-loops, parallel edges, single node, complete graph

9Complexity Quick Reference

HashMap get/setO(1)O(n)
Binary SearchO(log n)O(1)
Sort (merge/quick)O(n log n)O(n)/O(log n)
BFS / DFSO(V + E)O(V)
Heap push/popO(log n)O(n)
Build heapO(n)O(n)
Two PointersO(n)O(1)
Sliding WindowO(n)O(k)
BacktrackingO(2ⁿ) or O(n!)O(n)
DP (1D)O(n)O(n) → O(1)
DP (2D)O(n·m)O(n·m)
DijkstraO(E log V)O(V)
TimeSpace

10Template: Backtracking

Permutations, combinations, subsets, N-Queens

def backtrack(path, choices):

if goal reached:

result.append(path[:])

return

for choice in choices:

if valid(choice):

path.append(choice) # choose

backtrack(path, ...) # explore

path.pop() # unchoose

Key: Choose → Explore → Unchoose (undo the choice)

11Top Interview Mistakes

Jumping straight to code

Clarify → Examples → Brute Force → Optimize → Code

Not talking through your thinking

Narrate your thought process — silence = red flag

Ignoring edge cases

Empty input, single element, duplicates, negative numbers

Over-engineering the solution

Start simple. Optimize only after brute force works.

Not testing your code

Trace through with a small example before saying done

Forgetting time/space complexity

Always state it before coding. Update if you optimize.

Off-by-one errors

Be deliberate with < vs <=, i vs i+1, inclusive vs exclusive

1245-Minute Interview Breakdown

Clarify
Examples
Approach
Code
Test
Q&A
3m2m7m20m5m5m

Pro Tips

If stuck for 2+ minutes, ask for a hint
Write helper functions — show you write clean code
Name variables well — arr, target, result, not a, b, c
If brute force works and you have time, optimize after

The Complete Programming Interview Cheat Sheet

This programming interview cheat sheet covers the patterns, templates, and strategy you need to ace coding interviews at top tech companies. Instead of memorizing hundreds of LeetCode solutions, learn to recognize the 15 core patterns and apply reusable code templates.

Our coding interview cheat sheet includes a step-by-step problem-solving framework, pattern recognition guide (see signal → use pattern), code templates for BFS/DFS, sliding window, two pointers, binary search, backtracking, and dynamic programming, a data structure picker decision tree, edge cases checklist by data type, complexity quick reference, common interview mistakes to avoid, and time management strategy for 45-minute interviews.

Whether you're preparing for Google, Meta, Amazon, or any top tech company, this technical interview cheat sheet gives you the complete playbook — patterns, templates, and strategy in one view.

Programming Interview FAQ

What is a programming interview cheat sheet?expand_more

A programming interview cheat sheet is a quick-reference guide covering the patterns, templates, and strategies you need to solve coding problems in technical interviews. It includes pattern recognition (see X → use Y), code templates for common algorithms (BFS, DFS, binary search, sliding window, DP), edge cases to check, complexity analysis, and tips for communicating with your interviewer.

What are the most common coding interview patterns?expand_more

The most common patterns are: Two Pointers (sorted arrays, pair sums), Sliding Window (subarrays, substrings), Binary Search (sorted data, monotonic functions), BFS/DFS (trees, graphs), Dynamic Programming (optimal substructure + overlapping subproblems), Backtracking (permutations, combinations), Merge Intervals (overlapping ranges), Monotonic Stack (next greater element), and Prefix Sum (range queries). Recognizing which pattern to apply is the #1 skill.

How should I approach a coding interview problem?expand_more

Follow the 6-step framework: (1) Clarify — ask about inputs, outputs, constraints, and edge cases. (2) Examples — work through 2-3 examples including an edge case. (3) Brute force — state the obvious solution and its complexity. (4) Optimize — identify the right pattern and data structure. (5) Code — write clean code while narrating your thinking. (6) Test — trace through your code with an example and check edge cases.

What are the most important Dynamic Programming patterns?expand_more

DP problems fall into 6 main categories: 1D Linear (climbing stairs, house robber), 2D Grid (unique paths, min path sum), String DP (edit distance, LCS), Knapsack (subset sum, partition), Interval DP (matrix chain, burst balloons), and Tree DP (max path sum, house robber III). For each, identify the state, transition, and base case.

What edge cases should I check in coding interviews?expand_more

Always check: empty input ([], ""), single element, all same values, already sorted / reverse sorted, duplicates, negative numbers, zero, MAX_INT overflow, odd vs even length. For trees: empty tree, single node, left/right-skewed. For graphs: disconnected components, self-loops, cycles. Mentioning edge cases proactively shows thoroughness.

How do I manage time in a 45-minute coding interview?expand_more

Rough breakdown: Clarify requirements (3 min), work through examples (2 min), discuss approach and complexity (7 min), write code (20 min), test and debug (5 min), Q&A (5 min). If you're stuck for more than 2 minutes, ask for a hint — interviewers expect this. If brute force works and you have time, optimize after.

Ready to ace your coding interview?

Practice with an AI interviewer that tests pattern recognition, code quality, and communication — just like a real interview.

Try Crackr freearrow_forward

Continue learning