Coding Interview Patterns

Master Every Pattern

74 problems across 19 coding patterns. Learn the approach, understand the code, ace the interview.

Showing 74 problems

Two Pointers
easy

Pair Sum - Sorted

The problem asks to find two numbers in a sorted array that add up to a specific target value. It's a classic interview question testing your ability to leverage data ordering for efficient searching.

O(n)
O(1)
arraytwo-pointerssorted array
Solve
Two Pointers
medium

Triplet Sum

Given an array of integers, find all unique triplets that sum to a specified target value. The order of triplets and elements within triplets does not matter.

O(n^2)
O(n)
arraystwo pointerssorting
Solve
Two Pointers
easy

Is Palindrome Valid

This problem tests your ability to identify palindromes within strings, while also requiring you to handle and filter out irrelevant characters. It's a common interview question pattern that combines string manipulation with algorithmic thinking.

O(n)
O(1)
stringtwo-pointerspalindrome
Solve
Two Pointers
medium

Largest Container

Find the largest rectangular area possible within a histogram defined by an array of heights. This problem showcases your ability to optimize a solution from quadratic to linear time, a key skill for efficient algorithm design.

O(n)
O(n)
arraystackhistogram
Solve
Hash Maps and Sets
easy

Pair Sum - Unsorted

The Budget Buddies problem asks you to find two spending amounts from a list that, when combined, exactly match a given budget. This tests your ability to efficiently search for pairs within a data set, a common task in financial analysis and resource allocation.

O(n)
O(n)
arrayhash mapdictionary
Solve
Hash Maps and Sets
medium

Verify Sudoku Board

Validate a NumPlace puzzle board to ensure it follows the core rules: each row, column, and block contains unique digits. This tests your ability to use data structures for efficient constraint validation, a common pattern in problem-solving.

O(N^2)
O(N^2)
arrayhash-setvalidation
Solve
Hash Maps and Sets
medium

Zero Striping

The Matrix Infection problem requires modifying a matrix such that if any cell contains a specific value, its entire row and column are updated with that value. This tests understanding of in-place modifications and efficient matrix traversal.

O(m * n)
O(m + n)
matrixhash setsin-place
Solve
Linked Lists
easy

Linked List Reversal

Reversing a linked list is a classic interview question that tests your understanding of pointer manipulation and data structure fundamentals. Mastering this problem demonstrates your ability to think algorithmically and handle complex data structures efficiently.

O(n)
O(1)
linked-listreversalpointers
Solve
Linked Lists
medium

Remove the Kth Last Node From a Linked List

Given a singly linked list, remove the nth node from the end of the list. This problem tests your understanding of linked list manipulation and pointer techniques, crucial for many data structure and algorithm questions.

O(n)
O(1)
linked-listtwo-pointersdata-structures
Solve
Linked Lists
medium

Linked List Intersection

Determine if two singly linked lists intersect and return the intersecting node. This problem tests your understanding of linked list manipulation and efficient algorithm design.

O(m+n)
O(1)
linked-listtwo-pointersintersection
Solve
Linked Lists
hard

LRU Cache

Design a data structure that acts as a size-limited cache, efficiently storing and retrieving data while prioritizing the most recently accessed entries. This problem tests your understanding of data structures and algorithms, crucial for building performant and scalable systems.

O(1)
O(n)
cachelinked listhash map
Solve
Fast and Slow Pointers
easy

Linked List Loop

Detecting cycles in linked lists is a classic problem. It showcases your ability to work with pointer manipulation and algorithmic thinking under memory constraints.

O(n)
O(1)
linked-listcycle-detectionfloyd's-algorithm
Solve
Fast and Slow Pointers
easy

Linked List Midpoint

The problem is to efficiently find the middle node of a singly linked list. This is a classic interview question that tests your understanding of linked list manipulation and algorithm optimization.

O(n)
O(1)
linked-listfast-and-slow-pointerstwo-pointers
Solve
Fast and Slow Pointers
easy

Happy Number

The Digit Dance problem asks whether repeatedly summing the squares of a number's digits will eventually reach 1. This problem tests your ability to detect cycles and apply clever algorithmic thinking.

O(log n)
O(1)
number theorycycle detectionfast and slow pointers
Solve
Sliding Windows
medium

Substring Anagrams

Determine the number of substrings within a larger string that are anagrams of a given target string. This problem tests your ability to efficiently compare strings and identify patterns, crucial for tasks like data analysis and text processing.

O(m + n)
O(1)
sliding windowstringanagram
Solve
Sliding Windows
medium

Longest Substring With Unique Characters

Find the longest substring within a given string that contains no repeating characters. This tests your ability to efficiently manage string data and optimize for performance using sliding window techniques.

O(n)
O(min(m, n))
stringsliding windowsubstring
Solve
Sliding Windows
medium

Longest Uniform Substring After Replacements

Find the longest consecutive segment of a string that can be made up of the same character by changing at most 'k' characters. This tests your ability to use sliding windows and optimize for substring problems.

O(n)
O(1)
stringsliding windowsubstring
Solve
Binary Search
easy

Find the Insertion Index

Given a sorted array of integers, find the correct index to insert a target value so that the array remains sorted. This problem tests your ability to apply efficient search algorithms in common scenarios.

O(log n)
O(1)
arraybinary searchsorted array
Solve
Binary Search
medium

First and Last Occurrences of a Number

Find the first and last position of a given target value within a sorted array. This problem tests your ability to efficiently search sorted data, a core skill for any software engineer.

O(log n)
O(1)
arraybinary searchsorted array
Solve
Binary Search
medium

Cutting Wood

This problem involves optimizing resource allocation, a common theme in software engineering. It tests your ability to apply binary search in a non-traditional way to find the optimal solution within a defined range.

O(n log m)
O(1)
binary searchoptimizationresource allocation
Solve
Binary Search
medium

Find the Target in a Rotated Sorted Array

The Rotated Array Search problem challenges you to find a target value in a sorted array that has been rotated. Mastering this problem demonstrates your ability to adapt binary search to non-standard scenarios, a key skill for efficient algorithm design.

O(log n)
O(1)
arraybinary searchdivide and conquer
Solve
Stacks
easy

Valid Parenthesis Expression

Determine if a string containing different types of brackets is properly nested. This tests your understanding of data structures and algorithm design, specifically stacks, which are fundamental in many software engineering contexts.

O(n)
O(n)
stackdata structuresstring
Solve
Stacks
medium

Next Largest Number to the Right

Find, for each element in an array, the closest element to its right that is strictly greater. This problem tests understanding of data structures for efficient lookups and monotonic stacks.

O(n)
O(n)
stackarraymonotonic stack
Solve
Stacks
medium

Evaluate Expression

This problem challenges you to parse and evaluate a mathematical expression string, handling parentheses, addition, and subtraction. It's a good test of your stack data structure skills and ability to manage operator precedence.

O(n)
O(n)
stackstringmath
Solve
Heaps
medium

K Most Frequent Strings

Identify the N most frequent items in a collection, then return them in descending order of frequency. This is a common pattern for data analysis, search algorithms, and recommendation systems.

O(n log k)
O(n)
heaphash mapfrequency
Solve
Heaps
medium

Combine Sorted Linked Lists

Merge k sorted streams into a single sorted stream. This tests your ability to efficiently manage multiple sorted data sources and is a good indicator of your heap data structure skills.

O(n*log(k))
O(k)
heappriority queuemerging
Solve
Heaps
hard

Median of an Integer Stream

Design a data structure to efficiently calculate the running median of a continuously growing stream of numbers. This tests knowledge of heaps and data structure design under dynamic constraints, a common interview theme.

O(log n)
O(n)
heapdata structuremedian
Solve
Intervals
medium

Merge Overlapping Intervals

The 'Meeting Room Merger' problem asks you to consolidate overlapping time intervals, which is a common scenario in scheduling applications. Mastering this problem demonstrates your ability to work with interval data and optimize algorithms for efficiency, crucial for software engineering roles.

O(n log n)
O(n)
arrayintervalssorting
Solve
Intervals
medium

Identify All Interval Overlaps

Given two lists of time intervals, find all the overlapping time periods between them. This problem tests your ability to work with interval data structures and algorithmic thinking.

O(m+n)
O(k)
arrayintervalstwo-pointers
Solve
Intervals
medium

Largest Overlap of Intervals

The problem asks you to find the maximum number of overlapping time intervals within a given set. This tests your ability to efficiently manage and analyze intervals, a common pattern in scheduling and resource allocation problems.

O(n log n)
O(n)
intervalssortingsweeping line
Solve
Prefix Sums
easy

Sum Between Range

The Problematic Playlist problem asks you to find how many song combinations in a playlist have a total length equal to a target duration. This is a common interview problem that tests your understanding of prefix sums and efficient counting techniques.

O(n)
O(n)
arrayprefix sumhash map
Solve
Prefix Sums
medium

K-Sum Subarrays

The Target Sum Segments problem asks you to find how many continuous segments of an array add up to a specific target value. Mastering this problem demonstrates your ability to efficiently use prefix sums and hash maps, crucial skills for many coding interview questions.

O(n)
O(n)
arrayprefix sumhash map
Solve
Prefix Sums
medium

Product Array Without Current Element

The 'Modified Product Array' problem challenges you to create a new array where each element is the product of all other numbers in the input array, excluding the number at that index. This tests your ability to manipulate arrays efficiently and think creatively about prefix and suffix products.

O(n)
O(1)
arrayprefix-sumproduct
Solve
Trees
easy

Invert Binary Tree

The problem asks you to mirror a binary tree, effectively swapping the left and right children of each node. This tests your understanding of tree traversals and recursion, crucial for many tree-based algorithms.

O(n)
O(h)
treerecursionbinary tree
Solve
Trees
easy

Balanced Binary Tree Validation

Determine if a binary tree is considered 'well-proportioned' - meaning that for every node, the heights of its left and right subtrees are within a certain tolerance. This is a common tree problem that tests your understanding of recursion and tree traversals.

O(n)
O(n)
treerecursiondepth-first search
Solve
Trees
medium

Rightmost Nodes of a Binary Tree

Finding the visible nodes from the left in a binary tree is a classic tree traversal problem. It tests your ability to navigate tree structures and apply level-order traversal, a crucial skill for many tree-related interview questions.

O(n)
O(w)
treebinary treelevel-order traversal
Solve
Trees
medium

Widest Binary Tree Level

Determine the maximum width of any level in a binary tree. This tests your understanding of tree traversals and space optimization, crucial for efficient data structure manipulation.

O(n)
O(n)
treebreadth-first searchlevel-order traversal
Solve
Trees
medium

Binary Search Tree Validation

Determine if a given binary tree adheres to the rules of a Binary Search Tree (BST). This problem tests your understanding of tree traversals and BST properties, critical for many tree-based algorithms.

O(n)
O(n)
treebinary-treebinary-search-tree
Solve
Trees
medium

Lowest Common Ancestor

Finding the Lowest Common Ancestor in a binary tree is a classic problem that tests your understanding of tree traversals and recursion. It's important because it demonstrates your ability to efficiently navigate complex data structures.

O(n)
O(n)
treebinary treerecursion
Solve
Trees
medium

Build a Binary Tree From Preorder and Inorder Traversals

Reconstruct a binary tree given its preorder and inorder traversals. This tests understanding of tree traversals and recursive algorithms, crucial for many data structure-related interview questions.

O(n)
O(n)
treerecursionbinary-tree
Solve
Trees
hard

Maximum Sum of a Continuous Path in a Binary Tree

Find the maximum sum of any path in a binary tree. This problem tests your understanding of tree traversals and dynamic programming.

O(n)
O(h)
treerecursiondynamic programming
Solve
Tries
medium

Design a Trie

Implement a trie (prefix tree) data structure to efficiently store and retrieve words. This problem tests your understanding of tree-based structures and their application to string manipulation, a common theme in coding interviews.

O(m)
O(n*m)
trieprefix treestring
Solve
Tries
medium

Insert and Search Words with Wildcards

Design a data structure that efficiently stores words and supports searching for words with wildcard characters. This problem tests your understanding of tries and recursive searching, crucial for applications like autocomplete and spellcheck.

O(M * 26^W)
O(N*M)
trierecursionstring
Solve
Tries
hard

Find All Words on a Board

Given a grid of letters and a list of words, identify all words from the list that can be constructed by traversing adjacent cells in the grid. This problem tests your ability to combine graph traversal with efficient data structures for searching.

O(m * n * 4^L)
O(N)
triedepth-first searchbacktracking
Solve
Graphs
medium

Graph Deep Copy

The problem is to create a completely independent copy of a graph given a starting node. This tests your understanding of graph traversal and deep copying techniques, crucial for many real-world applications like data structure manipulation and algorithm design.

O(n + e)
O(n)
graphdfsrecursion
Solve
Graphs
medium

Count Islands

Given a grid representing a map with land and water, the task is to determine the number of distinct landmasses. This problem tests graph traversal skills, a fundamental concept in algorithm design and software engineering.

O(m*n)
O(m*n)
graphdfsmatrix
Solve
Graphs
medium

Matrix Infection

Determine the minimum time for a digital virus to spread across a network represented as a grid. This problem tests graph traversal and optimization skills crucial for real-world network analysis.

O(m*n)
O(m*n)
graphbfsmatrix
Solve
Graphs
medium

Bipartite Graph Validation

Determine if a given network of friends can be divided into two groups where no friends in the same group are directly connected. This problem tests graph traversal and coloring skills, crucial for understanding network structures and resource allocation.

O(n + m)
O(n)
graphbipartitegraph-coloring
Solve
Graphs
hard

Longest Increasing Path

Find the longest path in a matrix where each step increases in value. This tests graph traversal and optimization skills, crucial for system design and algorithm efficiency.

O(m*n)
O(m*n)
graphdfsmemoization
Solve
Graphs
hard

Shortest Transformation Sequence

Given a starting word, an ending word, and a dictionary of valid words, find the minimum number of transformations to change the starting word into the ending word by changing one letter at a time, where each intermediate word must exist in the dictionary.

O(N * L^2)
O(N)
GraphsBreadth-First SearchString Manipulation
Solve
Graphs
medium

Merging Communities

The 'Network Connectivity' problem focuses on modeling a social network where individuals can form connections, merging their friend groups. This tests your ability to efficiently track group memberships and sizes after a series of connection events, a common pattern in graph algorithms and data structure optimization.

O(m * α(n))
O(n)
graphdisjoint set unionunion-find
Solve
Backtracking
medium

Find All Permutations

Generate all possible orderings of elements in a list. This problem tests your ability to think recursively and use backtracking effectively, common skills in algorithm design.

O(n! * n)
O(n)
recursionbacktrackingpermutation
Solve
Backtracking
medium

Find All Subsets

Given a set of ingredients, find all possible combinations of ingredients that can be made. Each combination must be unique.

O(n * 2^n)
O(n)
backtrackingcombinationsrecursion
Solve
Backtracking
hard

N Queens

The Knight's Harmony problem challenges you to place a given number of knights on a chessboard such that no two knights threaten each other. This tests your ability to use backtracking to explore solution spaces efficiently, a crucial skill for solving combinatorial problems.

O(n!)
O(n)
backtrackingrecursioncombinatorial
Solve
Dynamic Programming
easy

Climbing Stairs

This problem explores dynamic programming through counting possible paths. Mastering this pattern is crucial for demonstrating problem-solving and optimization skills in coding interviews.

O(n)
O(n)
dynamic programmingrecursionoptimization
Solve
Dynamic Programming
medium

Minimum Coin Combination

This problem focuses on finding the minimum number of stamps needed to reach a specific postage value, a classic dynamic programming challenge. Mastering this demonstrates understanding of optimization techniques and problem decomposition, crucial skills for software engineers.

O(target * n)
O(target)
dynamic-programmingoptimizationminimum
Solve
Dynamic Programming
medium

Matrix Pathways

The 'Concert Lineup' problem asks you to find the maximum enjoyment a concert promoter can get from a lineup of bands, with the constraint that no two bands of the same genre can play consecutively. It tests dynamic programming skills and optimization.

O(n)
O(1)
dynamic programmingoptimizationarray
Solve
Dynamic Programming
medium

Neighborhood Burglary

Given a row of houses, each with a certain amount of valuables, determine the maximum amount you can collect without hitting two adjacent houses. This tests dynamic programming skills and optimization techniques.

O(n)
O(n)
dynamic programmingarrayoptimization
Solve
Dynamic Programming
medium

Longest Common Subsequence

Find the longest sequence of ingredients that appear in the same order in two different recipes. This problem tests dynamic programming skills and the ability to optimize for overlapping subproblems.

O(m*n)
O(m*n)
dynamic programmingstringsequence
Solve
Dynamic Programming
medium

Longest Palindrome in a String

Given a string, find the longest substring that is a palindrome. This problem tests your ability to recognize patterns and efficiently search for optimal solutions within strings, a common task in text processing and bioinformatics.

O(n^2)
O(n^2)
stringdynamic programmingpalindrome
Solve
Dynamic Programming
medium

Maximum Subarray Sum

The Maximum Profit Slice problem asks you to find the contiguous subarray within a given array of stock prices that yields the highest profit. This problem is a classic example of dynamic programming and tests your ability to optimize for time complexity.

O(n)
O(1)
arraydynamic programmingmaximum subarray
Solve
Dynamic Programming
medium

0/1 Knapsack

A museum curator needs to select artifacts to display, maximizing their cultural value while staying within the exhibit's weight limit. This is a variation of the classic knapsack problem.

O(n*capacity)
O(n*capacity)
dynamic programmingknapsack problemoptimization
Solve
Greedy
medium

Jump to the End

Determine if you can reach the last index of an array given jump lengths at each position. This tests dynamic programming and greedy approaches common in technical interviews.

O(n)
O(1)
arraygreedydynamic programming
Solve
Greedy
medium

Gas Stations

Imagine a circular race track where cars need fuel to complete laps. This problem asks you to find a starting point on the track that allows a car with a limited fuel capacity to complete the entire circuit without running out of fuel, testing your ability to apply greedy algorithms.

O(n)
O(1)
arraygreedyoptimization
Solve
Greedy
hard

Candies

The 'Treat Distribution' problem focuses on fairly distributing treats to children based on their performance ratings, ensuring those with higher ratings receive more treats than their neighbors. This problem tests your ability to think greedily and optimize resource allocation based on local information, a common scenario in real-world scheduling and optimization tasks.

O(n)
O(n)
arraygreedyoptimization
Solve
Sort and Search
medium

Sort Linked List

Sort a singly linked list in ascending order. This tests understanding of linked list manipulation and efficient sorting algorithms applicable to non-array data structures.

O(n log n)
O(log n)
linked listsortingmerge sort
Solve
Sort and Search
medium

Sort Array

Rearrange the elements of an array of integers such that all even numbers come before all odd numbers. This problem tests understanding of array manipulation and sorting concepts.

O(n)
O(1)
arraytwo-pointersin-place
Solve
Sort and Search
medium

Kth Largest Integer

Finding the Xth largest element in a dataset is a common task with various applications. This problem assesses your ability to efficiently identify specific elements within unsorted data, testing your knowledge of sorting and searching algorithms.

O(n log X)
O(X)
arrayheapsorting
Solve
Bit Manipulation
easy

Hamming Weights of Integers

Calculate the 'digital root' of each number from 0 to n, where the digital root is the result of repeatedly summing the digits of a number until a single-digit number is reached. This problem tests bit manipulation and dynamic programming skills, crucial for optimizing performance in numerical computations.

O(n)
O(n)
dynamic programmingmathematicsbit manipulation
Solve
Bit Manipulation
easy

Lonely Integer

Given an array of integers where each number appears a certain number of times except for one unique number, find the number that appears an odd number of times. You must do this efficiently.

O(n)
O(1)
bit manipulationXORarray
Solve
Bit Manipulation
easy

Swap Odd and Even Bits

This problem focuses on bit manipulation, a crucial skill for optimizing performance and understanding low-level system architecture. The task is to rearrange the bits of an integer by interleaving two new numbers created by its even and odd bits.

O(1)
O(1)
bit-manipulationbitmaskingoptimization
Solve
Math and Geometry
medium

Spiral Traversal

Traverse a 2D matrix in a clockwise, layer-by-layer fashion, returning the elements in the order visited. This problem tests your ability to work with multi-dimensional arrays and think geometrically about data structures.

O(m*n)
O(1)
matrixspiral traversalarray
Solve
Math and Geometry
medium

Reverse 32-Bit Integer

Given a 32-bit signed integer, cyclically right-shift its digits by a specified amount. If the resulting integer falls outside the 32-bit signed integer range, return 0.

O(log n)
O(1)
MathBit ManipulationInteger Manipulation
Solve
Math and Geometry
hard

Maximum Collinear Points

Given a set of coordinates on a 2D plane, find the largest number of points that lie on the same straight line. This tests your ability to apply geometric principles and optimize for precision issues in calculations.

O(n^2)
O(n)
geometryhashingmath
Solve