# Coding Interview Questions

> A growing library of the most common coding interview questions, each with a plain-English problem restatement, the core idea behind the solution, and complexity analysis.

## Arrays & Hashing

- [Two Sum](https://www.marauderbot.com/questions/two-sum.md): Easy. How to solve Two Sum: find the two array entries that add up to a target in a single O(n) pass using a hash map of complements.
- [Valid Sudoku](https://www.marauderbot.com/questions/valid-sudoku.md): Medium. How to solve Valid Sudoku: check a 9x9 board for rule violations by tracking seen digits per row, column, and 3x3 box.
- [Group Anagrams](https://www.marauderbot.com/questions/group-anagrams.md): Medium. How to solve Group Anagrams: bucket words that are anagrams of each other using a canonical key derived from their letters.
- [Longest Consecutive Sequence](https://www.marauderbot.com/questions/longest-consecutive-sequence.md): Medium. How to solve Longest Consecutive Sequence: find the longest run of consecutive integers in an unsorted array in linear time with a hash set.
- [Contains Duplicate](https://www.marauderbot.com/questions/contains-duplicate.md): Easy. How to solve Contains Duplicate: detect a repeated array value in one linear pass using a hash set instead of comparing every pair.
- [Product of Array Except Self](https://www.marauderbot.com/questions/product-of-array-except-self.md): Medium. How to solve Product of Array Except Self: compute every index product without division using running prefix and suffix products.
- [Valid Anagram](https://www.marauderbot.com/questions/valid-anagram.md): Easy. How to solve Valid Anagram: confirm two strings use the same letters the same number of times by comparing character counts.
- [Encode and Decode Strings](https://www.marauderbot.com/questions/encode-and-decode-strings.md): Medium. How to solve Encode and Decode Strings: pack a list of strings into one string and recover it exactly using length-prefixed chunks.
- [Top K Frequent Elements](https://www.marauderbot.com/questions/top-k-frequent-elements.md): Medium. How to solve Top K Frequent Elements: find the k most common values by combining a frequency count with a heap or bucket sort.

## Two Pointers

- [Container With Most Water](https://www.marauderbot.com/questions/container-with-most-water.md): Medium. How to solve Container With Most Water: maximize the area between two lines by moving the shorter pointer inward with two pointers.
- [3Sum](https://www.marauderbot.com/questions/three-sum.md): Medium. How to solve 3Sum: find every unique triplet in an array that sums to zero by sorting first and scanning with two pointers.
- [Trapping Rain Water](https://www.marauderbot.com/questions/trapping-rain-water.md): Hard. How to solve Trapping Rain Water: compute total water trapped between bars using two pointers that track the running max height on each side.
- [Valid Palindrome](https://www.marauderbot.com/questions/valid-palindrome.md): Easy. How to solve Valid Palindrome: check if a string reads the same forwards and backwards using two pointers that skip non-alphanumeric characters.
- [Two Sum II - Input Array Is Sorted](https://www.marauderbot.com/questions/two-sum-ii-input-array-is-sorted.md): Medium. How to solve Two Sum II: find two numbers in a sorted array that add to a target using two pointers that converge from both ends.

## Sliding Window

- [Longest Substring Without Repeating Characters](https://www.marauderbot.com/questions/longest-substring-without-repeating-characters.md): Medium. How to solve Longest Substring Without Repeating Characters: find the longest run of unique characters using a sliding window over the string.
- [Minimum Window Substring](https://www.marauderbot.com/questions/minimum-window-substring.md): Hard. How to solve Minimum Window Substring: find the smallest substring covering every required character using an expanding and shrinking window.
- [Best Time to Buy and Sell Stock](https://www.marauderbot.com/questions/best-time-to-buy-and-sell-stock.md): Easy. How to solve Best Time to Buy and Sell Stock: maximize profit from one trade with a single pass that tracks the lowest price seen so far.
- [Sliding Window Maximum](https://www.marauderbot.com/questions/sliding-window-maximum.md): Hard. How to solve Sliding Window Maximum: track the maximum in every fixed-size window efficiently using a monotonic decreasing deque.
- [Longest Repeating Character Replacement](https://www.marauderbot.com/questions/longest-repeating-character-replacement.md): Medium. How to solve Longest Repeating Character Replacement: find the longest substring made uniform with at most k swaps using a sliding window.
- [Permutation in String](https://www.marauderbot.com/questions/permutation-in-string.md): Medium. How to solve Permutation in String: detect whether one string contains an anagram of another using a fixed-size sliding window.

## Stack

- [Valid Parentheses](https://www.marauderbot.com/questions/valid-parentheses.md): Easy. How to solve Valid Parentheses: check that every bracket closes in the correct order using a single stack pass over the string.
- [Generate Parentheses](https://www.marauderbot.com/questions/generate-parentheses.md): Medium. How to solve Generate Parentheses: build every valid combination of n pairs using constrained backtracking.
- [Largest Rectangle in Histogram](https://www.marauderbot.com/questions/largest-rectangle-in-histogram.md): Hard. How to solve Largest Rectangle in Histogram: find the max rectangle area with a monotonic increasing stack of bars.
- [Evaluate Reverse Polish Notation](https://www.marauderbot.com/questions/evaluate-reverse-polish-notation.md): Medium. How to solve Evaluate Reverse Polish Notation: apply operators to a stack of operands in one left-to-right pass.
- [Min Stack](https://www.marauderbot.com/questions/min-stack.md): Medium. How to solve Min Stack: support push, pop, top, and constant-time minimum retrieval using a paired auxiliary stack.
- [Daily Temperatures](https://www.marauderbot.com/questions/daily-temperatures.md): Medium. How to solve Daily Temperatures: find the wait until a warmer day using a monotonic decreasing stack of indices.
- [Car Fleet](https://www.marauderbot.com/questions/car-fleet.md): Medium. How to solve Car Fleet: count merging fleets by comparing arrival times while sweeping cars from front to back.

## Binary Search

- [Median of Two Sorted Arrays](https://www.marauderbot.com/questions/median-of-two-sorted-arrays.md): Hard. How to solve Median of Two Sorted Arrays: binary search a partition point across both arrays to find the median in logarithmic time.
- [Search in Rotated Sorted Array](https://www.marauderbot.com/questions/search-in-rotated-sorted-array.md): Medium. How to solve Search in Rotated Sorted Array: identify which half stays sorted at each step to binary search around the rotation.
- [Search a 2D Matrix](https://www.marauderbot.com/questions/search-a-2d-matrix.md): Medium. How to solve Search a 2D Matrix: treat a row-sorted, column-linked grid as one flattened sorted list and binary search it directly.
- [Find Minimum in Rotated Sorted Array](https://www.marauderbot.com/questions/find-minimum-in-rotated-sorted-array.md): Medium. How to solve Find Minimum in Rotated Sorted Array: use binary search to locate the rotation point where the sorted order breaks.
- [Binary Search](https://www.marauderbot.com/questions/binary-search.md): Easy. How to solve Binary Search: locate a target in a sorted array by repeatedly halving the search range in logarithmic time.
- [Koko Eating Bananas](https://www.marauderbot.com/questions/koko-eating-bananas.md): Medium. How to solve Koko Eating Bananas: binary search over eating speed to find the slowest pace that still finishes every pile in time.
- [Time Based Key-Value Store](https://www.marauderbot.com/questions/time-based-key-value-store.md): Medium. How to solve Time Based Key-Value Store: store timestamped values per key and binary search the closest timestamp not after the query.

## Linked List

- [Add Two Numbers](https://www.marauderbot.com/questions/add-two-numbers.md): Medium. How to solve Add Two Numbers: add two reversed digit lists in one pass by tracking a running carry at each position.
- [Remove Nth Node From End of List](https://www.marauderbot.com/questions/remove-nth-node-from-end-of-list.md): Medium. How to solve Remove Nth Node From End of List: delete a node in one pass using two pointers separated by a fixed gap.
- [Merge Two Sorted Lists](https://www.marauderbot.com/questions/merge-two-sorted-lists.md): Easy. How to solve Merge Two Sorted Lists: splice two sorted lists into one in a single pass using a dummy head and two pointers.
- [Merge k Sorted Lists](https://www.marauderbot.com/questions/merge-k-sorted-lists.md): Hard. How to solve Merge k Sorted Lists: combine many sorted lists efficiently with a min heap or pairwise divide and conquer merging.
- [Reverse Nodes in k-Group](https://www.marauderbot.com/questions/reverse-nodes-in-k-group.md): Hard. How to solve Reverse Nodes in k-Group: reverse fixed-size chunks of a list in place and relink them group by group.
- [Copy List with Random Pointer](https://www.marauderbot.com/questions/copy-list-with-random-pointer.md): Medium. How to solve Copy List with Random Pointer: deep clone a list with random pointers using a hash map from old nodes to new nodes.
- [Linked List Cycle](https://www.marauderbot.com/questions/linked-list-cycle.md): Easy. How to solve Linked List Cycle: detect a cycle in constant space using the fast and slow pointer technique.
- [Reorder List](https://www.marauderbot.com/questions/reorder-list.md): Medium. How to solve Reorder List: interleave a list front to back in place by finding the middle, reversing the second half, and merging.
- [LRU Cache](https://www.marauderbot.com/questions/lru-cache.md): Medium. How to design an LRU Cache: get O(1) get and put by pairing a hash map with a doubly linked list ordered by recency.
- [Reverse Linked List](https://www.marauderbot.com/questions/reverse-linked-list.md): Easy. How to solve Reverse Linked List: reverse every next pointer in one pass using the prev-curr-next pointer technique.
- [Find the Duplicate Number](https://www.marauderbot.com/questions/find-the-duplicate-number.md): Medium. How to solve Find the Duplicate Number: locate the repeated value in constant space by treating the array as a linked list and finding its cycle.

## Trees

- [Validate Binary Search Tree](https://www.marauderbot.com/questions/validate-binary-search-tree.md): Medium. How to solve Validate Binary Search Tree: check every node against an inherited valid range, not just its direct children.
- [Same Tree](https://www.marauderbot.com/questions/same-tree.md): Easy. How to solve Same Tree: confirm two binary trees are structurally identical with matching values using a paired recursive comparison.
- [Binary Tree Level Order Traversal](https://www.marauderbot.com/questions/binary-tree-level-order-traversal.md): Medium. How to solve Binary Tree Level Order Traversal: group node values by depth using a breadth-first sweep with a queue.
- [Maximum Depth of Binary Tree](https://www.marauderbot.com/questions/maximum-depth-of-binary-tree.md): Easy. How to solve Maximum Depth of Binary Tree: measure the longest root-to-leaf path by combining each subtrees depth at every node.
- [Construct Binary Tree from Preorder and Inorder Traversal](https://www.marauderbot.com/questions/construct-binary-tree-from-preorder-and-inorder-traversal.md): Medium. How to solve Construct Binary Tree from Preorder and Inorder Traversal: use preorder roots to recursively split inorder ranges.
- [Balanced Binary Tree](https://www.marauderbot.com/questions/balanced-binary-tree.md): Easy. How to solve Balanced Binary Tree: check whether every nodes two subtree heights differ by at most one in a single bottom-up pass.
- [Binary Tree Maximum Path Sum](https://www.marauderbot.com/questions/binary-tree-maximum-path-sum.md): Hard. How to solve Binary Tree Maximum Path Sum: track the best downward chain at each node with post-order tree dynamic programming.
- [Binary Tree Right Side View](https://www.marauderbot.com/questions/binary-tree-right-side-view.md): Medium. How to solve Binary Tree Right Side View: capture the rightmost node at each depth using a level-order or right-first DFS traversal.
- [Invert Binary Tree](https://www.marauderbot.com/questions/invert-binary-tree.md): Easy. How to solve Invert Binary Tree: mirror every node by recursively swapping its left and right children.
- [Kth Smallest Element in a BST](https://www.marauderbot.com/questions/kth-smallest-element-in-a-bst.md): Medium. How to solve Kth Smallest Element in a BST: walk the tree in order and stop early, since that visits values in sorted order for free.
- [Lowest Common Ancestor of a Binary Search Tree](https://www.marauderbot.com/questions/lowest-common-ancestor-of-a-binary-search-tree.md): Medium. How to solve Lowest Common Ancestor of a Binary Search Tree: use ordering at each node to walk directly toward the split point of two nodes.
- [Serialize and Deserialize Binary Tree](https://www.marauderbot.com/questions/serialize-and-deserialize-binary-tree.md): Hard. How to solve Serialize and Deserialize Binary Tree: encode a preorder walk with explicit null markers to preserve tree shape.
- [Diameter of Binary Tree](https://www.marauderbot.com/questions/diameter-of-binary-tree.md): Easy. How to solve Diameter of Binary Tree: find the longest path between any two nodes by combining subtree heights at every node in one pass.
- [Subtree of Another Tree](https://www.marauderbot.com/questions/subtree-of-another-tree.md): Easy. How to solve Subtree of Another Tree: search a larger tree for a node whose subtree exactly matches a smaller candidate tree.
- [Count Good Nodes in Binary Tree](https://www.marauderbot.com/questions/count-good-nodes-in-binary-tree.md): Medium. How to solve Count Good Nodes in Binary Tree: carry the maximum ancestor value down each root-to-node path in a single DFS pass.

## Tries

- [Implement Trie (Prefix Tree)](https://www.marauderbot.com/questions/implement-trie-prefix-tree.md): Medium. How to solve Implement Trie (Prefix Tree): build a tree of characters so insert, search, and prefix checks all run in linear time.
- [Design Add and Search Words Data Structure](https://www.marauderbot.com/questions/design-add-and-search-words-data-structure.md): Medium. How to solve Design Add and Search Words Data Structure: extend a trie with depth-first search to support a wildcard dot in queries.
- [Word Search II](https://www.marauderbot.com/questions/word-search-ii.md): Hard. How to solve Word Search II: search a letter grid for many dictionary words at once by backtracking through a shared trie.

## Heap & Priority Queue

- [Kth Largest Element in an Array](https://www.marauderbot.com/questions/kth-largest-element-in-an-array.md): Medium. How to solve Kth Largest Element in an Array: find the kth largest value in one pass using quickselect partitioning or a bounded heap.
- [Find Median from Data Stream](https://www.marauderbot.com/questions/find-median-from-data-stream.md): Hard. How to solve Find Median from Data Stream: maintain the running median with two balanced heaps as numbers keep arriving.
- [Design Twitter](https://www.marauderbot.com/questions/design-twitter.md): Medium. How to solve Design Twitter: merge recent posts from followed users with a heap to build a fast news feed.
- [Task Scheduler](https://www.marauderbot.com/questions/task-scheduler.md): Medium. How to solve Task Scheduler: schedule CPU tasks with a cooldown by letting the most frequent task type set the timeline.
- [Kth Largest Element in a Stream](https://www.marauderbot.com/questions/kth-largest-element-in-a-stream.md): Easy. How to solve Kth Largest Element in a Stream: track the kth largest value as numbers arrive one at a time using a fixed-size min-heap.
- [K Closest Points to Origin](https://www.marauderbot.com/questions/k-closest-points-to-origin.md): Medium. How to solve K Closest Points to Origin: find the k nearest points to the origin efficiently with a bounded max-heap of distances.
- [Last Stone Weight](https://www.marauderbot.com/questions/last-stone-weight.md): Easy. How to solve Last Stone Weight: repeatedly smash the two heaviest stones together using a max-heap until at most one remains.

## Backtracking

- [Letter Combinations of a Phone Number](https://www.marauderbot.com/questions/letter-combinations-of-a-phone-number.md): Medium. How to solve Letter Combinations of a Phone Number: backtrack position by position through each digits mapped letters.
- [Combination Sum](https://www.marauderbot.com/questions/combination-sum.md): Medium. How to solve Combination Sum: backtrack through sorted candidates with unlimited reuse to reach a target while avoiding reordered duplicates.
- [Combination Sum II](https://www.marauderbot.com/questions/combination-sum-ii.md): Medium. How to solve Combination Sum II: backtrack over a sorted array with each element used once while skipping same-level duplicates.
- [Permutations](https://www.marauderbot.com/questions/permutations.md): Medium. How to solve Permutations: backtrack position by position, choosing from the remaining unused elements to enumerate every ordering.
- [N-Queens](https://www.marauderbot.com/questions/n-queens.md): Hard. How to solve N-Queens: place one queen per row via backtracking, pruning columns and diagonals already under attack.
- [Subsets](https://www.marauderbot.com/questions/subsets.md): Medium. How to solve Subsets: generate the full power set of an array by backtracking through an include or exclude decision at each element.
- [Word Search](https://www.marauderbot.com/questions/word-search.md): Medium. How to solve Word Search: trace a word through a letter grid with backtracking, marking and unmarking cells to avoid reuse.
- [Subsets II](https://www.marauderbot.com/questions/subsets-ii.md): Medium. How to solve Subsets II: generate the power set of a duplicate-containing array by sorting first and skipping repeats at each level.
- [Palindrome Partitioning](https://www.marauderbot.com/questions/palindrome-partitioning.md): Medium. How to solve Palindrome Partitioning: backtrack over every cut point of a string, keeping only splits where each piece is a palindrome.

## Graphs

- [Word Ladder](https://www.marauderbot.com/questions/word-ladder.md): Hard. How to solve Word Ladder: find the shortest chain of one letter word transformations between two words using breadth first search.
- [Surrounded Regions](https://www.marauderbot.com/questions/surrounded-regions.md): Medium. How to solve Surrounded Regions: capture enclosed regions of a grid by flood filling inward from border cells first.
- [Clone Graph](https://www.marauderbot.com/questions/clone-graph.md): Medium. How to solve Clone Graph: build a deep copy of a connected graph by mapping each original node to its clone during traversal.
- [Number of Islands](https://www.marauderbot.com/questions/number-of-islands.md): Medium. How to solve Number of Islands: count connected land clusters in a grid by flood filling each cluster with depth-first or breadth-first search.
- [Course Schedule](https://www.marauderbot.com/questions/course-schedule.md): Medium. How to solve Course Schedule: detect whether course prerequisites contain a cycle using depth first search or topological sort.
- [Course Schedule II](https://www.marauderbot.com/questions/course-schedule-ii.md): Medium. How to solve Course Schedule II: build a valid course order from prerequisites using topological sort over the dependency graph.
- [Graph Valid Tree](https://www.marauderbot.com/questions/graph-valid-tree.md): Medium. How to solve Graph Valid Tree: verify a graph is connected and has exactly n minus one edges, the signature of a tree.
- [Number of Connected Components in an Undirected Graph](https://www.marauderbot.com/questions/number-of-connected-components-in-an-undirected-graph.md): Medium. How to solve Number of Connected Components in an Undirected Graph: count separate components using flood fill or union find.
- [Pacific Atlantic Water Flow](https://www.marauderbot.com/questions/pacific-atlantic-water-flow.md): Medium. How to solve Pacific Atlantic Water Flow: find grid cells that can drain to both oceans by flooding uphill from each coastline inward.
- [Max Area of Island](https://www.marauderbot.com/questions/max-area-of-island.md): Medium. How to solve Max Area of Island: find the largest connected land region in a grid by counting cells during a flood fill traversal.
- [Rotting Oranges](https://www.marauderbot.com/questions/rotting-oranges.md): Medium. How to solve Rotting Oranges: find the minutes until every fresh orange rots using multi source breadth first search from all rotten cells.

## Dynamic Programming

- [Longest Palindromic Substring](https://www.marauderbot.com/questions/longest-palindromic-substring.md): Medium. How to solve Longest Palindromic Substring: find the longest substring that reads the same backward using dynamic programming over substring spans.
- [Climbing Stairs](https://www.marauderbot.com/questions/climbing-stairs.md): Easy. How to solve Climbing Stairs: count the ways to reach the top using one or two step moves with a Fibonacci-style bottom-up dynamic program.
- [Decode Ways](https://www.marauderbot.com/questions/decode-ways.md): Medium. How to solve Decode Ways: count how a digit string maps back to letters A through Z using dynamic programming over valid one and two digit groups.
- [Word Break](https://www.marauderbot.com/questions/word-break.md): Medium. How to solve Word Break: determine if a string splits into dictionary words using dynamic programming over reachable cut points.
- [House Robber](https://www.marauderbot.com/questions/house-robber.md): Medium. How to solve House Robber: find the maximum money you can steal from a row of houses without robbing two adjacent ones, using linear dynamic programming.
- [House Robber II](https://www.marauderbot.com/questions/house-robber-ii.md): Medium. How to solve House Robber II: maximize stolen money when houses form a circle, by running the linear house robber solution twice.
- [Longest Increasing Subsequence](https://www.marauderbot.com/questions/longest-increasing-subsequence.md): Medium. How to solve Longest Increasing Subsequence: find the longest strictly increasing subsequence length using dynamic programming with binary search.
- [Coin Change](https://www.marauderbot.com/questions/coin-change.md): Medium. How to solve Coin Change: find the fewest coins that sum to an amount using bottom-up dynamic programming over every subtotal.
- [Palindromic Substrings](https://www.marauderbot.com/questions/palindromic-substrings.md): Medium. How to solve Palindromic Substrings: count every palindromic substring of a string using the same expand-and-reuse dynamic programming idea.
- [Longest Common Subsequence](https://www.marauderbot.com/questions/longest-common-subsequence.md): Medium. How to solve Longest Common Subsequence: find the longest shared subsequence of two strings using a two-dimensional dynamic programming table.

## See also

- [System Design Interview Questions](https://www.marauderbot.com/questions/system-design.md): The most common system design interview questions, each worked through end to end: clarification questions, requirements, architecture diagram, data flow, and a component-by-component breakdown of APIs, data models, scaling, and tradeoffs.

---

Canonical URL: https://www.marauderbot.com/questions
