Using Heuristics to Dominate Coding Interviews
If you’re a self-taught programmer like me, you’re probably wondering how people with Computer Science degrees can so easily come up with answers in high-stress technical interviews. I looked back at the field where I got my degree – Finance – and realized they were probably approaching technical interviews the exact same way.
It’s all in heuristics and memorization.
Every single programming question you’ll ever come across is just one of the same 10 problems. Other things work like this too – for example, coming across the same 10 problems in customer service, or the same 10 patient issues in medicine. There are of course more problems than just those 10, but at that point you will be as clueless as the expert.
It’s this knowledge of the most common problems that set university-trained programmers ahead of those who are self-taught. But if you are self-taught, you likely don’t even know where to start.
Here’s where we get into the meat of this article.
(Psst, it’s probably important to learn what these algorithms are before diving into heuristics!)
The Top Ten Heuristics To Know1. “Find”, “Exists”, “Contains”, “Frequency” → Use a HashMap/SetKeywords: “contains”, “duplicate”, “unique”, “frequency”, “most common”, “anagram”Use:
HashMap if you just need to count the frequency.
HashSet for checking duplicates and/or membership.
Leetcode Examples:
Two Sum
Valid Anagram
2. “K-th”, “Top K”, “Smallest/Largest” → Use a Heap (Priority Queue)Keywords: “Kth”, “top K”, “largest”, “smallest”, “priority”
Use:
Min Heap to keep the largest K elements.
Max Heap for smallest K, or for custom sorting.
Leetcode Examples:
Kth Largest Element in an Array
Top K Frequent Words
3. “Sorted”, “Binary”, “Search” → Use Binary SearchKeywords: “search”, “sorted”, “find”, “minimum”, “maximum”
Use:
Search in a sorted array or apply binary search on the answer space (e.g. “min X that satisfies condition”).
Leetcode Examples:
Search Insert Position
Median of Two Sorted Arrays
4. “Step by Step”, “Minimum Number of ___”, “Transform A → B” → Use BFSKeywords: “shortest”, “fewest steps”, “transform”, “levels”
Use:
Breadth-first search for unweighted shortest paths.
Leetcode Examples:
Word Ladder
Rotting Oranges
5. “All Possibilities”, “All Combinations”, “Permutations” → Use BacktrackingKeywords: “all”, “generate”, “combinations”, “permutations”, “subset”
Use:
Recursive DFS with backtracking.
Leetcode Examples:
Subsets
Combination Sum
N-Queens
6. “Max/Min Path”, “Overlapping Subproblems”, “Optimal” → Use Dynamic ProgrammingKeywords: “max/min”, “longest”, “shortest”, “ways”, “count”, “how many”
Use:
Break problem into subproblems; cache intermediate results (memoization/tabulation).
Leetcode Examples:
Climbing Stairs
Longest Increasing Subsequence
Coin Change
7. “Interval”, “Schedule”, “Merge”, “Overlap” → Use Greedy or Sort + Sweep LineKeywords: “interval”, “overlap”, “schedule”, “merge”, “room”
Use:
Sort by start/end times, make greedy decisions.
Leetcode Examples:
Merge Intervals
Meeting Rooms
Non-overlapping Intervals
8. “Tree”, “Hierarchy”, “Parent/Child” → Use DFS/BFS or Tree RecursionKeywords: “tree”, “binary tree”, “path”, “root”, “leaf”
Use:
DFS recursion or level-order BFS
Leetcode Examples:
Binary Tree Level Order Traversal
Lowest Common Ancestor
9. “Graph”, “Connection”, “Island”, “Network” → Use DFS/BFS/Union-FindKeywords: “connected”, “island”, “network”, “friend group”, “component”
Use:
DFS/BFS or Union-Find for connected components
Leetcode Examples:
Number of Islands
Graph Valid Tree
Redundant Connection
10. “Sliding Window”, “Substring”, “Subarray” → Use Two Pointers / Sliding WindowKeywords: “longest”, “shortest”, “substring”, “subarray”, “window”
Use:
Expand and contract a window using two pointers.
Leetcode Examples:
Longest Substring Without Repeating Characters
Minimum Window Substring
Transforming the Heuristics into a Flowchart
These heuristics, on their own, are still rather simple – when looking at keywords alone, 3 different algorithms match up to the term “shortest”! Besides that, there’s also a problem with some algorithms not matching up to different data structures. Clearly it isn’t enough to just match the words we see in the coding problem to one of these ten heuristics. With that in mind, let’s create a clearer step-by-step guidance:
Is there a keyword in the problem statement? (e.g. “kth”, “shortest”, “all combinations”)What’s the core task?
Search? → Binary Search or BFS/DFS
Generate all? → Backtracking
Count or find optimal? → Dynamic Programming
Deal with intervals/greedy? → Sort + Greedy
What’s the input data structure?
Graph → DFS/BFS/Union-Find
Tree → DFS/BFS
Array → Prefix Sum, Two Pointers
Grid → DFS/BFS
Is performance a constraint?
Naive brute-force too slow? Think of caching (DP), pruning (Greedy), or smarter data structures.
Tips for Practicing the HeuristicsTag your own problems: When solving a Leetcode problem, tag it with the data structure or algorithm you used. That way you can come back to it later to help you solve future problems!
Redo problems with new tools: Solve the same problem with a different approach (e.g. if you had initially brute-forced but learned the correct algorithm later, come back to the problem with the new information).
Build a flashcard deck of Leetcode problems → algorithm/data structure mappings.
Practice by pattern: Instead of doing random Leetcode challenges, choose problems by type (e.g. “do 10 binary search problems in a row”). There are already some people who have done this, including the Blind 75 Question List and the website Neetcode!
A Specific Example
Problem: “Given a string s, return the length of the longest substring without repeating characters.”
Relevant Keywords: “longest”, “substring”, “without repeating”Heuristics Mapping:
Looking back up at our list, we see that the word “longest” matches to multiple heuristics. However, only one – Sliding Window – matches to “longest substring” specifically.
Solution:
Looking online, we see the Sliding Window algorithm is defined by expanding and contracting a window of characters using a HashSet or a HashMap. So to find our longest substring without repeating characters, we’ll use a HashMap on the characters in the current window and two pointers to expand/contract.
I’m hoping this article, combined with our algorithm explainer last week, will help in leading even the most bare-bones beginner to more victories in coding problem sets. You still need to put in the work of course – prepare to be confused by vaguely-worded or out of left field problems! – but the more work you put in, the easier things will become.
The post Using Heuristics to Dominate Coding Interviews appeared first on Jacob Robinson.


