Easy 52 exercises

1

Given an array a, reverse it in-place.

2

Given array a and integer k, return the left-rotated array.

4

Given sorted array a and target t, return indices of the pair that sums to t.

5

Given string s, return True if it's a palindrome.

6

Given sorted arrays a and b, return a single merged sorted array.

7

Given array a and window size k, find the maximum sum of any contiguous k elements.

8

Given array a, answer multiple range-sum queries [l, r] efficiently.

9

Insert value val at position k (0-indexed) in a linked list.

10

Delete the node at position k (0-indexed) in a linked list.

11

Reverse a singly linked list in-place.

14

Find the middle node of a linked list.

15

Merge two sorted linked lists into one sorted list.

19

Evaluate a postfix (reverse Polish notation) expression.

21

Check if a string of parentheses () is valid.

22

Reverse a string using a stack.

23

Given unsorted array a and target t, return indices of the pair that sums to t.

24

Find the index of the first character that appears only once.

26

Given prices and a budget, find two items that sum to exactly the budget.

27

Count unique pairs where |a-b| = k.

29

Sort an array using insertion sort.

32

Merge two sorted arrays into one sorted array.

34

Insert a value into a binary search tree.

35

Search for a value in a BST. Return the node or None.

36

Find the height of a binary tree.

37

Return the inorder traversal of a binary tree as a list.

38

Return the preorder traversal of a binary tree as a list.

42

Traverse a graph depth-first using an explicit stack.

43

Traverse a graph depth-first using recursion.

45

Count the number of connected components in an undirected graph.

47

Compute the nth Fibonacci number efficiently.

48

Compute n! recursively.

51

Given a sorted array and a target value, return the index of the target or -1 if not found.

52

Given a sorted array and a target, return the index where the target would be inserted.

54

Compute floor(sqrt(x)) without using math.sqrt.

57

Demonstrate pushing and popping elements from a min-heap.

58

Convert an arbitrary array into a min-heap in-place.

63

You can climb 1 or 2 steps. How many distinct ways to climb n stairs?

71

Find maximum profit from one buy and one sell (buy before sell).

83

Count the number of 1-bits in an integer.

84

Check if n is a power of two.

85

Every element appears twice except one. Find it.

86

Reverse the bits of a 32-bit unsigned integer.

87

Count positions where corresponding bits of two integers differ.

90

Find the diameter (longest path between any two nodes).

92

Is there a root-to-leaf path that sums to a target?

94

Check if a binary tree is its own mirror image.

100

Change all connected cells of the same color starting from (sr, sc).

101

Given an array of stock prices by day, find the maximum profit from one buy-sell pair.

114

Given an array of meeting time intervals, determine if a person can attend all meetings.

120

Determine if a number is 'happy': repeatedly sum the squares of its digits; if it reaches 1 it's happy.

126

Given a sorted array, remove duplicates in-place and return the new length.

139

Find the diameter (longest path between any two nodes) of a binary tree.

Medium 93 exercises

3

Given array a, find the contiguous subarray with the largest sum.

12

Detect if a linked list has a cycle using O(1) space.

13

Find the node where the cycle begins in a linked list.

16

Check if a string of brackets is balanced: ()[]{}

17

Implement a FIFO queue using only two stacks.

18

Design a stack that supports push, pop, and getMin in O(1).

20

For each element, find the next element to its right that is larger.

25

Group a list of words by anagram.

28

Count subarrays whose elements sum to exactly k.

30

Sort an array using merge sort.

31

Sort an array using quicksort with Lomuto partition.

33

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

39

Return the level-order traversal of a binary tree as list of lists.

40

Check if a binary tree is a valid binary search tree.

41

Find shortest distances from a start node in an unweighted graph.

44

Detect if an undirected graph contains a cycle.

46

Find minimum dice rolls to reach square 100 on a snakes-and-ladders board.

49

Compute x^n in O(log n) time.

50

Generate all subsets (power set) of a list of numbers.

53

Given a sorted array with duplicates and a target, find the first and last index of the target.

55

Given an array where no two adjacent elements are equal, find any peak element.

56

Given a rotated sorted array with unique elements, find the minimum element.

59

Given an array of integers and k, return the k most frequent elements.

60

Given an n x n matrix where rows and columns are sorted, find the kth smallest element.

61

Given meeting intervals [start, end], find minimum conference rooms required.

62

Given k sorted arrays, merge them into one sorted array.

64

Max amount you can rob without robbing two adjacent houses.

65

Given coin denominations and a target, find minimum coins needed.

66

Find the length of the longest strictly increasing subsequence.

67

Maximize value in a knapsack of capacity W, each item used at most once.

68

Find path from top-left to bottom-right minimizing sum. Move only right or down.

69

Minimum operations (insert, delete, replace) to convert word1 into word2.

70

Count ways to decode a digit string into letters (A=1, B=2, ..., Z=26).

72

Can you reach the last index? Each element is the max jump length from that position.

73

Find starting station for a complete circuit, or -1 if impossible.

74

Partition string into most parts so each letter appears in at most one part.

75

Minimum intervals to remove so the rest don't overlap.

76

Given digits 2-9, return all possible letter combinations (phone keypad).

77

Return all permutations of a list of distinct integers.

78

Given n and k, return all combinations of k numbers from 1 to n.

79

Find all unique combinations that sum to target. Numbers can be reused.

80

Place 4 queens on 4x4 board with no two threatening each other.

81

Does a word exist in a 2D grid? Move up/down/left/right, no cell reuse.

82

Partition a string so every substring is a palindrome. Return all partitions.

88

Return the bitwise AND of all numbers in range [m, n].

89

Find the LCA of two nodes in a binary tree.

91

Serialize a binary tree to a string and reconstruct it.

93

Return values visible from the right side (rightmost node per level).

95

Shortest path from source in a weighted graph.

96

Return a topological ordering of a DAG.

97

Can all courses be completed given prerequisites? (Cycle detection in DAG.)

98

Count islands in a 2D grid of '1' (land) and '0' (water).

99

Deep copy a graph. Each node has a value and neighbor list.

102

Given a string, find the length of the longest substring without repeating characters.

103

Given string s and integer k, find the longest substring where you can replace at most k characters to make all characters the same.

104

Given strings s1 and s2, return true if s2 contains a permutation of s1 as a substring.

107

Implement a trie with insert, search, and startsWith methods.

108

Design a data structure that supports adding words and searching with '.' wildcards that match any character.

110

Given a list of words, find the longest word that can be built one character at a time by other words in the list.

111

Given a collection of intervals, merge all overlapping intervals.

112

Given a sorted list of non-overlapping intervals and a new interval, insert and merge if necessary.

113

Given intervals, find the minimum number of intervals to remove to make the rest non-overlapping.

115

Given meeting time intervals, find the minimum number of conference rooms required.

117

Rotate an n×n matrix 90 degrees clockwise in-place.

118

Given an m×n matrix, return all elements in spiral order.

119

Given an m×n matrix, if an element is 0, set its entire row and column to 0. Do it in-place.

121

Implement pow(x, n) — compute x raised to the power n.

122

Given two numbers represented as strings, return their product as a string.

123

Given n vertical lines, find two that form a container holding the most water.

125

Given array nums, find all unique triplets that sum to zero.

127

Given an m×n grid, count the number of unique paths from top-left to bottom-right (only move right or down).

128

Given two strings, find the length of their longest common subsequence.

129

Given two words, find the minimum number of operations (insert, delete, replace) to convert one to the other.

130

Given coins and an amount, find the number of WAYS to make the amount (unlimited supply).

131

Given nums and a target, assign + or - to each number to reach target. Count the number of ways.

132

Given strings s1, s2, s3, determine if s3 is formed by interleaving s1 and s2.

133

Given array nums, return an array where each element is the product of all other elements. No division allowed.

134

Evaluate an arithmetic expression in Reverse Polish Notation (postfix).

135

Given daily temperatures, for each day find how many days until a warmer temperature.

136

Generate all combinations of n pairs of well-formed parentheses.

137

Deep copy a linked list where each node has a next pointer and a random pointer to any node.

138

Design a data structure for Least Recently Used cache with O(1) get and put.

140

Given a binary tree, return the values visible from the right side (rightmost node at each level).

141

Count nodes where the node's value is greater than or equal to all values on the path from root.

142

Find the kth largest element in an unsorted array.

143

Given tasks with a cooldown period n between same tasks, find the minimum time to finish all tasks.

144

In a grid, fresh oranges rot when adjacent to rotten ones. Find the minimum minutes for all to rot.

145

Given a board of 'X' and 'O', capture all regions surrounded by 'X' (flip 'O' to 'X'), except those connected to the border.

146

Given n courses and prerequisites, return a valid order to take all courses, or empty if impossible.

147

Given array where each element is max jump length, find minimum jumps to reach the last index.

148

Given gas stations in a circle with gas[i] and cost[i], find the starting station to complete the circuit, or -1.

149

Partition a string so each letter appears in at most one part. Maximize the number of parts.

150

Given a string with '(', ')', and '*' (* can be '(', ')' or empty), determine if it's valid.

Hard 5 exercises

105

Given strings s and t, find the minimum window in s that contains all characters of t.

106

Given array nums and window size k, return the max value in each window position.

109

Given a board of characters and a list of words, find all words that can be formed by adjacent cells.

116

Given intervals and queries (points), for each query find the size of the smallest interval containing it.

124

Given n non-negative integers representing an elevation map, compute how much water it can trap.