Two Sum
Given an array of integers nums and an integer target, return indices of the two numbers that add up to target. Each input has exactly one solution; you may not use the same element twice.
Best Time to Buy and Sell Stock
You are given an array prices where prices[i] is the price of a stock on day i. Choose one day to buy and a later day to sell to maximize profit. Return 0 if no profit is possible.
Contains Duplicate
Given an integer array nums, return true if any value appears at least twice in the array, and false if every element is distinct.
Product of Array Except Self
Given nums, return an array answer where answer[i] equals the product of all elements of nums except nums[i]. Solve in O(n) without using division.
Maximum Subarray
Given an integer array nums, find the contiguous subarray with the largest sum and return that sum. (Kadane's algorithm.)
Maximum Product Subarray
Given an integer array nums, find a contiguous subarray that has the largest product and return that product. Track both the running max and min because of negatives.
Find Minimum in Rotated Sorted Array
An ascending sorted array of unique elements has been rotated. Find the minimum element in O(log n).
Search in Rotated Sorted Array
Given a rotated sorted array of distinct integers and a target, return its index, or -1 if not found. Must run in O(log n).
3Sum
Given nums, return all unique triplets [a,b,c] such that a+b+c == 0. The solution set must not contain duplicate triplets.
Container With Most Water
Given n non-negative integers representing wall heights, find two lines that with the x-axis form a container holding the most water.
Sum of Two Integers
Given two integers a and b, return their sum without using the operators + or -. Use bit manipulation: XOR for sum-without-carry, AND+shift for carry.
Number of 1 Bits
Write a function that takes an unsigned integer and returns the number of 1 bits (Hamming weight). Trick: n & (n-1) clears the lowest set bit.
Counting Bits
Given n, return an array of length n+1 where ans[i] is the number of 1's in the binary representation of i. Solve in O(n).
Missing Number
Given an array nums containing n distinct numbers in [0, n], return the only number in the range that is missing.
Reverse Bits
Reverse the bits of a 32-bit unsigned integer. Shift the answer left and OR in the lowest bit of n; then shift n right.
Climbing Stairs
You can climb 1 or 2 steps at a time. How many distinct ways can you reach step n? (Fibonacci.)
Coin Change
Given coin denominations and an amount, return the fewest number of coins needed to make up that amount. Return -1 if impossible.
Longest Increasing Subsequence
Given an integer array nums, return the length of the longest strictly increasing subsequence. Patience sort gives O(n log n).
Longest Common Subsequence
Given two strings text1 and text2, return the length of their longest common subsequence (not necessarily contiguous).
Word Break
Given string s and a dictionary of words, return true if s can be segmented into a space-separated sequence of dictionary words.
Combination Sum IV
Given an array of distinct positive integers nums and a target, return the number of possible orderings (sequences) that sum to target.
House Robber
Given non-negative integers representing money in each house, return the max you can rob without robbing two adjacent houses.
House Robber II
Same as House Robber, but the houses are arranged in a circle (first and last are adjacent). Run House Robber on nums[1:] and nums[:-1].
Decode Ways
A message of digits encodes letters: 'A'=1 ... 'Z'=26. Return the number of ways to decode the string.
Unique Paths
On an m x n grid, a robot at top-left moves only right or down. How many distinct paths reach the bottom-right?
Jump Game
Given nums where nums[i] is the max jump length from index i, can you reach the last index? Greedy: track furthest reach.
Clone Graph
Given a reference to a node in a connected undirected graph, return a deep copy of the graph. Use DFS + a Map from original to clone.
Course Schedule
n courses labeled 0..n-1 with prerequisite pairs [a,b] meaning b must precede a. Return true if all courses can be finished (no cycles). Use Kahn's topological sort.
Pacific Atlantic Water Flow
Given an m x n matrix of heights, water flows to a neighbor if its height is <=. Return cells from which water can flow to BOTH the Pacific (top/left) and Atlantic (bottom/right).
Number of Islands
Given an m x n grid of '1' (land) and '0' (water), return the number of islands. An island is a group of '1's connected 4-directionally.
Longest Consecutive Sequence
Given an unsorted array, return the length of the longest consecutive elements sequence. Solve in O(n). Only start counting from sequence starts.
Alien Dictionary
Given a sorted list of words from an alien language, derive the order of letters. Build a graph from each adjacent word pair and topo-sort.
Graph Valid Tree
Given n nodes (0..n-1) and a list of edges, decide if these edges form a valid tree. A valid tree has exactly n-1 edges, is connected, and has no cycles.
Number of Connected Components
Given n nodes and an array of edges in an undirected graph, return the number of connected components. Union-Find: start with n components and decrement on each successful union.
Insert Interval
Given non-overlapping intervals sorted by start, insert a new interval (merging if necessary) and return the result.
Merge Intervals
Given an array of intervals, merge all overlapping intervals and return the merged list. Sort by start; merge by extending the last interval's end.
Non-overlapping Intervals
Given intervals, return the minimum number you must remove so that the rest are non-overlapping. Greedy by earliest end time.
Meeting Rooms
Given an array of meeting time intervals, determine if a person could attend all meetings (no overlaps).
Meeting Rooms II
Given meeting intervals, return the minimum number of conference rooms needed. Use sorted starts/ends two-pointer or a min-heap of end times.
Reverse Linked List
Given the head of a singly linked list, reverse the list and return the new head.
Linked List Cycle
Given head of a linked list, determine if it contains a cycle. Floyd's tortoise & hare: slow moves 1, fast moves 2; they meet inside any cycle.
Merge Two Sorted Lists
Merge two sorted linked lists and return as a single sorted list, splicing nodes (no new node creation needed).
Merge K Sorted Lists
Given an array of k sorted linked lists, merge them into one sorted list. Pairwise merge (divide & conquer) is O(N log k).
Remove Nth Node From End
Given the head of a linked list, remove the nth node from the end and return the head. Use two pointers: advance fast n+1 steps first.
Reorder List
Given L0->L1->...->Ln-1, reorder it to L0->Ln-1->L1->Ln-2->L2->Ln-3->... in place. Steps: find middle, reverse second half, weave the two halves.
Set Matrix Zeroes
Given an m x n matrix, if a cell is 0, set its entire row and column to 0 — in place. Use the first row/col as markers; track them separately.
Spiral Matrix
Given an m x n matrix, return all elements in spiral order (right, down, left, up — repeat). Track top/bottom/left/right boundaries.
Rotate Image
Rotate an n x n matrix 90 degrees clockwise in place. Trick: transpose, then reverse each row.
Word Search
Given an m x n grid of letters and a word, return true if the word can be constructed from sequentially adjacent cells. The same cell may not be used twice.
Longest Substring Without Repeating Characters
Given a string s, find the length of the longest substring without repeating characters. Sliding window with a Map of last-seen indices.
Longest Substring Without Repeating Characters
Longest Repeating Character Replacement
Given string s and integer k, you may replace at most k characters with any letter. Return the length of the longest substring containing the same letter.
Longest Repeating Character Replacement
Minimum Window Substring
Given strings s and t, return the minimum window substring of s such that every character in t (including counts) is included in the window. Sliding window + counts.
Valid Anagram
Given two strings s and t, return true if t is an anagram of s. Compare character counts.
Group Anagrams
Given an array of strings, group the anagrams together. Use a sorted version of each string as the bucket key.
Valid Parentheses
Given a string of '()[]{}', determine if the input has properly matched and nested brackets.
Valid Palindrome
Given a string s, return true if it is a palindrome considering only alphanumeric characters and ignoring case.
Longest Palindromic Substring
Given a string s, return the longest palindromic substring. Expand around each center (odd and even).
Palindromic Substrings
Given a string s, return the number of palindromic substrings in it. Expand around centers and count.
Encode and Decode Strings
Design an algorithm to encode a list of strings to one string and decode it back. Length-prefix each string with a delimiter, e.g. '5#hello'.
Maximum Depth of Binary Tree
Given the root of a binary tree, return its maximum depth.
Same Tree
Given the roots of two binary trees p and q, return true if they are structurally identical and the nodes have the same values.
Invert Binary Tree
Given the root of a binary tree, invert the tree (mirror it) and return its root.
Binary Tree Maximum Path Sum
A path is any sequence of nodes connected by edges. Return the maximum sum of any non-empty path. Track best at each node; return 'gain' upward.
Binary Tree Level Order Traversal
Given the root of a binary tree, return the level order traversal of its nodes' values (BFS).
Serialize and Deserialize Binary Tree
Design an algorithm to serialize a binary tree to a string and deserialize it back. Preorder DFS with '#' for null works well.
Subtree of Another Tree
Given roots root and subRoot, return true if subRoot has the same structure & values as some subtree of root.
Construct Tree from Preorder & Inorder
Given preorder and inorder traversals of a binary tree (with unique values), reconstruct and return the tree. preorder[0] is the root; locate it in inorder to split left/right.
Validate Binary Search Tree
Given the root of a binary tree, determine if it is a valid BST. Pass an open (lo, hi) range down recursively.
Kth Smallest Element in a BST
Given the root of a BST and an integer k, return the kth smallest value (1-indexed). Iterative inorder traversal.
Lowest Common Ancestor of BST
Given a BST and two nodes p and q, return their lowest common ancestor. Walk down: when p and q split, that node is the LCA.
Implement Trie (Prefix Tree)
Implement a Trie with insert(word), search(word), and startsWith(prefix) operations.
Add and Search Word
Design WordDictionary supporting addWord and search where '.' matches any letter. DFS over a Trie when '.' is encountered.
Word Search II
Given a board of letters and a list of words, return all words that can be formed in the board. Build a Trie of the words and DFS once.
Top K Frequent Elements
Given nums and integer k, return the k most frequent elements. Bucket sort by frequency for O(n).
Find Median from Data Stream
Design MedianFinder with addNum(num) and findMedian(). Maintain a max-heap (lower half) and min-heap (upper half), kept balanced.