Coding interview problems

Practice 121 classic coding interview problems in Python, JavaScript, Java, Go, and Rust. Run your code against real test cases, then read the step-by-step solutions with time & space complexity.

Terminal view →

Your progress

0 / 121 solved

Prefer a structured plan?

Follow the ordered Interview Roadmap: 0/121 solved.

Browse by pattern

121 problems

Two Sum

easy

Find the indices of two distinct values whose sum matches a target.

arrays
hashmap

Merge Intervals

medium

Sort and merge every overlapping or touching interval into a disjoint list.

arrays
sorting

Reverse a String

easy

Return a string with every character in reverse order without changing characters.

strings

Longest Common Prefix

easy

Find the longest leading substring shared by every string in a list.

strings

Palindrome String

easy

Determine whether a phrase reads the same after ignoring case and non-alphanumeric characters.

strings
two-pointers

Palindromic Substrings

medium

Count every contiguous substring that reads the same forward and backward.

strings
dynamic-programming

Longest Palindromic Substring

medium

Return the longest contiguous substring that is a palindrome.

strings
dynamic-programming

Palindrome Linked List

easy

Check whether the values represented by a singly linked list form a palindrome.

linked-list
two-pointers

Roman to Integer

easy

Convert a valid canonical Roman numeral into its integer value.

strings
math

Letter Combinations of a Phone Number

medium

Generate every letter combination represented by digits on a telephone keypad.

strings
backtracking

Valid Parentheses

easy

Validate that every bracket closes in the correct order and with the matching type.

strings
stack

Find the Index of the First Occurrence in a String

easy

Return the starting index of the first exact occurrence of one string inside another.

strings
two-pointers

Length of Last Word

easy

Measure the final whitespace-delimited word in a string.

strings

Spiral Matrix

medium

Generate an n-by-n matrix filled clockwise in spiral order.

arrays
matrix

FizzBuzz

easy

Produce the classic FizzBuzz sequence using divisibility rules.

strings
math

Contains Duplicate

easy

Determine whether any integer appears more than once in an array.

arrays
hashmap

Maximum Subarray

medium

Return the largest sum obtainable from a non-empty contiguous subarray.

arrays
dynamic-programming

Fibonacci Number

easy

Compute the zero-indexed Fibonacci number with an iterative recurrence.

math
dynamic-programming

Factorial

easy

Compute the product of all positive integers up to a non-negative input.

math
recursion

Move Zeroes

easy

Move every zero to the end while preserving non-zero element order.

arrays
two-pointers

Single Number

easy

Find the only value that is not paired using constant extra space.

arrays
bit-manipulation

Best Time to Buy and Sell Stock

easy

Find the best profit from one buy followed by one later sale.

arrays
dynamic-programming

Valid Anagram

easy

Check whether two lowercase strings contain identical character frequencies.

strings
hashmap

Binary Search

easy

Locate a target in a sorted unique array using logarithmic search.

arrays
binary-search

Count Vowels

easy

Count ASCII vowels in a string without changing other characters.

strings

Climbing Stairs

easy

Count ordered one-step and two-step paths to the top of a staircase.

math
dynamic-programming

Product of Array Except Self

medium

Build prefix and suffix products without division.

arrays
prefix-sum

Trapping Rain Water

hard

Compute water trapped between elevation bars after rainfall.

arrays
two-pointers

Edit Distance

hard

Measure the minimum insertions, deletions, and replacements between two strings.

strings
dynamic-programming

Median of Two Sorted Arrays

hard

Find the combined median without fully merging two sorted arrays.

arrays
binary-search

Longest Valid Parentheses

hard

Measure the longest contiguous substring of balanced parentheses.

strings
stack

Palindrome Number

easy

Determine whether a base-10 integer reads identically in reverse.

math

Plus One

easy

Add one to a number represented as an array of digits.

arrays
math

Majority Element

easy

Find the element that appears more than half the time in an array.

arrays
hashmap

Is Subsequence

easy

Check whether one string is a subsequence of another.

strings
two-pointers

First Unique Character in a String

easy

Find the index of the first non-repeating character in a string.

strings
hashmap

Search Insert Position

easy

Return the index where a target should be inserted into a sorted array.

arrays
binary-search

House Robber

medium

Maximize the loot from non-adjacent houses along a street.

arrays
dynamic-programming

Jump Game

medium

Decide whether you can reach the last index of an array of jump lengths.

arrays
greedy

Coin Change

medium

Find the fewest coins needed to make up a given amount.

arrays
dynamic-programming

Longest Substring Without Repeating Characters

medium

Find the length of the longest substring without repeating characters.

strings
sliding-window

Container With Most Water

medium

Find two lines that together with the x-axis hold the most water.

arrays
two-pointers

Number of Islands

medium

Count the number of islands in a grid of land and water cells.

matrix
graph

Word Break

hard

Determine whether a string can be segmented into words from a dictionary.

strings
dynamic-programming

Longest Increasing Subsequence

hard

Find the length of the longest strictly increasing subsequence.

arrays
dynamic-programming

Largest Rectangle in Histogram

hard

Find the area of the largest rectangle that fits under a histogram.

arrays
stack

Sliding Window Maximum

hard

Return the maximum of every contiguous window of size k.

arrays
sliding-window

Jump Game II

hard

Find the minimum number of jumps needed to reach the last index.

arrays
greedy

Reverse Linked List

easy

Reverse a singly linked list in place using real ListNode structures.

linked-list
pointers

Maximum Depth of Binary Tree

easy

Find the maximum depth of a binary tree using real TreeNode structures.

binary-tree
recursion

Course Schedule

medium

Determine whether all courses can be completed given prerequisite pairs.

graph
topological-sort

Rotting Oranges

medium

Find how many minutes are needed for rot to spread to every fresh orange.

matrix
graph

Redundant Connection

medium

Find the edge that creates a cycle in an otherwise tree-shaped graph.

graph
union-find

Kth Largest Element in an Array

medium

Return the kth largest value without requiring a fully sorted array.

arrays
heap

Top K Frequent Elements

medium

Return the k values that occur most often in an integer array.

arrays
hashmap

Subsets

medium

Generate every subset in the power set of a distinct integer array.

arrays
backtracking

Invert Binary Tree

easy

Swap every binary-tree node's left and right children.

binary-tree
depth-first-search

Same Tree

easy

Determine whether two binary trees have identical structure and values.

binary-tree
depth-first-search

Validate Binary Search Tree

medium

Check whether a binary tree satisfies strict BST ordering.

binary-tree
binary-search-tree

Binary Tree Level Order Traversal

medium

Return binary-tree values grouped by breadth-first level.

binary-tree
breadth-first-search

Lowest Common Ancestor of a BST

medium

Find the lowest shared ancestor of two values in a binary search tree.

binary-tree
binary-search-tree

Merge Two Sorted Lists

easy

Merge two sorted linked lists by rewiring their nodes.

linked-list
two-pointers

Linked List Cycle

easy

Detect a cycle from a linked list represented by next-node indices.

linked-list
two-pointers

Reorder List

medium

Reorder a linked list by alternating nodes from its front and back.

linked-list
two-pointers

Clone Graph

medium

Deep-copy a connected graph from its adjacency-list representation.

graph
depth-first-search

Pacific Atlantic Water Flow

medium

Find grid cells that can flow to both oceans under height constraints.

graph
matrix

Network Delay Time

medium

Compute how long a weighted directed network takes to receive a signal.

graph
shortest-path

Merge K Sorted Lists

hard

Merge k sorted sequences using a heap of current candidates.

linked-list
heap

Find Median from Data Stream

hard

Return the median after incrementally inserting every stream value.

heap
priority-queue

Permutations

medium

Generate every ordering of a distinct integer array.

arrays
backtracking

Combination Sum

medium

Find target-sum combinations when candidate values may be reused.

arrays
backtracking

Word Search

medium

Find a word by backtracking through adjacent grid cells.

matrix
backtracking

Implement Trie

medium

Execute insert, exact-search, and prefix-search operations on a trie.

trie
string

Design Add and Search Words

medium

Build a trie that supports exact letters and wildcard searches.

trie
backtracking

LRU Cache

medium

Simulate a bounded least-recently-used key-value cache.

hashmap
linked-list

Unique Paths

medium

Count distinct top-left to bottom-right grid paths moving only right or down.

math
dynamic-programming

Pascal's Triangle

easy

Build the first numRows rows of Pascal's triangle by summing adjacent values.

math
combinatorics

Count Primes

medium

Count prime numbers strictly below n with the Sieve of Eratosthenes.

math

Sqrt(x)

easy

Return the floored integer square root without a built-in sqrt function.

math
binary-search

Happy Number

easy

Decide whether summing squared digits eventually reaches one or cycles.

math
hashmap

Excel Sheet Column Number

easy

Convert a spreadsheet column title like AB into its 1-based number.

math
strings

Insert Interval

medium

Insert one interval into sorted disjoint intervals and merge any overlaps.

arrays
intervals

Non-overlapping Intervals

medium

Remove the fewest intervals so the remaining schedule has no overlaps.

arrays
intervals

Sort Colors

medium

Sort an array of 0s, 1s, and 2s in one in-place pass.

arrays
sorting

Number of Provinces

medium

Count how many disconnected city groups exist in an undirected connectivity matrix.

graph
union-find

Minimum Size Subarray Sum

medium

Find the shortest contiguous subarray whose sum reaches a target.

arrays
sliding-window

3Sum

medium

Find every unique triplet of numbers in an array that sums to zero.

arrays
two-pointers

Rotate Image

medium

Rotate an n x n matrix 90 degrees clockwise and return the rotated matrix.

matrix
arrays

Search in Rotated Sorted Array

medium

Find a target value's index in an ascending array that has been rotated at an unknown pivot.

arrays
binary-search

Longest Consecutive Sequence

medium

Find the length of the longest run of consecutive integers in an unsorted array.

arrays
hashmap

Sum of Two Integers

easy

Add two integers together without using the + or - operators.

bit-manipulation
math

Symmetric Tree

easy

Check whether a binary tree is a mirror image of itself around its center.

binary-tree
depth-first-search

Diameter of Binary Tree

easy

Find the length in edges of the longest path between any two nodes in a binary tree.

binary-tree
depth-first-search

Kth Smallest Element in a BST

medium

Return the k-th smallest value stored in a binary search tree.

binary-tree
binary-search-tree

Add Two Numbers

medium

Add two non-negative integers represented as reversed-digit linked lists.

linked-list
math

Remove Nth Node From End of List

medium

Delete the n-th node from the end of a singly linked list in one pass.

linked-list
two-pointers

Path Sum

medium

Determine whether a binary tree has a root-to-leaf path whose values sum to a target.

binary-tree
depth-first-search

Convert Sorted Array to Binary Search Tree

easy

Build a height-balanced binary search tree from an ascending array of unique integers.

binary-tree
binary-search-tree

Reverse Linked List II

medium

Reverse only the nodes between two 1-indexed positions of a singly linked list, in one pass.

linked-list
pointers

Swap Nodes in Pairs

medium

Swap every adjacent pair of nodes in a singly linked list without changing the node values.

linked-list
pointers

Binary Tree Right Side View

medium

Return the values visible when looking at a binary tree from the right side, ordered top to bottom.

binary-tree
breadth-first-search

Merge Two Binary Trees

easy

Overlay two binary trees, summing values where both trees have a node.

binary-tree
depth-first-search

Path Sum II

medium

Find every root-to-leaf path in a binary tree whose values sum to a target.

binary-tree
depth-first-search

Construct Binary Tree from Preorder & Inorder

medium

Rebuild a unique binary tree from its preorder and inorder traversal arrays.

binary-tree
divide-and-conquer

Linked List Cycle II

medium

Find the index where a cycle begins in a linked list represented by next-node indices, or -1 if there is none.

linked-list
two-pointers

Odd Even Linked List

medium

Regroup a singly linked list so all odd-positioned nodes come before all even-positioned nodes, preserving relative order within each group.

linked-list
two-pointers

Task Scheduler

medium

Find the minimum timeline length needed to schedule tasks with a cooldown between identical letters.

heap
priority-queue

Connected Components in an Undirected Graph

medium

Count how many disconnected components remain in an undirected graph with labeled vertices.

graph
union-find

Palindrome Partitioning

medium

Return every way to cut a string into palindromic substrings, ordered by DFS prefix exploration.

strings
backtracking

Word Search II

hard

Find all dictionary words that can be traced through adjacent board cells without reusing a cell.

trie
backtracking

Word Ladder

hard

Find the length of the shortest one-letter transformation sequence from a start word to an end word.

graph
breadth-first-search

Daily Temperatures

medium

Return how many days each temperature must wait until a warmer reading appears.

arrays
stack

Decode Ways

medium

Count how many valid A1Z26 decodings a digit string can represent.

strings
dynamic-programming

Gas Station

medium

Find the unique starting station that lets you complete a circular route, or report failure.

arrays
greedy

Alien Dictionary

hard

Recover a valid alien letter order from a sorted word list, returning the lexicographically smallest valid order.

graph
topological-sort

Binary Tree Maximum Path Sum

hard

Find the largest sum obtainable from any non-empty path inside a binary tree.

binary-tree
depth-first-search

Meeting Rooms II

medium

Return the minimum number of meeting rooms needed so every interval can be scheduled without overlap.

intervals
sorting

Minimum Window Substring

hard

Find the shortest substring of s that still contains every character required by t, including duplicates.

strings
sliding-window

Longest Increasing Path in a Matrix

hard

Find the maximum path length you can build by moving to orthogonally adjacent cells with strictly larger values.

matrix
graph

Range Sum Query - Mutable

medium

Process point updates and inclusive range-sum queries efficiently over the same integer array.

design
prefix-sum

Cheapest Flights Within K Stops

medium

Find the least expensive route from src to dst when you may use at most k intermediate stops.

graph
shortest-path

FAQ