Skip to main content

Introduction to Coding Patterns

In technical interviews, starting with a brute-force solution can help you understand the problem better before optimizing. However, to solve problems efficiently under time constraints, recognizing key characteristics is crucial. This is where coding patterns become essential.

Coding patterns are proven techniques that can be applied to common problem types, helping you improve performance and optimize your solution.

Below are some of the most commonly used coding patterns, along with when and why you should use them:

TechniqueWhatWhen to UseTime ComplexitySpace Complexity
Two-Pointer TechniqueUses two pointers to traverse data structuresWhen dealing with sorted arrays or finding pairsO(n)O(1)
Tortoise and HareFast and slow pointers to detect cyclesWhen working with linked lists or cycle detectionO(n)O(1)
Sliding WindowMaintains a window to optimize subarray sumsWhen finding contiguous subarrays or substringsO(n)O(1) or O(k)
Two-Pass TechniqueIterates through data structures twiceWhen needing to gather information in multiple passesO(n)O(1) or O(n)
Cyclic SortSorts elements by placing them in correct spotsWhen elements are in a range [1, n] and need sortingO(n)O(1)
Bit ManipulationUses bitwise operators to solve problemsWhen optimizing space and operations with bitsO(n)O(1)

Each pattern has a specific purpose, and knowing when to apply it can greatly improve problem-solving efficiency. In this module, we’ll explore each coding pattern in depth.

Have a pattern not present in this module that you want us to cover? Email us at [email protected] to let us know!