The Blind 75, visualized.
Step through every algorithm, compare four approaches from brute force to production grade, and rehearse the follow-up questions an interviewer will ask. 63 of 75 problems are live.
#1 · Arrays & Hashing
Two Sum
Given an array of integers and a target, return the indices of the two numbers that add up to the target. Exactly one solution exists and you may not use the same element twice.
#2 · Sliding Window
Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring that contains no repeated character.
#3 · Trees
Invert Binary Tree
Given the root of a binary tree, invert it (swap every node's left and right children) and return its root.
#4 · Graphs
Course Schedule
There are n courses, and prerequisites[i] = [a, b] means you must take b before a. Return true if you can finish all courses, which is the same as asking whether the prerequisite graph has no cycle.
#5 · 1D DP
Coin Change
Given coin denominations and a target amount, return the fewest coins needed to make that amount, or −1 if it is impossible. Each coin can be used any number of times.
#6 · Arrays & Hashing
Contains Duplicate
Given an integer array, return true if any value appears at least twice, and false if every element is distinct.
#7 · Arrays & Hashing
Valid Anagram
Given two strings s and t, return true if t is an anagram of s: the same letters, each used exactly the same number of times, in any order.
#8 · Sliding Window
Best Time to Buy and Sell Stock
Given daily prices, choose one day to buy and a later day to sell to maximize profit. Return the maximum profit, or 0 if no profitable trade exists.
#9 · Arrays & Hashing
Product of Array Except Self
Return an array where out[i] is the product of every element of nums except nums[i], in O(n) time and without using division.
#10 · Arrays & Hashing
Group Anagrams
Given an array of strings, group the anagrams together. You may return the groups in any order.
#11 · Arrays & Hashing
Encode and Decode Strings
Design an algorithm to encode a list of strings into a single string, and decode it back to the original list. The strings can contain any characters, so no character is safe to use as a plain separator.
#12 · Arrays & Hashing
Longest Consecutive Sequence
Given an unsorted array of integers, return the length of the longest run of consecutive values (like 1, 2, 3, 4), regardless of their order in the array. The solution must run in O(n) time.
#13 · Two Pointers
Valid Palindrome
After lowercasing and removing every non-alphanumeric character, a phrase is a palindrome if it reads the same forwards and backwards. Given a string, return true if it is a palindrome.
#14 · Two Pointers
3Sum
Given an integer array, return all unique triplets [a, b, c] with a + b + c = 0. The solution set must not contain duplicate triplets.
#15 · Two Pointers
Container With Most Water
Given n vertical lines of given heights, pick two of them that together with the x-axis form a container holding the most water, and return that amount. (Area = distance between the lines × the shorter height.)
#16 · Sliding Window
Minimum Window Substring
Given strings s and t, return the shortest substring of s that contains every character of t (including duplicates). Return an empty string if no such window exists.
#17 · Sliding Window
Longest Repeating Character Replacement
Given a string of uppercase letters and an integer k, you may replace at most k characters with any letter. Return the length of the longest substring that can be turned into a single repeated letter.
#18 · Linked List
Reverse Linked List
Given the head of a singly linked list, reverse the list and return the new head.
#19 · Linked List
Merge Two Sorted Lists
Merge two sorted linked lists into one sorted list by splicing together the nodes of the two lists, and return its head.
#20 · Linked List
Reorder List
Rearrange L0 → L1 → … → Ln−1 → Ln into L0 → Ln → L1 → Ln−1 → L2 → … in place, without changing any node values.
#21 · Linked List
Remove Nth Node From End of List
Given the head of a linked list, remove the n-th node from the end of the list and return its head.
#22 · Linked List
Linked List Cycle
Given the head of a linked list, return true if the list contains a cycle: some node can be reached again by continuously following next.
#23 · Linked List
Merge k Sorted Lists
Given an array of k sorted linked lists, merge them into one sorted linked list and return its head.
#24 · Stack
Valid Parentheses
Given a string containing only '(', ')', '{', '}', '[' and ']', determine whether it is valid: every bracket is closed by the same type, in the correct order.
#25 · Binary Search
Find Minimum in Rotated Sorted Array
A sorted array of unique values was rotated an unknown number of times (for example [0,1,2,4,5,6,7] became [4,5,6,7,0,1,2]). Return its minimum element in O(log n) time.
#26 · Binary Search
Search in Rotated Sorted Array
A sorted array of distinct values was rotated at an unknown pivot. Given a target, return its index, or −1 if it is absent, in O(log n) time.
#27 · Heap / Priority Queue
Top K Frequent Elements
Given an integer array and an integer k, return the k most frequent elements, in any order. The algorithm should be better than O(n log n).
#28 · Heap / Priority Queue
Find Median from Data Stream
Design a data structure that supports addNum(num), which inserts an integer from a data stream, and findMedian(), which returns the median of everything inserted so far.
#29 · Trees
Maximum Depth of Binary Tree
Given the root of a binary tree, return its maximum depth: the number of nodes on the longest path from the root down to the farthest leaf.
#30 · Trees
Same Tree
Given the roots of two binary trees p and q, return true if they are structurally identical and every pair of corresponding nodes has the same value.
#31 · Trees
Subtree of Another Tree
Given the roots of two binary trees root and subRoot, return true if some node of root together with ALL of its descendants is identical to subRoot.
#32 · Trees
Lowest Common Ancestor of a BST
Given a binary SEARCH tree and two of its nodes p and q, find their lowest common ancestor: the deepest node that has both p and q as descendants (a node counts as a descendant of itself).
#33 · Trees
Binary Tree Level Order Traversal
Given the root of a binary tree, return the values of its nodes level by level, from left to right, as a list of lists.
#34 · Trees
Validate Binary Search Tree
Given the root of a binary tree, decide whether it is a valid binary search tree: for EVERY node, all values in its left subtree are smaller and all values in its right subtree are larger.
#35 · Trees
Kth Smallest Element in a BST
Given the root of a binary search tree and an integer k, return the k-th smallest value (1-indexed) among all node values.
#36 · Trees
Construct Binary Tree from Preorder and Inorder Traversal
Given the pre-order and in-order traversals of a binary tree with distinct values, rebuild and return the tree.
#37 · Trees
Binary Tree Maximum Path Sum
A path is a sequence of nodes where each adjacent pair is connected by an edge; it need not pass through the root. Return the maximum sum of node values over all non-empty paths in the tree.
#38 · Trees
Serialize and Deserialize Binary Tree
Design an algorithm to turn a binary tree into a string (serialize) and to rebuild exactly the same tree from that string (deserialize). The encoding format is up to you.
#39 · Tries
Implement Trie (Prefix Tree)
Design a Trie with insert(word), search(word) for an exact stored word, and startsWith(prefix) for any stored word that begins with the prefix.
#40 · Tries
Design Add and Search Words Data Structure
Design a WordDictionary with addWord(word) and search(pattern), where a "." in the pattern matches any single letter.
#41 · Tries
Word Search II
Given an m × n board of letters and a list of words, return every word that can be built from letters of sequentially adjacent cells (up, down, left, right), using each cell at most once per word.
#42 · Backtracking
Combination Sum
Given distinct positive integers and a target, return every unique combination of the numbers that adds up to the target. The same number may be used any number of times, and order inside a combination does not matter.
#43 · Backtracking
Word Search
Given an m × n grid of letters and a word, return true if the word can be constructed from letters of sequentially adjacent cells (horizontal or vertical), using each cell at most once.
#44 · Graphs
Clone Graph
Given a reference to a node in a connected undirected graph, return a deep copy: new nodes with the same values and the same neighbour structure, sharing nothing with the original.
#45 · Graphs
Number of Islands
Given an m × n grid of "1" (land) and "0" (water), count the islands. An island is land connected horizontally or vertically; diagonal contact does not count.
#46 · Graphs
Pacific Atlantic Water Flow
Given a grid of heights, rain water flows to a neighbouring cell with equal or lower height. The Pacific touches the top and left edges and the Atlantic touches the bottom and right edges. Return every cell from which water can flow to both oceans.
#47 · Graphs
Graph Valid Tree
Given n nodes labelled 0 to n − 1 and a list of undirected edges, check whether the edges form a valid tree: connected, with no cycles.
#48 · Graphs
Number of Connected Components in an Undirected Graph
Given n nodes labelled 0 to n − 1 and a list of undirected edges, return the number of connected components.
#49 · Advanced Graphs
Alien Dictionary
You receive a list of words from an alien language, sorted lexicographically by that language's own alphabet. Return a string of its letters in a valid order, or an empty string if the ordering is inconsistent.
#50 · 1D DP
Climbing Stairs
You climb a staircase of n steps, taking 1 or 2 steps at a time. In how many distinct ways can you reach the top?
#51 · 1D DP
House Robber
Given the amount of money in each house along a street, return the maximum you can rob without robbing two adjacent houses.
#52 · 1D DP
House Robber II
The houses are arranged in a circle, so the first and last houses are neighbours. Return the maximum you can rob without robbing two adjacent houses.
#53 · 1D DP
Longest Palindromic Substring
Given a string s, return its longest palindromic substring (a contiguous part that reads the same forwards and backwards).
#54 · 1D DP
Palindromic Substrings
Given a string s, return the number of palindromic substrings in it. Substrings with different positions count separately even if they have the same letters.
#55 · 1D DP
Decode Ways
A message of digits encodes letters with A=1 … Z=26. Given a string of digits, return the number of ways to decode it. A zero cannot stand alone.
#56 · 1D DP
Maximum Product Subarray
Given an integer array, find a contiguous non-empty subarray with the largest product and return that product.
#57 · 1D DP
Word Break
Given a string s and a dictionary of words, return true if s can be split into a sequence of one or more dictionary words (words may be reused).
#58 · 1D DP
Longest Increasing Subsequence
Given an integer array, return the length of the longest strictly increasing subsequence (elements in order, not necessarily adjacent).
#59 · 2D DP
Unique Paths
A robot starts at the top-left of an m × n grid and may move only right or down. How many distinct paths lead to the bottom-right corner?
#60 · 2D DP
Longest Common Subsequence
Given two strings, return the length of their longest common subsequence: the longest sequence of characters that appears, in order but not necessarily contiguously, in both.
#61 · Greedy
Maximum Subarray
Given an integer array, find the contiguous non-empty subarray with the largest sum and return that sum.
#62 · Greedy
Jump Game
Each element of the array is the maximum jump length from that position. Starting at the first index, can you reach the last index?
#63 · Intervals
Merge Intervals
Given a list of intervals [start, end], merge all overlapping ones and return the non-overlapping intervals that cover the same ranges.