Technical round · Problems

Coding problems

Practise answering first. Each answer takes two clicks to reveal, so you can test your recall without spoilers.

Practice by pattern

Start with arrays, then work through these patterns toward trees. Select a topic to filter the problems.

Showing 120 of 120 questions

Arrays & hashing · Python · #problem-1

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).

Arrays & hashing · Python · #problem-2

Contains Duplicate

Return whether an array contains a repeated value. Example: [1,2,3,1] → true.

Arrays & hashing · Python · #problem-3

Valid Anagram

Determine whether two strings contain the same characters with the same counts. Example: listen, silent → true.

Arrays & hashing · Python · #problem-4

Product Except Self

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].

Two pointers · Python · #problem-5

Valid Palindrome

Check whether a string is a palindrome considering only alphanumeric characters and ignoring case. Example: A man, a plan, a canal: Panama → true.

Two pointers · Python · #problem-6

Two Sum in Sorted Array

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).

Stack · Python · #problem-7

Valid Parentheses

Check whether a string of parentheses, brackets and braces is balanced and properly nested. Example: ([]) → true; ([)] → false.

Binary search · Python · #problem-8

Binary Search

Find target in an ascending sorted array and return its index, or -1. Example: [1,3,8], 8 → 2.

Sliding window · Python · #problem-9

Longest Substring Without Repeats

Return the length of the longest contiguous substring containing no repeated character. Example: abba → 2.

Linked list · Python · #problem-10

Reverse Linked List

Reverse a singly linked list and return the new head. Each node has a next attribute. Example: 1 → 2 → 3 becomes 3 → 2 → 1.

Linked list · Python · #problem-11

Linked List Cycle

Determine whether a singly linked list has a cycle. Example: a node whose next link points to itself has a cycle.

Trees · Python · #problem-12

Maximum Depth of Binary Tree

Return the maximum number of nodes on a root-to-leaf path. Nodes expose left and right. A single-node tree has depth 1.

Trees · Python · #problem-13

Invert Binary Tree

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.

Arrays & hashing · TypeScript · #problem-14

Two Sum

Return the indices of two distinct numbers summing to target, or null. Example: [2,7,11,15], 9 → [0,1].

Binary search · TypeScript · #problem-15

Binary Search

Find a target in a sorted ascending array, or return -1. Example: [1,3,8], 8 → 2.

Arrays & hashing · Python · #problem-16

Best Time to Buy and Sell Stock

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.

Arrays & hashing · Python · #problem-17

Maximum Subarray

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.

Arrays & hashing · Python · #problem-18

Missing Number

Given n distinct integers from 0 through n, return the missing value. Example: [3,0,1] → 2.

Arrays & hashing · Python · #problem-19

Majority Element

A nonempty array has an element appearing more than half the time. Return that element. Example: [2,2,1,2] → 2.

Two pointers · Python · #problem-20

Move Zeroes

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].

Two pointers · Python · #problem-21

Remove Duplicates from Sorted Array

Given a sorted array, overwrite its prefix with distinct values and return the prefix length. Example: [1,1,2] → 2, with prefix [1,2].

Two pointers · Python · #problem-22

Container With Most Water

Given nonnegative heights, choose two lines maximizing width times the smaller height. Example: [1,8,6,2,5,4,8,3,7] → 49.

Two pointers · Python · #problem-23

Squares of a Sorted Array

Return squares of a nondecreasing integer array in nondecreasing order. Example: [-4,-1,0,3,10] → [0,1,9,16,100].

Binary search · Python · #problem-24

First Bad Version

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.

Binary search · Python · #problem-25

Search Insert Position

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.

Binary search · Python · #problem-26

Search a Rotated Sorted Array

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.

Binary search · Python · #problem-27

Find Minimum in Rotated Array

Return the minimum in a nonempty rotated ascending array of distinct values. Example: [3,4,5,1,2] → 1.

Stack · Python · #problem-28

Daily Temperatures

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].

Stack · Python · #problem-29

Evaluate Reverse Polish Notation

Evaluate valid postfix tokens containing integers and +, -, *, /. Integer division truncates toward zero. Example: [2,1,+,3,*] → 9.

Sliding window · Python · #problem-30

Maximum Sum Subarray of Size K

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.

Sliding window · Python · #problem-31

Minimum Size Subarray Sum

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.

Trees · Python · #problem-32

Diameter of Binary Tree

Return the maximum number of edges on a path between any two nodes in a binary tree. Nodes have left and right attributes.

Trees · Python · #problem-33

Same Binary Tree

Return whether two binary trees have the same structure and node values. Nodes expose val, left and right.

Trees · Python · #problem-34

Binary Tree Level Order

Return a list of levels from top to bottom, each containing node values from left to right. Empty tree returns [].

Trees · Python · #problem-35

Validate Binary Search Tree

Return whether every node of a binary tree satisfies strict BST ordering. Duplicate values are not allowed.

Arrays & hashing · TypeScript · #problem-36

Contains Duplicate

Return true if any integer appears twice. Example: [1,2,3,1] → true.

Arrays & hashing · TypeScript · #problem-37

Valid Anagram

For lowercase English strings, return whether both contain the same letters with the same multiplicities. Example: anagram, nagaram → true.

Stack · TypeScript · #problem-38

Valid Parentheses

Given a string containing only ()[]{} characters, determine if brackets are properly nested. Example: ()[]{} → true; ([)] → false.

Arrays & hashing · TypeScript · #problem-39

Best Time to Buy and Sell Stock

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.

Arrays & hashing · TypeScript · #problem-40

Maximum Subarray

Given a nonempty integer array, return the largest sum of a nonempty contiguous subarray. Example: [-2,1,-3,4,-1,2,1] → 6.

Two pointers · TypeScript · #problem-41

Move Zeroes

Move all zeros to the end in place while preserving nonzero order. Return the array. Example: [0,1,0,3] → [1,3,0,0].

Two pointers · TypeScript · #problem-42

Remove Duplicates from Sorted Array

Overwrite a sorted array prefix with its distinct values and return the prefix length. Example: [1,1,2] → 2 with prefix [1,2].

Binary search · TypeScript · #problem-43

Search Insert Position

Return the index of a target in a sorted distinct array, or the position where it belongs. Example: [1,3,5,6], 2 → 1.

Sliding window · TypeScript · #problem-44

Longest Substring Without Repeats

Return the length of the longest substring without repeating characters. Input consists of ASCII characters. Example: abba → 2.

Sliding window · TypeScript · #problem-45

Maximum Sum Window

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.

Arrays & hashing · TypeScript · #problem-46

Product Except Self

Return products of all values except each index, without division. Example: [1,2,3,4] → [24,12,8,6].

Arrays & hashing · TypeScript · #problem-47

Missing Number

Given n distinct integers in the range 0 through n, return the missing integer. Example: [3,0,1] → 2.

Two pointers · TypeScript · #problem-48

Two Sum in Sorted Array

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].

Two pointers · TypeScript · #problem-49

Container With Most Water

Given nonnegative heights, find the largest area between two lines. Example: [1,8,6,2,5,4,8,3,7] → 49.

Binary search · TypeScript · #problem-50

First Bad Version

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.

Linked list · TypeScript · #problem-51

Reverse a Linked List

Reverse a singly linked list and return its head. Example: 1 → 2 → 3 becomes 3 → 2 → 1.

Trees · TypeScript · #problem-52

Maximum Depth of Binary Tree

Return the maximum number of nodes in a root-to-leaf path. An empty tree has depth 0.

Trees · TypeScript · #problem-53

Invert Binary Tree

Swap the left and right children at every node and return the root.

Linked list · Python · #problem-54

Merge Two Sorted Lists

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.

Linked list · Python · #problem-55

Middle of Linked List

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.

Linked list · Python · #problem-56

Remove Nth Node From End

Remove the nth node from the end of a singly linked list and return the head. Assume 1 ≤ n ≤ list length.

Linked list · Python · #problem-57

Intersection of Two Linked Lists

Return the shared node where two acyclic singly linked lists intersect, or None. Intersection means identical nodes, not equal values.

Trees · Python · #problem-58

Balanced Binary Tree

Return whether each node's left and right subtree heights differ by at most one.

Trees · Python · #problem-59

Lowest Common Ancestor in BST

Given a BST and two existing nodes p and q, return their lowest common ancestor. Values are distinct.

Trees · Python · #problem-60

Binary Tree Inorder Traversal

Return a binary tree's values in left, root, right order without recursion. An empty tree returns [].

Trees · Python · #problem-61

Kth Smallest Element in a BST

Given a BST with unique values and valid 1-indexed k, return the kth smallest node value.

Trees · Python · #problem-62

Maximum Tree Path Sum

Given a nonempty binary tree, return the maximum sum of a path between any two nodes. A path cannot reuse a node.

Trees · Python · #problem-63

Count Good Nodes in Binary Tree

A node is good if no earlier node on its root path has a larger value. Return the count; an empty tree has zero.

Trees · Python · #problem-64

Invert Binary Tree Iteratively

Invert a binary tree in place without recursion, swapping each node's children. Return the root.

Arrays & hashing · Python · #problem-65

Group Anagrams

Group lowercase English words that are anagrams. Return groups in any order. Example: [eat,tea,tan,ate] groups eat/tea/ate together.

Arrays & hashing · Python · #problem-66

Longest Consecutive Sequence

Given integers, return the length of the longest sequence of consecutive values, regardless of original order. Example: [100,4,200,1,3,2] → 4.

Arrays & hashing · Python · #problem-67

Top K Frequent Elements

Given integers and valid k, return any k most frequent distinct values. Ties may be returned in any order.

Two pointers · Python · #problem-68

Three Sum

Return unique triplets of values summing to zero. Example: [-1,0,1,2,-1,-4] → [[-1,-1,2],[-1,0,1]].

Sliding window · Python · #problem-69

Longest Repeating Character Replacement

Given uppercase letters and k replacements, return the longest substring that can be made of one repeated letter. Example: AABABBA, 1 → 4.

Sliding window · Python · #problem-70

Find All Anagrams in a String

Given strings s and nonempty p, return start indices of substrings of s that are anagrams of p. Example: cbaebabacd, abc → [0,6].

Binary search · Python · #problem-71

Find Peak Element

Return any index of a peak in a nonempty array whose adjacent values differ. A peak is larger than its existing neighbors.

Binary search · Python · #problem-72

Find First and Last Position

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].

Stack · Python · #problem-73

Implement Queue Using Stacks

Implement FIFO push, pop and peek using two stacks. Assume pop and peek are called only when the queue is nonempty.

Stack · Python · #problem-74

Min Stack

Design a stack supporting push, pop, top and get_min in constant time. Assume pop, top and get_min are called only when nonempty.

Two pointers · Python · #problem-75

Valid Palindrome After One Deletion

Return whether a string can be a palindrome after deleting at most one character. Example: abca → true.

Two pointers · Python · #problem-76

Reverse String

Reverse a list of characters in place. Example: [h,e,l,l,o] becomes [o,l,l,e,h].

Two pointers · Python · #problem-77

Is Subsequence

Return whether string s can be formed by deleting zero or more characters from t without reordering. Example: abc, ahbgdc → true.

Arrays & hashing · Python · #problem-78

Intersection of Two Arrays

Return the distinct values present in both arrays in any order. Example: [1,2,2,1], [2,2] → [2].

Arrays & hashing · Python · #problem-79

First Unique Character

Return the index of the first character occurring exactly once, or -1. Example: loveleetcode → 2.

Arrays & hashing · Python · #problem-80

Ransom Note

Return whether the characters in note can be formed from magazine, using each magazine character at most once. Example: aa, aab → true.

Arrays & hashing · Python · #problem-81

Isomorphic Strings

Return whether two strings can be mapped character-by-character with a one-to-one mapping. Example: egg, add → true; foo, bar → false.

Arrays & hashing · Python · #problem-82

Word Pattern

Given a character pattern and space-separated words, determine if there is a bijection. Example: abba and 'dog cat cat dog' → true.

Arrays & hashing · Python · #problem-83

Valid Sudoku

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.

Two pointers · Python · #problem-84

Find Duplicate Number

An array of length n+1 contains integers 1..n with one duplicated value. Return the duplicate without modifying the array.

Arrays & hashing · Python · #problem-85

Find Pivot Index

Return the leftmost index whose left-side sum equals its right-side sum, or -1. Example: [1,7,3,6,5,6] → 3.

Arrays & hashing · Python · #problem-86

Range Sum Query

Build a structure supporting inclusive sum_range(left, right) on an immutable integer array. Assume valid indices.

Arrays & hashing · Python · #problem-87

Subarray Sum Equals K

Return the number of contiguous subarrays whose sum equals k. Example: [1,1,1], 2 → 2.

Arrays & hashing · Python · #problem-88

Running Sum of Array

Return an array whose ith element is the sum of input elements through i. Example: [1,2,3,4] → [1,3,6,10].

Two pointers · Python · #problem-89

Find Minimum Common Value

Given two sorted integer arrays, return their smallest common value, or -1 if none. Example: [1,2,3], [2,4] → 2.

Two pointers · Python · #problem-90

Merge Sorted Arrays

Merge two sorted integer arrays and return a new sorted array. Example: [1,3], [2,4] → [1,2,3,4].

Two pointers · Python · #problem-91

Remove Element

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.

Binary search · Python · #problem-92

Search a 2D Matrix

A rectangular matrix has sorted rows and each row starts after the previous row ends. Return whether target occurs; an empty matrix returns false.

Binary search · Python · #problem-93

Integer Square Root

Given a nonnegative integer x, return floor(sqrt(x)) without floating-point arithmetic. Example: 8 → 2.

Two pointers · Python · #problem-94

Sort Colors

Sort an array containing only 0, 1 and 2 in place. Example: [2,0,2,1,1,0] → [0,0,1,1,2,2].

Two pointers · Python · #problem-95

Trapping Rain Water

Given nonnegative heights, return the units of water trapped after rain. Example: [0,1,0,2] → 1.

Stack · Python · #problem-96

Backspace String Compare

Treat # as a backspace and compare the resulting strings. Example: ab#c and ad#c → true. Backspace on an empty string does nothing.

Stack · Python · #problem-97

Remove Adjacent Duplicates

Repeatedly remove pairs of equal adjacent characters and return the remaining string. Example: abbaca → ca.

Stack · Python · #problem-98

Next Greater Element

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].

Stack · Python · #problem-99

Valid Parenthesis String

Given a string containing (, ) and *, return whether it can be valid if each * represents (, ) or empty. Example: (*) → true.

Binary search · Python · #problem-100

Koko Eating Bananas

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.

Binary search · Python · #problem-101

Capacity to Ship Packages

Given positive package weights in fixed order and a number of days, return the smallest ship capacity that ships all packages within those days.

Binary search · Python · #problem-102

Find Smallest Letter Greater Than Target

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.

Binary search · Python · #problem-103

Search in Rotated Array with Duplicates

Return whether target appears in a rotated sorted array that may contain duplicates. Example: [2,5,6,0,0,1,2], 0 → true.

Sliding window · Python · #problem-104

Permutation in String

Return whether any substring of s2 is a permutation of nonempty s1. Example: ab, eidbaooo → true.

Sliding window · Python · #problem-105

Longest Subarray with At Most K Zeroes

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.

Sliding window · Python · #problem-106

Longest Substring with At Most K Distinct Characters

Return the longest substring with at most k distinct characters; k is nonnegative. Example: eceba, 2 → 3.

Linked list · Python · #problem-107

Delete Duplicates from Sorted List

Remove duplicate values from a sorted linked list in place, keeping one of each, and return its head.

Linked list · Python · #problem-108

Palindrome Linked List

Return whether a singly linked list is a palindrome. The list may be empty.

Linked list · Python · #problem-109

Odd Even Linked List

Reorder a linked list so nodes at odd positions precede nodes at even positions, preserving relative order within each group. Positions are 1-based.

Linked list · Python · #problem-110

Swap Nodes in Pairs

Swap each adjacent pair of nodes in a linked list and return the head. Do not swap node values.

Trees · Python · #problem-111

Binary Tree Preorder Traversal

Return node values in root, left, right order without recursion. An empty tree returns [].

Trees · Python · #problem-112

Binary Tree Right Side View

Return the value visible from the right side at each level of a binary tree. Empty tree returns [].

Trees · Python · #problem-113

Minimum Depth of Binary Tree

Return the number of nodes on the shortest root-to-leaf path, or zero for an empty tree.

Trees · Python · #problem-114

Path Sum

Return whether a root-to-leaf path has values summing to target. An empty tree returns false.

Trees · Python · #problem-115

Symmetric Tree

Return whether a binary tree is a mirror of itself around its center. An empty tree is symmetric.

Trees · Python · #problem-116

Subtree of Another Tree

Return whether sub is structurally identical to a subtree of root. Treat an empty sub as a subtree of any tree.

Trees · Python · #problem-117

Count Nodes in Binary Tree

Return the number of nodes in a binary tree. Empty tree returns zero.

Trees · Python · #problem-118

Search in a Binary Search Tree

Return the node with a target value in a BST, or None if absent. Values are distinct.

Trees · Python · #problem-119

Lowest Common Ancestor in Binary Tree

Given a binary tree and two distinct nodes guaranteed to exist, return their lowest common ancestor by node identity.

Trees · Python · #problem-120

Binary Tree Level Averages

Return the arithmetic mean of values at each level of a binary tree. Empty tree returns [].