Two Sum
Given integers and a target, return indices of two distinct entries whose sum equals the target, or None. Example: [2,7,11,15], 9 → (0,1).
Practise answering first. Each answer takes two clicks to reveal, so you can test your recall without spoilers.
Start with arrays, then work through these patterns toward trees. Select a topic to filter the problems.
Showing 120 of 120 questions
Given integers and a target, return indices of two distinct entries whose sum equals the target, or None. Example: [2,7,11,15], 9 → (0,1).
Return whether an array contains a repeated value. Example: [1,2,3,1] → true.
Determine whether two strings contain the same characters with the same counts. Example: listen, silent → true.
Return an array where position i contains the product of every input except nums[i], without division. Example: [1,2,3,4] → [24,12,8,6].
Check whether a string is a palindrome considering only alphanumeric characters and ignoring case. Example: A man, a plan, a canal: Panama → true.
Given a sorted array, find zero-based indices of two numbers that add to a target; return None if absent. Example: [1,2,4,8], 6 → (1,2).
Check whether a string of parentheses, brackets and braces is balanced and properly nested. Example: ([]) → true; ([)] → false.
Find target in an ascending sorted array and return its index, or -1. Example: [1,3,8], 8 → 2.
Return the length of the longest contiguous substring containing no repeated character. Example: abba → 2.
Reverse a singly linked list and return the new head. Each node has a next attribute. Example: 1 → 2 → 3 becomes 3 → 2 → 1.
Determine whether a singly linked list has a cycle. Example: a node whose next link points to itself has a cycle.
Return the maximum number of nodes on a root-to-leaf path. Nodes expose left and right. A single-node tree has depth 1.
Swap every node's left and right children and return the root. Example: root 1 with children 2 and 3 becomes root 1 with children 3 and 2.
Return the indices of two distinct numbers summing to target, or null. Example: [2,7,11,15], 9 → [0,1].
Find a target in a sorted ascending array, or return -1. Example: [1,3,8], 8 → 2.
Given daily prices, return the largest profit from one buy followed by one sell. Return 0 if no profit is possible. Example: [7,1,5,3,6,4] → 5.
Given a nonempty integer array, return the maximum sum of any nonempty contiguous subarray. Example: [-2,1,-3,4,-1,2,1,-5,4] → 6.
Given n distinct integers from 0 through n, return the missing value. Example: [3,0,1] → 2.
A nonempty array has an element appearing more than half the time. Return that element. Example: [2,2,1,2] → 2.
Move zeroes to the end of an array in place while retaining the order of nonzero values. Return the array. Example: [0,1,0,3] → [1,3,0,0].
Given a sorted array, overwrite its prefix with distinct values and return the prefix length. Example: [1,1,2] → 2, with prefix [1,2].
Given nonnegative heights, choose two lines maximizing width times the smaller height. Example: [1,8,6,2,5,4,8,3,7] → 49.
Return squares of a nondecreasing integer array in nondecreasing order. Example: [-4,-1,0,3,10] → [0,1,9,16,100].
Versions 1 through n are ordered: once a version is bad all later ones are bad. Given is_bad(v) and at least one bad version, return the first bad version.
Given a sorted array of distinct integers and a target, return its index or the index where it should be inserted. Example: [1,3,5,6], 2 → 1.
An ascending array of distinct integers was rotated. Return the index of target or -1. Example: [4,5,6,7,0,1,2], 0 → 4.
Return the minimum in a nonempty rotated ascending array of distinct values. Example: [3,4,5,1,2] → 1.
For each day's temperature return how many days until a warmer day, or 0. Example: [73,74,75,71,69,72,76,73] → [1,1,4,2,1,1,0,0].
Evaluate valid postfix tokens containing integers and +, -, *, /. Integer division truncates toward zero. Example: [2,1,+,3,*] → 9.
Given integers and 1 ≤ k ≤ n, return the largest sum of any contiguous subarray of length k. Example: [2,1,5,1,3,2], 3 → 9.
Given positive integers and a positive target, return the shortest length of a contiguous subarray with sum at least target, or 0 if none. Example: 7, [2,3,1,2,4,3] → 2.
Return the maximum number of edges on a path between any two nodes in a binary tree. Nodes have left and right attributes.
Return whether two binary trees have the same structure and node values. Nodes expose val, left and right.
Return a list of levels from top to bottom, each containing node values from left to right. Empty tree returns [].
Return whether every node of a binary tree satisfies strict BST ordering. Duplicate values are not allowed.
Return true if any integer appears twice. Example: [1,2,3,1] → true.
For lowercase English strings, return whether both contain the same letters with the same multiplicities. Example: anagram, nagaram → true.
Given a string containing only ()[]{} characters, determine if brackets are properly nested. Example: ()[]{} → true; ([)] → false.
Given prices by day, return the best profit from one buy followed by one sale, or 0. Example: [7,1,5,3,6,4] → 5.
Given a nonempty integer array, return the largest sum of a nonempty contiguous subarray. Example: [-2,1,-3,4,-1,2,1] → 6.
Move all zeros to the end in place while preserving nonzero order. Return the array. Example: [0,1,0,3] → [1,3,0,0].
Overwrite a sorted array prefix with its distinct values and return the prefix length. Example: [1,1,2] → 2 with prefix [1,2].
Return the index of a target in a sorted distinct array, or the position where it belongs. Example: [1,3,5,6], 2 → 1.
Return the length of the longest substring without repeating characters. Input consists of ASCII characters. Example: abba → 2.
Given integers and 1 ≤ k ≤ n, return the maximum sum of a contiguous window of length k. Example: [2,1,5,1,3,2], 3 → 9.
Return products of all values except each index, without division. Example: [1,2,3,4] → [24,12,8,6].
Given n distinct integers in the range 0 through n, return the missing integer. Example: [3,0,1] → 2.
Return zero-based indices of two distinct values summing to target in a sorted array, or null. Example: [1,2,4,8], 6 → [1,2].
Given nonnegative heights, find the largest area between two lines. Example: [1,8,6,2,5,4,8,3,7] → 49.
Versions 1..n are ordered and at least one is bad; all versions after a bad one are bad. Given isBad, return the first bad version.
Reverse a singly linked list and return its head. Example: 1 → 2 → 3 becomes 3 → 2 → 1.
Return the maximum number of nodes in a root-to-leaf path. An empty tree has depth 0.
Swap the left and right children at every node and return the root.
Given heads of two sorted linked lists, merge them into one sorted list using their existing nodes and return its head. Nodes have val and next.
Return the middle node of a singly linked list; if there are two middles, return the second. Example: 1 → 2 → 3 → 4 returns node 3.
Remove the nth node from the end of a singly linked list and return the head. Assume 1 ≤ n ≤ list length.
Return the shared node where two acyclic singly linked lists intersect, or None. Intersection means identical nodes, not equal values.
Return whether each node's left and right subtree heights differ by at most one.
Given a BST and two existing nodes p and q, return their lowest common ancestor. Values are distinct.
Return a binary tree's values in left, root, right order without recursion. An empty tree returns [].
Given a BST with unique values and valid 1-indexed k, return the kth smallest node value.
Given a nonempty binary tree, return the maximum sum of a path between any two nodes. A path cannot reuse a node.
A node is good if no earlier node on its root path has a larger value. Return the count; an empty tree has zero.
Invert a binary tree in place without recursion, swapping each node's children. Return the root.
Group lowercase English words that are anagrams. Return groups in any order. Example: [eat,tea,tan,ate] groups eat/tea/ate together.
Given integers, return the length of the longest sequence of consecutive values, regardless of original order. Example: [100,4,200,1,3,2] → 4.
Given integers and valid k, return any k most frequent distinct values. Ties may be returned in any order.
Return unique triplets of values summing to zero. Example: [-1,0,1,2,-1,-4] → [[-1,-1,2],[-1,0,1]].
Given uppercase letters and k replacements, return the longest substring that can be made of one repeated letter. Example: AABABBA, 1 → 4.
Given strings s and nonempty p, return start indices of substrings of s that are anagrams of p. Example: cbaebabacd, abc → [0,6].
Return any index of a peak in a nonempty array whose adjacent values differ. A peak is larger than its existing neighbors.
Given a sorted array, return the first and last indices of target, or [-1,-1]. Example: [5,7,7,8,8,10], 8 → [3,4].
Implement FIFO push, pop and peek using two stacks. Assume pop and peek are called only when the queue is nonempty.
Design a stack supporting push, pop, top and get_min in constant time. Assume pop, top and get_min are called only when nonempty.
Return whether a string can be a palindrome after deleting at most one character. Example: abca → true.
Reverse a list of characters in place. Example: [h,e,l,l,o] becomes [o,l,l,e,h].
Return whether string s can be formed by deleting zero or more characters from t without reordering. Example: abc, ahbgdc → true.
Return the distinct values present in both arrays in any order. Example: [1,2,2,1], [2,2] → [2].
Return the index of the first character occurring exactly once, or -1. Example: loveleetcode → 2.
Return whether the characters in note can be formed from magazine, using each magazine character at most once. Example: aa, aab → true.
Return whether two strings can be mapped character-by-character with a one-to-one mapping. Example: egg, add → true; foo, bar → false.
Given a character pattern and space-separated words, determine if there is a bijection. Example: abba and 'dog cat cat dog' → true.
Given a 9×9 board containing digits 1–9 and dots, check that each row, column and 3×3 box contains no repeated digit. Completion is not required.
An array of length n+1 contains integers 1..n with one duplicated value. Return the duplicate without modifying the array.
Return the leftmost index whose left-side sum equals its right-side sum, or -1. Example: [1,7,3,6,5,6] → 3.
Build a structure supporting inclusive sum_range(left, right) on an immutable integer array. Assume valid indices.
Return the number of contiguous subarrays whose sum equals k. Example: [1,1,1], 2 → 2.
Return an array whose ith element is the sum of input elements through i. Example: [1,2,3,4] → [1,3,6,10].
Given two sorted integer arrays, return their smallest common value, or -1 if none. Example: [1,2,3], [2,4] → 2.
Merge two sorted integer arrays and return a new sorted array. Example: [1,3], [2,4] → [1,2,3,4].
Remove all occurrences of val in place and return the length of the retained prefix; order may be preserved. Example: [3,2,2,3], 3 → 2.
A rectangular matrix has sorted rows and each row starts after the previous row ends. Return whether target occurs; an empty matrix returns false.
Given a nonnegative integer x, return floor(sqrt(x)) without floating-point arithmetic. Example: 8 → 2.
Sort an array containing only 0, 1 and 2 in place. Example: [2,0,2,1,1,0] → [0,0,1,1,2,2].
Given nonnegative heights, return the units of water trapped after rain. Example: [0,1,0,2] → 1.
Treat # as a backspace and compare the resulting strings. Example: ab#c and ad#c → true. Backspace on an empty string does nothing.
Repeatedly remove pairs of equal adjacent characters and return the remaining string. Example: abbaca → ca.
For each value in a list of distinct integers, return the next larger value to its right or -1. Example: [2,1,3] → [3,3,-1].
Given a string containing (, ) and *, return whether it can be valid if each * represents (, ) or empty. Example: (*) → true.
Given positive pile sizes and h hours, return the smallest integer speed that finishes all piles when each pile takes whole hours. Assume h is at least the pile count.
Given positive package weights in fixed order and a number of days, return the smallest ship capacity that ships all packages within those days.
Given sorted letters and a target, return the smallest letter strictly greater than target, wrapping to the first if none exists. The list is nonempty.
Return whether target appears in a rotated sorted array that may contain duplicates. Example: [2,5,6,0,0,1,2], 0 → true.
Return whether any substring of s2 is a permutation of nonempty s1. Example: ab, eidbaooo → true.
Given a binary array and nonnegative k, return the longest contiguous subarray with at most k zeros. Example: [1,0,1,1,0], 1 → 4.
Return the longest substring with at most k distinct characters; k is nonnegative. Example: eceba, 2 → 3.
Remove duplicate values from a sorted linked list in place, keeping one of each, and return its head.
Return whether a singly linked list is a palindrome. The list may be empty.
Reorder a linked list so nodes at odd positions precede nodes at even positions, preserving relative order within each group. Positions are 1-based.
Swap each adjacent pair of nodes in a linked list and return the head. Do not swap node values.
Return node values in root, left, right order without recursion. An empty tree returns [].
Return the value visible from the right side at each level of a binary tree. Empty tree returns [].
Return the number of nodes on the shortest root-to-leaf path, or zero for an empty tree.
Return whether a root-to-leaf path has values summing to target. An empty tree returns false.
Return whether a binary tree is a mirror of itself around its center. An empty tree is symmetric.
Return whether sub is structurally identical to a subtree of root. Treat an empty sub as a subtree of any tree.
Return the number of nodes in a binary tree. Empty tree returns zero.
Return the node with a target value in a BST, or None if absent. Values are distinct.
Given a binary tree and two distinct nodes guaranteed to exist, return their lowest common ancestor by node identity.
Return the arithmetic mean of values at each level of a binary tree. Empty tree returns [].