Fundamentals 19 min read

14 Essential Coding Interview Patterns Every Developer Should Master

This article outlines fourteen common algorithmic patterns—such as sliding window, two pointers, fast‑slow pointers, interval merging, cyclic sort, in‑place list reversal, BFS/DFS on trees, two‑heap, subsets, modified binary search, top‑K, K‑way merge, and topological sort—explaining when to use each, how they work, and example problems to help developers prepare efficiently for technical interviews.

Liangxu Linux
Liangxu Linux
Liangxu Linux
14 Essential Coding Interview Patterns Every Developer Should Master

1. Sliding Window

The sliding window pattern processes a contiguous sub‑range (window) of an array, string, or linked list. The window can be fixed‑size or variable‑size and moves forward one element at a time, expanding or shrinking based on problem constraints. It is ideal when the task asks for the longest/shortest sub‑array or substring that satisfies a condition.

Identify: input is a linear structure (array, string, list) and the problem requires a longest/shortest contiguous segment.

Example problems:

Maximum sum of a subarray of size K (easy)

Longest substring with K distinct characters (medium)

Find all anagrams of a pattern in a string (hard)

2. Two Pointers (Iterators)

Two pointers start at opposite ends (or at a fixed offset) of a sorted array or linked list and move toward each other until a condition is met. This reduces a naïve O(n²) scan to O(n) time and uses O(1) extra space.

Identify: the input is sorted and you need to locate pairs, triples, or compare elements.

Example problems:

Square each element of a sorted array (easy)

3‑sum problem (medium)

Compare strings with backspace characters (medium)

3. Fast and Slow Pointers (Hare & Tortoise)

Two pointers traverse the same structure at different speeds (typically 1× and 2×). The fast pointer eventually meets the slow pointer in a cycle, enabling detection of loops, finding the middle of a list, or checking palindromes.

Identify: the problem involves a cyclic structure or requires the middle element of a list/array.

Example problems:

Detect a cycle in a linked list (easy)

Check if a linked list is a palindrome (medium)

Find a cycle in a circular array (hard)

4. Interval Merging

When intervals overlap, merge them into a set of disjoint intervals. The algorithm sorts intervals by start time and iteratively combines overlapping ones.

Identify: the statement mentions “overlapping intervals” or asks for a list of non‑overlapping intervals.

Example problems:

Interval intersection (medium)

Maximum CPU load (hard)

5. Cyclic Sort

For arrays containing numbers in the range 1..n (or a known range), place each element at its correct index by swapping. This runs in O(n) time and O(1) space.

Identify: the array consists of values within a known contiguous range and the task asks for missing, duplicate, or minimum values.

Example problems:

Find the missing number (easy)

Find the smallest missing positive integer (medium)

6. In‑Place List Reversal

Reverse a linked list (or a sub‑list) by re‑linking nodes using only existing pointers. Iterate through the list, redirect each node’s next to the previous node, and advance.

Identify: the problem requires reversing a list without extra memory.

Example problems:

Reverse a sub‑list (medium)

Reverse every K nodes (medium)

7. Tree Breadth‑First Search (BFS)

BFS traverses a tree level by level using a queue. Enqueue the root, then repeatedly dequeue a node, process it, and enqueue its children.

Identify: the task asks for level‑order processing of a tree.

Example problems:

Binary tree level order traversal (easy)

Zigzag (spiral) traversal (medium)

8. Tree Depth‑First Search (DFS)

DFS explores a tree using recursion or an explicit stack. It can be performed in pre‑order, in‑order, or post‑order depending on when the node is visited relative to its children.

Identify: the problem requires a specific ordering of node visits or searching deeper nodes first.

Example problems:

Path sum (medium)

All root‑to‑leaf paths that sum to a target (medium)

9. Two Heaps

Maintain a max‑heap for the lower half of a data stream and a min‑heap for the upper half. The median is either the top of one heap or the average of both tops, allowing O(log n) insertion and O(1) median retrieval.

Identify: the problem asks for the median, or the smallest/largest element of a dynamic set.

Example problem:

Find the median of a number stream (medium)

10. Subsets (Power Set)

Generate all subsets of a set by iteratively adding each element to existing subsets (BFS style). Starting from [[]], for each element create new subsets that include it.

Identify: the task involves enumerating all combinations or permutations of a collection.

Example problems:

Subsets with duplicate elements (easy)

String permutations with case changes (medium)

11. Modified Binary Search

Standard binary search on a sorted array, list, or matrix with careful midpoint calculation to avoid overflow: mid = left + (right‑left)/2. Adjust left or right based on comparison, repeat until left > right.

Identify: the input is sorted and the problem asks for a target value.

Example problems:

Order‑agnostic binary search (easy)

Search in an infinite sorted array (medium)

12. Top K Elements

Use a heap of size K to keep the smallest (min‑heap) or largest (max‑heap) K elements while scanning the data set. When a new element outranks the heap’s root, replace the root and heapify.

Identify: the problem asks for the first K numbers, the most frequent K, or similar.

Example problems:

First K numbers (easy)

Most frequent K numbers (medium)

13. K‑Way Merge

Merge K sorted arrays by inserting the first element of each array into a min‑heap. Repeatedly extract the smallest element, append it to the result, and push the next element from the same array. Continue until the heap is empty.

Identify: the input consists of multiple sorted lists that need to be merged into a single sorted sequence.

Example problems:

Merge K sorted lists (medium)

Find K pairs with the largest sums (hard)

14. Topological Sort

Topological sorting orders vertices of a directed acyclic graph (DAG). Compute in‑degree for each vertex, enqueue all vertices with in‑degree 0, then repeatedly dequeue a vertex, add it to the ordering, and decrement the in‑degree of its neighbors, enqueuing any that become 0.

Identify: the problem involves ordering tasks with dependencies, or finding a linear order of elements where some must precede others.

Example problems:

Task scheduling (medium)

Find the minimum height of a tree (medium)

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Software Engineeringproblem solvingcoding interviewalgorithm patterns
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.