Coding Interview Problems

In-depth walkthroughs of coding interview problems from top tech companies

← Back to Blog
81 problems
Problem Diff.
Valid parentheses
Given a string of brackets, write a method that checks if every opening bracket has a matching closing bracket in the correct order. A classic stack-based interview problem.
50
Subset summation
Given a list of positive integers and a target, determine if any subset sums to the target. A classic recursion and backtracking interview problem.
65
Recursive String Permutations
Given an input string of unique characters, return all permutations of the string using recursion and backtracking. A classic recursion problem testing divide-and-conquer thinking and the insert-at-every-position technique.
70
Find the lowest common ancestor of two binary tree nodes
Given the root of a binary tree and two target node values, find and return the data of the lowest common ancestor. A classic binary tree recursion problem frequently asked at Facebook, Amazon, and Microsoft.
67
Find duplicate numbers
Given an integer array, write a method that identifies and returns all numbers that appear more than once as a sorted array containing each duplicate value exactly once.
14
Happy numbers
Write a method that checks if a given positive integer is a happy number by repeatedly summing the squares of its digits until it reaches 1 or enters a cycle. A classic set-based cycle detection interview problem.
45
Find the diameter of a binary tree
Given the root of a binary tree, find its diameter - the number of nodes on the longest path between any two nodes. A classic tree recursion problem testing depth-first search and height computation.
58
Convert a binary tree to its mirror image
Learn how to convert a binary tree to its mirror image by recursively swapping left and right children at every node. Walkthrough with visualizations and solutions in 14 languages.
55
Identical binary trees
Given the roots of two binary trees, determine if they are identical. Two trees are identical when they share the same shape and the same data at every node. A classic recursion problem testing base cases and tree traversal.
52
Remove duplicate nodes from a linked list
Given the head of a singly linked list, remove all duplicate nodes while keeping the first occurrence of each value. A classic linked list problem testing set-based traversal and pointer manipulation.
29
Array rotation
Rotate an array to the left by k positions in-place using the three-reversal trick. A classic array manipulation interview problem with O(n) time and O(1) space.
52
Palindrome linked list
Check whether a singly linked list is a palindrome using constant space. Learn the slow/fast pointer technique to find the middle, reverse the second half in-place, and compare both halves node by node.
50
Minimum depth of a binary tree
Given the root of a binary tree, find the minimum depth - the smallest number of node traversals from root to the nearest leaf. A classic BFS / level order traversal problem using a two-queue approach.
45
Nth Fibonacci number with recursion
Write a recursive method to return the nth Fibonacci number. A classic recursion problem testing base cases and the Fibonacci recurrence relation.
8
Is power of two?
Write a method to test whether a given positive integer is a power of 2 using bitwise operations, with constant time and space complexity.
38
Binary search
Search a sorted array for a target value using the binary search algorithm. O(log n) time, O(1) space. A foundational divide-and-conquer interview problem.
9
Reverse an integer
Write a method that reverses an input integer using only constant space. A fundamental number manipulation problem that tests your understanding of modular arithmetic and integer division.
24
Merge sorted arrays
Write a method that merges two sorted arrays into a single globally sorted array using a two-pointer approach. A fundamental sorting and array interview problem.
27
Palindrome tester
Write a method that checks if a string reads the same forward and backward, ignoring case. A classic string manipulation interview problem.
4
Max Profit
Given an array of stock prices over n days, find the maximum profit from a single buy-sell transaction. A classic array problem that tests greedy algorithm thinking.
25
Find the middle of a linked list in one pass
Write a method to find and return the data of the middle node of a singly linked list in one pass using the slow and fast pointer technique.
18
Find the unique number in linear time
Given an array that contains duplicates and exactly one unique number, write a method that finds and returns the unique number using a HashMap for O(n) time complexity.
11
Find the average of contiguous sub-arrays of size k
Given an array of integers and an integer k, find the average of all contiguous subarrays of size k using the sliding window technique in O(n) time.
20
Minimum sum path in a triangle
Given an integer triangle as a nested list, find the minimum sum path from the top to the bottom. A classic dynamic programming interview problem solved with bottom-up DP in O(n) space.
70
Reverse a linked list in pairs
Given a singly-linked list, reverse the list in pairs by swapping adjacent nodes two at a time and return the head of the modified list.
58
Insert interval
Given a list of sorted, non-overlapping intervals and a new interval, write a method to insert it into the list such that there is no overlap between them (merge as needed) and the sorted order is maintained.
63
Generate combinations of parentheses
Given n pairs of parentheses, generate all valid combinations using recursive backtracking. A classic recursion problem frequently asked at Amazon, Google, and Facebook.
54
Maximal Ascending Sequence Length
Find the length of the longest strictly increasing subsequence using binary search and patience sorting. O(n log n) time, O(n) space, with step-by-step walkthrough.
49
Two Sum
Given an array of integers and a target, find indices of two numbers that add up to the target using a hash map in O(n) time.
31
Anagram Checker: verify two strings share the same letters
Learn to check if two strings are anagrams using character frequency counting. Walkthrough with visualizations and solutions in 14 languages.
23
Array Multiplicative Exclusion
Build an output array where each element is the product of every other element, without using division. O(n) time using prefix and suffix products.
45
Binary Search Tree Lowest Common Ancestor Finder
Find the lowest common ancestor of two nodes in a BST using its ordering property. O(h) time, O(1) space.
66
Kth Minimal Node in a Binary Search Tree
Given the root of a binary search tree and an integer k, find and return the kth smallest value among all the node values in the tree. The indexing is 1-based.
45
Frequent Element Finder
Given an array of integers with length n, identify all elements that occur more than n/3 times. Solve it in O(n) time and O(1) space using the extended Boyer-Moore Voting Algorithm.
49
Course Completion Feasibility: detect cycles in prerequisite graphs
Determine if all courses can be completed given prerequisites. DFS cycle detection in O(V+E) time using three-state coloring.
53
Street Safe Heist Strategy: maximize loot without robbing neighbors
Maximize the sum of non-adjacent elements using space-optimized DP. O(n) time, O(1) space walkthrough of the House Robber problem.
47
Counting Set Bits in Binary Representation
Given a positive integer n, create a function to determine how many set bits (1s) are present in its binary form, also referred to as the Hamming weight.
23
Find Pair Indices in Sorted Array
Find two numbers in a sorted array that sum to a target using the two-pointer technique. O(n) time, O(1) space.
32
Locate Lowest Element in a Rotated Array
Find the minimum element in a rotated sorted array using binary search. O(log n) time, O(1) space, with step-by-step walkthrough and solutions in 14 languages.
48
Highest Product of Subarray
Find the contiguous subarray with the highest product using a max/min tracking algorithm. O(n) time, O(1) space, with step-by-step walkthrough and negative number handling.
48
Sentence Formation with Word Dictionary
Find all possible ways to segment a string into valid dictionary words using backtracking with memoization.
83
Unique Lone Integer: find the number that appears only once
Learn to find the single number in an array where every other element appears twice, using XOR bit manipulation. Walkthrough with visualizations and solutions in 14 languages.
18
ASCII Palindrome Checker
Determine if a string is a palindrome after removing non-alphanumeric characters and ignoring case. Two-pointer O(n) time, O(1) space solution.
23
Optimal Path Sum in Binary Trees
Given the root of a binary tree, determine and return the highest possible path sum for any path that contains at least one node. A path consists of a sequence of nodes where each pair of consecutive nodes is connected by an edge, and no node appears more than once.
82
Root-to-Leaf Target Path Sum: find if any path sums to a target
Given a binary tree and a target sum, determine if any root-to-leaf path sums to the target. Recursive DFS solution in O(n) time and O(h) space.
40
Max Depth of Binary Tree
Given the root of a binary tree, determine its maximum depth. The maximum depth of a binary tree is defined as the number of nodes along the longest path from the root node to the most distant leaf node.
23
Tree Structure Equality Check - How to Compare Two Binary Trees
Determine if two binary trees are identical by comparing structure and node values using recursive DFS. O(n) time, O(n) space.
35
Grid Word Hunt
Given an m x n character grid and a target word, determine if the word exists by connecting adjacent cells using DFS with backtracking in O(m * n * 4^k) time.
49
Power Set Exploration
Generate all possible subsets (the power set) of a unique integer array using backtracking and bit manipulation. O(2^n) time and space, with full recursion tree walkthrough and solutions in 14 languages.
45
Color Segregation Challenge
Sort an array of 0s, 1s, and 2s in-place using the Dutch National Flag algorithm. O(n) time, O(1) space. Full walkthrough with solutions in 14 languages.
47
Zero Out Rows and Columns
Given an m x n matrix, if any element is 0, set its entire row and column to 0 in place. Solve it in O(1) space by using the first row and column as markers.
47
String Transformation Cost - Minimum Edit Distance with Dynamic Programming
Master the edit distance problem using 2D dynamic programming. Learn to compute the minimum insert, delete, and replace operations to transform one string into another.
82
Step combinations to reach the summit
Find the number of distinct ways to climb n stairs taking 1 or 2 steps at a time using Fibonacci-like dynamic programming.
22
Combining Overlapping Ranges: merge intervals with sorting
Learn to merge overlapping intervals by sorting and scanning. Step-by-step walkthrough with visualizations and solutions in 14 languages.
47
Leap to the Finish Line: can you reach the last index?
Determine if you can reach the last array index using a greedy maxReach approach. O(n) time, O(1) space walkthrough.
48
Matrix Spiral Traversal
Given an m x n matrix, return all elements in spiral order using four boundary pointers. O(m*n) time, O(m*n) space for the output.
48
Subarray with Maximum Total: Kadane's algorithm explained
Find the contiguous subarray with the largest sum using Kadane's algorithm. O(n) time, O(1) space, with step-by-step walkthrough.
49
Anagram clustering challenge
Group an array of strings so that anagrams appear together using sorted keys and a hash map in O(n * k log k) time.
45
Image Matrix 90-Degree Twist
Rotate an n x n matrix 90 degrees clockwise in-place using the transpose-then-reverse technique. O(n^2) time, O(1) space.
45
Sum Combinations with Repeats
Given an array of unique integers and a target, find all unique combinations that sum to the target. Each number can be reused unlimited times. Solved with backtracking in O(2^target) time.
47
Insertion Index Finder
Given a sorted array of unique integers and a target value, find the index of the target or the position where it should be inserted. O(log n) binary search solution with step-by-step walkthrough and solutions in 14 languages.
33
Pivoted Array Target Search: Binary search in a rotated sorted array
Search for a target in a rotated sorted array using modified binary search. O(log n) time, O(1) space, with step-by-step walkthrough.
58
Combine Multiple Sorted Linked Lists Using a Min-Heap
Learn two approaches to merge k sorted linked lists: min-heap for O(N log k) efficiency and divide-and-conquer for elegant recursion. Complete solutions in 14 languages.
82
Bracket Harmony Check: validate nested brackets with a stack
Learn to validate matching brackets using a stack. Step-by-step walkthrough of the Valid Parentheses problem with visualizations and solutions in 14 languages.
22
Eliminate Node from Tail of Linked List
Given the head of a linked list, remove the nth node from the end and return its head. Solved with the two-pointer technique in O(n) time and O(1) space.
48
Phone Keypad Letter Combinations
Given a string of digits from 2-9, generate all possible letter combinations using backtracking. A classic interview problem at Google, Amazon, Apple, and Meta.
45
Zero-Sum Triplet Finder
Find all unique triplets in an array that sum to zero using sort and two pointers in O(n^2) time.
47
Shared Prefix Finder
Find the longest common prefix shared by an array of strings using the horizontal scanning approach. O(n * m) time, O(1) space.
23
Roman Numeral Conversion to Decimal
Convert a Roman numeral string to its integer value using a hash map and right-to-left traversal with subtraction rule detection. O(n) time, O(1) space.
22
Maximize Water Storage Between Lines
Find two lines that hold the most water using the two-pointer technique in O(n) time and O(1) space.
48
Digit Reversal with Overflow Guard
Reverse the digits of a signed 32-bit integer, returning 0 if the result overflows. O(log n) time, O(1) space.
48
Maximal Symmetric Substring
Given a string s, find and return the longest palindromic substring within s using the expand-around-center technique in O(n^2) time and O(1) space.
48
Central Median from Dual Sorted Lists
Find the median of two sorted arrays in O(log(min(m,n))) time using binary search partitioning. A classic hard problem tested at Google, Amazon, and Uber.
83
Number of islands
Given an m x n grid of 1s (land) and 0s (water), count the number of islands using DFS flood fill in O(m*n) time and space.
61
Longest substring without repeated characters
Given a string, find the length of the longest substring without repeated characters using the sliding window technique. O(n) time, O(n) space.
67
Bottom-up level order binary tree traversal
Traverse a binary tree bottom-up, level by level, returning node values from the deepest level to the root. Uses BFS with a queue and a final reversal for O(n) time and O(n) space.
59
Count paths on a board
Calculate the number of unique paths from top-left to bottom-right in an m×n grid, moving only right or down.
45
Wildcard Parenthesis Validator
Master the art of validating parentheses with wildcards - a favorite interview problem at Amazon, Microsoft, and Apple that tests your greedy algorithm skills.
60
Post-Bigram Word Finder
Find all words that appear immediately after a specific two-word sequence in a string - a classic string parsing challenge.
18
Reverse a linked list
Given the head of a singly linked list, write a method that reverses the list and returns its new head.
35
Height of a binary tree
Given the root TreeNode of a binary tree, write a method return its height. An empty tree has a height of 0 while a single node tree has a height of 1.
32