Daily Coding Problems by Category
- Math
- Array
- List
- Sort
- String
- Tree
- Trie
- Stack
- Heap / Priority Queue
- Hashmap
- Greedy
- Divide and Conquer
- Graph
- Backtracking
- Dynamic Programming
Math
- [Easy] Pascal’s triangle – Given an input k, return the kth row of Pascal’s triangle. (Try ME)
Question: Pascal’s triangle is a triangular array of integers constructed with the following formula:
- The first row consists of the number 1.
- For each subsequent row, each element is the sum of the numbers directly above it, on either side.
For example, here are the first few rows:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Given an input k, return the kth row of Pascal’s triangle.
Bonus: Can you do this using only O(k) space?
- [Easy] GCD of N Numbers – Given n numbers, find the greatest common denominator between them. (Try ME)
Question: Given n
numbers, find the greatest common denominator between them.
For example, given the numbers [42, 56, 14]
, return 14
.
- [Easy] N-th Perfect Number – A number is considered perfect if its digits sum up to exactly 10. (Try ME)
Question: A number is considered perfect if its digits sum up to exactly 10
.
Given a positive integer n
, return the n-th perfect number.
For example, given 1
, you should return 19
. Given 2
, you should return 28
.
- [Easy] Sevenish Number – Define a “sevenish” number to be one which is either a power of 7, or the sum of unique powers of 7. Find the nth sevenish number. (Try ME)
Question: Let’s define a “sevenish” number to be one which is either a power of 7, or the sum of unique powers of 7. The first few sevenish numbers are 1, 7, 8, 49, and so on. Create an algorithm to find the nth sevenish number.
- [Easy] Goldbach’s conjecture – Given an even number (greater than 2), return two prime numbers whose sum will be equal to the given number. (Try ME)
Question: Given an even number (greater than 2), return two prime numbers whose sum will be equal to the given number.
A solution will always exist. See Goldbach’s conjecture. If there are more than one solution possible, return the lexicographically smaller solution.
Example:
Input: 4
Output: (2, 2)
Explanation: 2 + 2 = 4
- [Hard] Sum of Consecutive Numbers – Given a number n, return the number of lists of consecutive numbers that sum up to n. (Try ME)
Question: Given a number n
, return the number of lists of consecutive numbers that sum up to n
.
For example, for n = 9
, you should return 3
since the lists are: [2, 3, 4]
, [4, 5]
, and [9]
. Can you do it in linear time?
- [Medium] LC 65. Determine if number – Given a string that may represent a number, determine if it is a number. (Try ME)
Question: Given a string that may represent a number, determine if it is a number. Here’s some of examples of how the number may be presented:
"123" # Integer
"12.3" # Floating point
"-123" # Negative numbers
"-.3" # Negative floating point
"1.5e5" # Scientific notation
Here’s some examples of what isn’t a proper number:
"12a" # No letters
"1 2" # No space between numbers
"1e1.2" # Exponent can only be an integer (positive or negative or 0)
Scientific notation requires the first number to be less than 10, however to simplify the solution assume the first number can be greater than 10. Do not parse the string with int() or any other python functions.
- [Medium] Integer Exponentiation – Implement integer exponentiation. That is, implement the
pow(x, y)
function, where x
and y
are integers and returns x^y
. (Try ME)
pow(x, y)
function, where x
and y
are integers and returns x^y
. (Try ME)Question: Implement integer exponentiation. That is, implement the pow(x, y)
function, where x
and y
are integers and returns x^y
.
Do this faster than the naive method of repeated multiplication.
For example, pow(2, 10)
should return 1024
.
- [Easy] Rand7 – Given a function rand5(), use that function to implement a function rand7() (Try ME)
Question: Given a function rand5()
, use that function to implement a function rand7()
where rand5()
returns an integer from 1
to 5
(inclusive) with uniform probability and rand7()
is from 1
to 7
(inclusive). Also, use of any other library function and floating point arithmetic are not allowed.
- [Easy] Count Line Segment Intersections – Suppose you are given two lists of n points, write an algorithm to determine how many pairs of the line segments intersect. (Try ME)
Question: Suppose you are given two lists of n points, one list p1, p2, ..., pn
on the line y = 0
and the other list q1, q2, ..., qn
on the line y = 1
.
Imagine a set of n line segments connecting each point pi to qi.
Write an algorithm to determine how many pairs of the line segments intersect.
- [Medium] Ways to Form Heap with Distinct Integers – Write a program to determine how many distinct ways there are to create a max heap from a list of N given integers. (Try ME)
Question: Write a program to determine how many distinct ways there are to create a max heap from a list of N
given integers.
Example:
If N = 3, and our integers are [1, 2, 3], there are two ways, shown below.
3 3
/ \ / \
1 2 2 1
- [Easy] LC 405. Convert a Number to Hexadecimal – Given an integer, write an algorithm to convert it to hexadecimal. (Try ME)
Question: Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s complement method is used.
Example 1:
Input: 26
Output: "1a"
Example 2:
Input: -1
Output: "ffffffff"
- [Medium] Multiply Large Numbers Represented as Strings – Given two strings which represent non-negative integers, multiply the two numbers and return the product as a string as well. (Try ME)
Question: Given two strings which represent non-negative integers, multiply the two numbers and return the product as a string as well. You should assume that the numbers may be sufficiently large such that the built-in integer type will not be able to store the input.
Example 1:
Input: num1 = "2", num2 = "3"
Output: "6"
Example 2:
Input: num1 = "123", num2 = "456"
Output: "56088"
- [Medium] Count Occurrence in Multiplication Table – Given integers N and X, write a function that returns the number of times X appears as a value in an N by N multiplication table. (Try ME)
Question: Suppose you have a multiplication table that is N
by N
. That is, a 2D array where the value at the i-th
row and j-th
column is (i + 1) * (j + 1)
(if 0-indexed) or i * j
(if 1-indexed).
Given integers N
and X
, write a function that returns the number of times X
appears as a value in an N
by N
multiplication table.
For example, given N = 6
and X = 12
, you should return 4
, since the multiplication table looks like this:
| 1 | 2 | 3 | 4 | 5 | 6 |
| 2 | 4 | 6 | 8 | 10 | 12 |
| 3 | 6 | 9 | 12 | 15 | 18 |
| 4 | 8 | 12 | 16 | 20 | 24 |
| 5 | 10 | 15 | 20 | 25 | 30 |
| 6 | 12 | 18 | 24 | 30 | 36 |
And there are 4
12’s in the table.
- [Easy] Palindrome Integers – Write a program that checks whether an integer is a palindrome. (Try ME)
Question: Write a program that checks whether an integer is a palindrome. For example, 121
is a palindrome, as well as 888
. But neither 678
nor 80
is a palindrome. Do not convert the integer into a string.
- [Medium] Regular Numbers – A regular number in mathematics is defined as one which evenly divides some power of 60. Returns, in order, the first N regular numbers. (Try ME)
Question: A regular number in mathematics is defined as one which evenly divides some power of 60
. Equivalently, we can say that a regular number is one whose only prime divisors are 2
, 3
, and 5
.
These numbers have had many applications, from helping ancient Babylonians keep time to tuning instruments according to the diatonic scale.
Given an integer N, write a program that returns, in order, the first N regular numbers.
- [Easy] Fancy Number – Check if a given number is Fancy. A fancy number is one which when rotated 180 degrees is the same. (Try ME)
Question: Check if a given number is Fancy. A fancy number is one which when rotated 180 degrees is the same. Given a number, find whether it is fancy or not.
180 degree rotations of 6, 9, 1, 0 and 8 are 9, 6, 1, 0 and 8 respectively
- [Easy] LC 1344. Angle between Clock Hands – Given a clock time in
hh:mm
format, determine, to the nearest degree, the angle between the hour and the minute hands. (Try ME)
hh:mm
format, determine, to the nearest degree, the angle between the hour and the minute hands. (Try ME)Question: Given a clock time in hh:mm
format, determine, to the nearest degree, the angle between the hour and the minute hands.
Example:
Input: "9:00"
Output: 90
- [Easy] Spreadsheet Columns – Given a number n, find the n-th column name. From the 1st to the 26th column the letters are A to Z. (Try ME)
Question: In many spreadsheet applications, the columns are marked with letters. From the 1st to the 26th column the letters are A to Z. Then starting from the 27th column it uses AA, AB, …, ZZ, AAA, etc.
Given a number n, find the n-th column name.
Examples:
Input Output
26 Z
51 AY
52 AZ
80 CB
676 YZ
702 ZZ
705 AAC
- [Easy] Add Digits – Given a number, add the digits repeatedly until you get a single number. (Try ME)
Question: Given a number, add the digits repeatedly until you get a single number.
Example:
Input: 159
Output: 6
Explanation:
1 + 5 + 9 = 15.
1 + 5 = 6.
So the answer is 6.
- [Hard] LC 273. Integer to English Words – Convert a non-negative integer to its English word representation. (Try ME)
Question: Convert a non-negative integer to its English word representation.
Example 1:
Input: 123
Output: "One Hundred Twenty Three"
Example 2:
Input: 12345
Output: "Twelve Thousand Three Hundred Forty Five"
Example 3:
Input: 1234567
Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"
Example 4:
Input: 1234567891
Output: "One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One"
- [Easy] LC 13. Convert Roman Numerals to Decimal – Given a Roman numeral, find the corresponding decimal value. Inputs will be between 1 and 3999. (Try ME)
Question: Given a Roman numeral, find the corresponding decimal value. Inputs will be between 1 and 3999.
Note: Numbers are strings of these symbols in descending order. In some cases, subtractive notation is used to avoid repeated characters. The rules are as follows:
- I placed before V or X is one less, so 4 = IV (one less than 5), and 9 is IX (one less than 10)
- X placed before L or C indicates ten less, so 40 is XL (10 less than 50) and 90 is XC (10 less than 100).
- C placed before D or M indicates 100 less, so 400 is CD (100 less than 500), and 900 is CM (100 less than 1000).
Example:
Input: IX
Output: 9
Input: VII
Output: 7
Input: MCMIV
Output: 1904
Roman numerals are based on the following symbols:
I 1
IV 4
V 5
IX 9
X 10
XL 40
L 50
XC 90
C 100
CD 400
D 500
CM 900
M 1000
- [Medium] LC 8. String to Integer (atoi) – Given a string, convert it to an integer without using the builtin str function. (Try ME)
Question: Given a string, convert it to an integer without using the builtin str function. You are allowed to use ord to convert a character to ASCII code.
Consider all possible cases of an integer. In the case where the string is not a valid integer, return 0
.
Example:
atoi('-105') # -105
- [Medium] LC 166. Fraction to Recurring Decimal – Given two integers representing the numerator and denominator of a fraction, return the fraction in string format. (Try ME)
Question: Given two integers representing the numerator and denominator of a fraction, return the fraction in string format.
If the fractional part is repeating, enclose the repeating part in parentheses.
Example 1:
Input: numerator = 1, denominator = 2
Output: "0.5"
Example 2:
Input: numerator = 2, denominator = 1
Output: "2"
Example 3:
Input: numerator = 2, denominator = 3
Output: "0.(6)"
- [Easy] Overlapping Rectangles – Find the area of overlapping rectangles. (Try ME)
Question: You’re given 2 over-lapping rectangles on a plane. For each rectangle, you’re given its bottom-left and top-right points. How would you find the area of their overlap?
- [Easy] Exists Overlap Rectangle – Given a list of rectangles represented by min and max x- and y-coordinates. Compute whether or not a pair of rectangles overlap each other. (Try ME)
Question: You are given a list of rectangles represented by min and max x- and y-coordinates. Compute whether or not a pair of rectangles overlap each other. If one rectangle completely covers another, it is considered overlapping.
For example, given the following rectangles:
{
"top_left": (1, 4),
"dimensions": (3, 3) # width, height
},
{
"top_left": (-1, 3),
"dimensions": (2, 1)
},
{
"top_left": (0, 5),
"dimensions": (4, 3)
}
Return true as the first and third rectangle overlap each other.
Puzzle
- [Easy] Single Bit Switch – Given three 32-bit integers x, y, and b, return x if b is 1 and y if b is 0, using only mathematical or bit operations. (Try ME)
Question: Given three 32-bit integers x, y, and b, return x if b is 1 and y if b is 0, using only mathematical or bit operations. You can assume b can only be 1 or 0.
- [Medium] Toss Biased Coin – You do not know the bias of the coin. Write a function to simulate an unbiased coin toss. (Try ME)
Question: Assume you have access to a function toss_biased() which returns 0 or 1 with a probability that’s not 50-50 (but also not 0-100 or 100-0). You do not know the bias of the coin. Write a function to simulate an unbiased coin toss.
- [Easy] Rand25, Rand75 – Generate
0
and 1
with 25%
and 75%
probability. (Try ME)
0
and 1
with 25%
and 75%
probability. (Try ME)Question: Generate 0
and 1
with 25%
and 75%
probability.
Given a function rand50()
that returns 0
or 1
with equal probability, write a function that returns 1
with 75%
probability and 0
with 25%
probability using rand50()
only. Minimize the number of calls to rand50()
method. Also, use of any other library function and floating point arithmetic are not allowed.
- [Hard] Random Elements from Infinite Stream (Reservoir Sampling) – Randomly choosing a sample of k items from a list S containing n items (Try ME)
Question: Randomly choosing a sample of k items from a list S containing n items, where n is either a very large or unknown number. Typically, n is too large to fit the whole list into main memory.
- [Medium] LC 289. Conway’s Game of Life – Conway’s Game of Life takes place on an infinite two-dimensional board of square cells. Implement Conway’s Game of Life. (Try ME)
Question: Conway’s Game of Life takes place on an infinite two-dimensional board of square cells. Each cell is either dead or alive, and at each tick, the following rules apply:
- Any live cell with less than two live neighbours dies.
- Any live cell with two or three live neighbours remains living.
- Any live cell with more than three live neighbours dies.
- Any dead cell with exactly three live neighbours becomes a live cell.
- A cell neighbours another cell if it is horizontally, vertically, or diagonally adjacent.
Implement Conway’s Game of Life. It should be able to be initialized with a starting list of live cell coordinates and the number of steps it should run for. Once initialized, it should print out the board state at each step. Since it’s an infinite board, print out only the relevant coordinates, i.e. from the top-leftmost live cell to bottom-rightmost live cell.
You can represent a live cell with an asterisk (*) and a dead cell with a dot (.).
- [Medium] Add Subtract Currying – Write a function, add_subtract, which alternately adds and subtracts curried arguments. (Try ME)
Question: Write a function, add_subtract, which alternately adds and subtracts curried arguments.
Example:
add_subtract(7) -> 7
add_subtract(1)(2)(3) -> 1 + 2 - 3 -> 0
add_subtract(-5)(10)(3)(9) -> -5 + 10 - 3 + 9 -> 11
- [Hard] Elo Rating System – In chess, the Elo rating system is used to calculate player strengths based on game results. (Try ME)
Question: In chess, the Elo rating system is used to calculate player strengths based on game results.
A simplified description of the Elo system is as follows. Every player begins at the same score. For each subsequent game, the loser transfers some points to the winner, where the amount of points transferred depends on how unlikely the win is. For example, a 1200-ranked player should gain much more points for beating a 2000-ranked player than for beating a 1300-ranked player.
Implement this system.
- [Hard] Array Shuffle – Given an array, write a program to generate a random permutation of array elements. (Try ME)
Question: Given an array, write a program to generate a random permutation of array elements. This question is also asked as “shuffle a deck of cards” or “randomize a given array”. Here shuffle means that every permutation of array element should equally likely.
Input: [1, 2, 3, 4, 5, 6]
Output: [3, 4, 1, 5, 6, 2]
The output can be any random permutation of the input such that all permutation are equally likely.
Hint: Given a function that generates perfectly random numbers between 1 and k (inclusive) where k is an input, write a function that shuffles the input array using only swaps.
- [Easy] Maximum Time – Fill in ? such that the time represented by this string is the maximum possible. (Try ME)
Question: You are given a string that represents time in the format hh:mm
. Some of the digits are blank (represented by ?). Fill in ? such that the time represented by this string is the maximum possible. Maximum time: 23:59
, minimum time: 00:00
. You can assume that input string is always valid.
Example 1:
Input: "?4:5?"
Output: "14:59"
Example 2:
Input: "23:5?"
Output: "23:59"
Example 3:
Input: "2?:22"
Output: "23:22"
Example 4:
Input: "0?:??"
Output: "09:59"
Example 5:
Input: "??:??"
Output: "23:59"
- [Medium] LC 1007. Minimum Domino Rotations For Equal Row –
A[i]
and B[i]
represents dominos. Return the minimum number of rotations so that all the values in A
or B
are the same. (Try ME)
A[i]
and B[i]
represents dominos. Return the minimum number of rotations so that all the values in A
or B
are the same. (Try ME)Question: In a row of dominoes, A[i]
and B[i]
represent the top and bottom halves of the ith domino. (A domino is a tile with two numbers from 1 to 6 - one on each half of the tile.)
We may rotate the ith domino, so that A[i]
and B[i]
swap values.
Return the minimum number of rotations so that all the values in A are the same, or all the values in B are the same.
If it cannot be done, return -1
.
Example 1:
Input: A = [2,1,2,4,2,2], B = [5,2,6,2,3,2]
Output: 2
Explanation:
The first figure represents the dominoes as given by A and B: before we do any rotations.
If we rotate the second and fourth dominoes, we can make every value in the top row equal to 2, as indicated by the second figure.
Example 2:
Input: A = [3,5,1,2,3], B = [3,6,3,3,4]
Output: -1
Explanation: In this case, it is not possible to rotate the dominoes to make one row of values equal.
- [Easy] Quxes Transformation – Given N Quxes standing in a line, determine the smallest number of them remaining after any possible sequence of such transformations. (Try ME)
Question: On a mysterious island there are creatures known as Quxes which come in three colors: red, green, and blue. One power of the Qux is that if two of them are standing next to each other, they can transform into a single creature of the third color.
Given N Quxes standing in a line, determine the smallest number of them remaining after any possible sequence of such transformations.
For example, given the input [‘R’, ‘G’, ‘B’, ‘G’, ‘B’], it is possible to end up with a single Qux through the following steps:
Arrangement | Change |
---|---|
[‘R’, ‘G’, ‘B’, ‘G’, ‘B’] | (R, G) -> B |
[‘B’, ‘B’, ‘G’, ‘B’] | (B, G) -> R |
[‘B’, ‘R’, ‘B’] | (R, B) -> G |
[‘B’, ‘G’] | (B, G) -> R |
[‘R’] |
- [Hard] Anagram to Integer – Given a string formed by concatenating several words corresponding to the integers zero through nine and then anagramming,return the integer (Try ME)
Question: You are given a string formed by concatenating several words corresponding to the integers zero
through nine
and then anagramming.
For example, the input could be 'niesevehrtfeev'
, which is an anagram of 'threefiveseven'
. Note that there can be multiple instances of each integer.
Given this string, return the original integers in sorted order. In the example above, this would be 357
.
- [Easy] LC 766. Toeplitz Matrix – Write a program to determine whether a given input is a Toeplitz matrix whose diagonal elements are the same. (Try ME)
Question: In linear algebra, a Toeplitz matrix is one in which the elements on any given diagonal from top left to bottom right are identical.
Write a program to determine whether a given input is a Toeplitz matrix.
Example of Toeplitz Matrix:
1 2 3 4 8
5 1 2 3 4
4 5 1 2 3
7 4 5 1 2
- [Medium] LC 89. Generate Gray Code – Gray code is a binary code where each successive value differ in only one bit (Try ME)
Question: Gray code is a binary code where each successive value differ in only one bit, as well as when wrapping around. Gray code is common in hardware so that we don’t see temporary spurious values during transitions.
Given a number of bits n, generate a possible gray code for it.
Example:
For n = 2, one gray code would be [00, 01, 11, 10].
- [Hard] Minimum Cost to Construct Pyramid with Stones – Given N stones in a row, you can change the height of any stone by paying a cost of 1 unit. Determine the lowest cost or produce a pyramid. (Try ME)
Question: You have N
stones in a row, and would like to create from them a pyramid. This pyramid should be constructed such that the height of each stone increases by one until reaching the tallest stone, after which the heights decrease by one. In addition, the start and end stones of the pyramid should each be one stone high.
You can change the height of any stone by paying a cost of 1
unit to lower its height by 1
, as many times as necessary. Given this information, determine the lowest cost method to produce this pyramid.
For example, given the stones [1, 1, 3, 3, 2, 1]
, the optimal solution is to pay 2
to create [0, 1, 2, 3, 2, 1]
.
- [Easy] Reconstruct a Jumbled Array – The sequence 0 to N is jumbled, and given its order is an array with each number is larger or smaller than the previous one. Return an array. (Try ME)
Question: The sequence [0, 1, ..., N]
has been jumbled, and the only clue you have for its order is an array representing whether each number is larger or smaller than the last.
Given this information, reconstruct an array that is consistent with it. For example, given [None, +, +, -, +]
, you could return [1, 2, 3, 0, 4]
.
- [Medium] Count Attacking Bishop Pairs – Given N bishops, represented as (row, column) tuples on a M by M chessboard, count the number of pairs of bishops attacking each other. (Try ME)
Question: On our special chessboard, two bishops attack each other if they share the same diagonal. This includes bishops that have another bishop located between them, i.e. bishops can attack through pieces.
You are given N bishops, represented as (row, column) tuples on a M by M chessboard. Write a function to count the number of pairs of bishops that attack each other. The ordering of the pair doesn’t matter: (1, 2)
is considered the same as (2, 1)
.
For example, given M = 5
and the list of bishops:
(0, 0)
(1, 2)
(2, 2)
(4, 0)
The board would look like this:
[b 0 0 0 0]
[0 0 b 0 0]
[0 0 b 0 0]
[0 0 0 0 0]
[b 0 0 0 0]
You should return 2, since bishops 1 and 3 attack each other, as well as bishops 3 and 4.
- [Hard] Find Next Greater Permutation – Given a number represented by a list of digits, find the next greater permutation of a number, in terms of lexicographic ordering. (Try ME)
Question: Given a number represented by a list of digits, find the next greater permutation of a number, in terms of lexicographic ordering. If there is not greater permutation possible, return the permutation with the lowest value/ordering.
For example, the list [1,2,3]
should return [1,3,2]
. The list [1,3,2]
should return [2,1,3]
. The list [3,2,1]
should return [1,2,3]
.
Can you perform the operation without allocating extra memory (disregarding the input memory)?
Bitwise Operations
- [Medium] Swap Even and Odd Bits – Given an unsigned 8-bit integer, swap its even and odd bits. The 1st and 2nd bit should be swapped, the 3rd and 4th bit should be swapped, and so on. (Try ME)
Question: Given an unsigned 8-bit integer, swap its even and odd bits. The 1st and 2nd bit should be swapped, the 3rd and 4th bit should be swapped, and so on.
Example:
10101010 should be 01010101. 11100010 should be 11010001.
Bonus: Can you do this in one line?
- [Easy] Reverse Bits – Given a 32 bit integer, reverse the bits and return that number. (Try ME)
Questions: Given a 32 bit integer, reverse the bits and return that number.
Example:
Input: 1234
# In bits this would be 0000 0000 0000 0000 0000 0100 1101 0010
Output: 1260388352
# Reversed bits is 0100 1011 0010 0000 0000 0000 0000 0000
- [Easy] Flip Bit to Get Longest Sequence of 1s – Given an integer, flip exactly one bit from a 0 to a 1 to get the longest sequence of 1s. (Try ME)
Question: Given an integer, can you flip exactly one bit from a 0 to a 1 to get the longest sequence of 1s? Return the longest possible length of 1s after flip.
Example:
Input: 183 (or binary: 10110111)
Output: 6
Explanation: 10110111 => 10111111. The longest sequence of 1s is of length 6.
- [Easy] Number of 1 bits – Given an integer, find the number of 1 bits it has. (Try ME)
Question: Given an integer, find the number of 1 bits it has.
Example:
one_bits(23) # Returns 4 as 23 equals 0b10111
- [Medium] Bitwise AND of a Range – Write a function that returns the bitwise AND of all integers between M and N, inclusive. (Try ME)
Question: Write a function that returns the bitwise AND of all integers between M and N, inclusive.
- [Easy] Valid UTF-8 Encoding – Takes in an array of integers representing byte values, and returns whether it is a valid UTF-8 encoding. (Try ME)
Question: UTF-8 is a character encoding that maps each symbol to one, two, three, or four bytes.
Write a program that takes in an array of integers representing byte values, and returns whether it is a valid UTF-8 encoding.
For example, the Euro sign, €
, corresponds to the three bytes 11100010 10000010 10101100. The rules for mapping characters are as follows:
- For a single-byte character, the first bit must be zero.
- For an n-byte character, the first byte starts with n ones and a zero. The other n - 1 bytes all start with 10.
Visually, this can be represented as follows.
Bytes | Byte format
-----------------------------------------------
1 | 0xxxxxxx
2 | 110xxxxx 10xxxxxx
3 | 1110xxxx 10xxxxxx 10xxxxxx
4 | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
- [Easy] Longest Consecutive 1s in Binary Representation – Given an integer n, return the length of the longest consecutive run of 1s in its binary representation. (Try ME)
Question: Given an integer n, return the length of the longest consecutive run of 1s in its binary representation.
Example:
Input: 156
Output: 3
Exaplanation: 156 in binary is 10011100
- [Medium] LC 29. Divide Two Integers – Implement integer division without using the division operator: take two numbers, return a tuple of (dividend, remainder) (Try ME)
Question: Implement integer division without using the division operator. Your function should return a tuple of (dividend, remainder)
and it should take two numbers, the product and divisor.
For example, calling divide(10, 3)
should return (3, 1)
since the divisor is 3
and the remainder is 1
.
- [Hard] Find the Element That Appears Once While Others Occur 3 Times – Given an array of integers where every integer occurs three times except for one integer, which only occurs once, find that element. (Try ME)
Question: Given an array of integers where every integer occurs three times except for one integer, which only occurs once, find and return the non-duplicated integer.
For example, given [6, 1, 3, 3, 3, 6, 6]
, return 1
. Given [13, 19, 13, 13]
, return 19
.
Do this in O(N)
time and O(1)
space.
- [Hard] Find Next Sparse Number – For a given input N, find the smallest sparse number greater than or equal to N. (Try ME)
Question: We say a number is sparse if there are no adjacent ones in its binary representation. For example, 21 (10101)
is sparse, but 22 (10110)
is not.
For a given input N
, find the smallest sparse number greater than or equal to N
.
Do this in faster than O(N log N)
time.
- [Medium] Find Next Biggest Integer – Given an integer n, find the next biggest integer with the same number of 1-bits on. (Try ME)
Question: Given an integer n
, find the next biggest integer with the same number of 1-bits on. For example, given the number 6 (0110 in binary)
, return 9 (1001)
.
- [Easy] Find Unique Element among Array of Duplicates – Given an array of integers, arr, where all numbers occur twice except one number which occurs once, find the number. (Try ME)
Question: Given an array of integers, arr, where all numbers occur twice except one number which occurs once, find the number. Your solution should ideally be O(n)
time and use constant extra space.
Example:
Input: arr = [7, 3, 5, 5, 4, 3, 4, 8, 8]
Output: 7
- [Medium] Find Two Elements Appear Once – Given an array with two elements appear exactly once and all other elements appear exactly twice,find the two elements that appear only once (Try ME)
Question: Given an array of integers in which two elements appear exactly once and all other elements appear exactly twice, find the two elements that appear only once. Can you do this in linear time and constant space?
Example:
Input: [2, 4, 6, 8, 10, 2, 6, 10]
Output: [4, 8] order does not matter
Hashing
- [Hard] Minimum Appends to Craft a Palindrome – Given a string s we need to append (insertion at end) minimum characters to make a string palindrome. (Try ME)
Question: Given a string s we need to append (insertion at end) minimum characters to make a string palindrome.
Follow-up: Don’t use Manacher’s Algorithm, even though Longest Palindromic Substring can be efficiently solved with that algorithm.
Example 1:
Input : s = "abede"
Output : "abedeba"
We can make string palindrome as "abedeba" by adding ba at the end of the string.
Example 2:
Input : s = "aabb"
Output : "aabbaa"
We can make string palindrome as"aabbaa" by adding aa at the end of the string.
- [Medium] Bloom Filter – Bloom Filter is a data structure features fast and space-efficient element checking. (Try ME)
Question: Implement a data structure which carries out the following operations without resizing the underlying array:
add(value)
: Add a value to the set of values.check(value)
: Check whether a value is in the set.
Note: The check method may return occasional false positives (in other words, incorrectly identifying an element as part of the set), but should always correctly identify a true element. In other words, a query returns either “possibly in set” or “definitely not in set.”
Background: Suppose you are creating an account on a website, you want to enter a cool username, you entered it and got a message, “Username is already taken”. You added your birth date along username, still no luck. Now you have added your university roll number also, still got “Username is already taken”. It’s really frustrating, isn’t it? But have you ever thought how quickly the website check availability of username by searching millions of username registered with it. That is exactly when above data structure comes into play.
- [Easy] URL Shortener – Implement a URL shortener with the following methods: shorten(url), restore(short). (Try ME)
Question: Implement a URL shortener with the following methods:
shorten(url)
, which shortens the url into a six-character alphanumeric string, such aszLg6wl
.restore(short)
, which expands the shortened string into the original url. If no such shortened string exists, returnnull
.
Follow-up: What if we enter the same URL twice?
- [Medium] LC 652. Find Duplicate Subtrees – Given a binary tree, find all duplicate subtrees (subtrees with the same value and same structure) (Try ME)
Question: Given a binary tree, find all duplicate subtrees (subtrees with the same value and same structure) and return them as a list of list [subtree1, subtree2, ...]
where subtree1
is a duplicate of subtree2
etc.
Example1:
Given the following tree:
1
/ \
2 2
/ /
3 3
The duplicate subtrees are
2
/ And 3
3
Example2:
Given the following tree:
1
/ \
2 3
/ / \
4 2 4
/
4
The duplicate subtrees are
2
/ And 4
4
- [Easy] LC 796. Shift-Equivalent Strings – Given two strings A and B, return whether or not A can be shifted some number of times to get B. (Try ME)
Question: Given two strings A and B, return whether or not A can be shifted some number of times to get B.
For example, if A is 'abcde'
and B is 'cdeab'
, return True
. If A is 'abc'
and B is 'acb'
, return False
.
Array
- [Medium] Longest Consecutive Sequence in an Unsorted Array – Given an array of integers, return the largest range, inclusive, of integers that are all included in the array. (Try ME)
Question: Given an array of integers, return the largest range, inclusive, of integers that are all included in the array.
For example, given the array [9, 6, 1, 3, 8, 10, 12, 11]
, return (8, 12)
since 8, 9, 10, 11, and 12
are all in the array.
- [Medium] Look-and-Say Sequence – The “look and say”: beginning with the term 1, each subsequent term visually describes the digits appearing in the previous term. (Try ME)
Question: The “look and say” sequence is defined as follows: beginning with the term 1, each subsequent term visually describes the digits appearing in the previous term. The first few terms are as follows:
1
11
21
1211
111221
As an example, the fourth term is 1211, since the third term consists of one 2 and one 1.
Given an integer N, print the Nth term of this sequence
- [Easy] Sum Binary Numbers – Given two binary numbers represented as strings, return the sum of the two binary numbers. (Try ME)
Question: Given two binary numbers represented as strings, return the sum of the two binary numbers as a new binary represented as a string. Do this without converting the whole binary string into an integer.
Example:
sum_binary("11101", "1011")
# returns "101000"
- [Easy] Plus One – Given a non-empty array where each element represents a digit of a non-negative integer, add one to the integer. (Try ME)
Question: Given a non-empty array where each element represents a digit of a non-negative integer, add one to the integer. The most significant digit is at the front of the array and each element in the array contains only one digit. Furthermore, the integer does not have leading zeros, except in the case of the number ‘0’.
Example:
Input: [2, 3, 4]
Output: [2, 3, 5]
- [Medium] Longest Alternating Subsequence Problem – Finding the length of a subsequence of a given sequence in which the elements are in alternating order. (Try ME)
Question: Finding the length of a subsequence of a given sequence in which the elements are in alternating order, and in which the sequence is as long as possible.
For example, consider array A[] = [8, 9, 6, 4, 5, 7, 3, 2, 4]
The length of longest subsequence is 6
and the subsequence is [8, 9, 6, 7, 3, 4]
as (8 < 9 > 6 < 7 > 3 < 4)
.
- [Easy] Rearrange Array in Alternating Positive & Negative Order – Given an array of positive and negative numbers, arrange them in an alternate fashion. (Try ME)
Question: Given an array of positive and negative numbers, arrange them in an alternate fashion such that every positive number is followed by negative and vice-versa maintaining the order of appearance.
Number of positive and negative numbers need not be equal. If there are more positive numbers they appear at the end of the array. If there are more negative numbers, they too appear in the end of the array.
Example1:
Input: arr[] = {1, 2, 3, -4, -1, 4}
Output: arr[] = {-4, 1, -1, 2, 3, 4}
Example2:
Input: arr[] = {-5, -2, 5, 2, 4, 7, 1, 8, 0, -8}
output: arr[] = {-5, 5, -2, 2, -8, 4, 7, 1, 8, 0}
- [Easy] Permutation with Given Order – Given an array and a permutation, apply the permutation to the array. (Try ME)
Question: A permutation can be specified by an array P
, where P[i]
represents the location of the element at i
in the permutation. For example, [2, 1, 0]
represents the permutation where elements at the index 0
and 2
are swapped.
Given an array and a permutation, apply the permutation to the array.
For example, given the array ["a", "b", "c"]
and the permutation [2, 1, 0]
, return ["c", "b", "a"]
.
- [Medium] Peaks and Troughs in an Array of Integers – Find a list of all the peaks and another list of all the troughs present in the array. (Try ME)
Question: Given an array of integers arr[], the task is to print a list of all the peaks and another list of all the troughs present in the array. A peak is an element in the array which is greater than its neighbouring elements. Similarly, a trough is an element that is smaller than its neighbouring elements.
Example 1:
Input: arr[] = {5, 10, 5, 7, 4, 3, 5}
Output:
Peaks : 10 7 5
Troughs : 5 5 3
Example 2:
Input: arr[] = {1, 2, 3, 4, 5}
Output:
Peaks : 5
Troughs : 1
- [Easy] Intersection of Lists – Given 3 sorted lists, find the intersection of those 3 lists. (Try ME)
Question: Given 3 sorted lists, find the intersection of those 3 lists.
Example:
intersection([1, 2, 3, 4], [2, 4, 6, 8], [3, 4, 5]) # returns [4]
- [Easy] Three Equal Sums – Given an array of numbers, determine whether it can be partitioned into 3 arrays of equal sums. (Try ME)
Question: Given an array of numbers, determine whether it can be partitioned into 3 arrays of equal sums.
Example:
[0, 2, 1, -6, 6, -7, 9, 1, 2, 0, 1] can be partitioned into:
[0, 2, 1], [-6, 6, -7, 9, 1], [2, 0, 1] all of which sum to 3.
- [Easy] Find Duplicates – Given an array of size n, and all values in the array are in the range 1 to n, find all the duplicates. (Try ME)
Question: Given an array of size n, and all values in the array are in the range 1 to n, find all the duplicates.
Example:
Input: [4, 3, 2, 7, 8, 2, 3, 1]
Output: [2, 3]
- [Medium] LC 227. Basic Calculator II – Implement a basic calculator to evaluate a simple expression string. (Try ME)
Question: Implement a basic calculator to evaluate a simple expression string.
The expression string contains only non-negative integers, +, -, *, / operators and empty spaces. The integer division should truncate toward zero.
Example 1:
Input: "3+2*2"
Output: 7
Example 2:
Input: " 3/2 "
Output: 1
Example 3:
Input: " 3+5 / 2 "
Output: 5
- [Hard] LC 772. Basic Calculator III – Implement a basic calculator to evaluate a simple expression string contains integers, +, -, *, / operators , open and closing parentheses. (Try ME)
Questions: Implement a basic calculator to evaluate a simple expression string.
The expression string contains integers, +
, -
, *
, /
operators , open (
and closing parentheses )
and empty spaces. The integer division should truncate toward zero.
Example 1:
Input: "1 + 1"
Output: 2
Example 2:
Input: " 6-4 / 2 "
Output: 4
Example 3:
Input: "2*(5+5*2)/3+(6/2+8)"
Output: 21
Example 4:
Input: "(2+6* 3+5- (3*14/7+2)*5)+3"
Output: -12
- [Easy] Compare Version Numbers – Given two version numbers version1 and version2, conclude which is the latest version number. (Try ME)
Question: Version numbers are strings that are used to identify unique states of software products. A version number is in the format a.b.c.d. and so on where a, b, etc. are numeric strings separated by dots. These generally represent a hierarchy from major to minor changes.
Given two version numbers version1 and version2, conclude which is the latest version number. Your code should do the following:
- If version1 > version2 return 1.
- If version1 < version2 return -1.
- Otherwise return 0.
Note that the numeric strings such as a, b, c, d, etc. may have leading zeroes, and that the version strings do not start or end with dots. Unspecified level revision numbers default to 0.
Example 1:
Input:
version1 = "1.0.33"
version2 = "1.0.27"
Output: 1
#version1 > version2
Example 2:
Input:
version1 = "0.1"
version2 = "1.1"
Output: -1
#version1 < version2
Example 3:
Input:
version1 = "1.01"
version2 = "1.001"
Output: 0
#ignore leading zeroes, 01 and 001 represent the same number.
Example 4:
Input:
version1 = "1.0"
version2 = "1.0.0"
Output: 0
#version1 does not have a 3rd level revision number, which
defaults to "0"
- [Medium] LC 554. Brick Wall – Find a vertical line going from the top to the bottom of the wall that cuts through the fewest (Try ME)
Question: A wall consists of several rows of bricks of various integer lengths and uniform height. Your goal is to find a vertical line going from the top to the bottom of the wall that cuts through the fewest number of bricks. If the line goes through the edge between two bricks, this does not count as a cut.
For example, suppose the input is as follows, where values in each row represent the lengths of bricks in that row:
[[3, 5, 1, 1],
[2, 3, 3, 2],
[5, 5],
[4, 4, 2],
[1, 3, 3, 3],
[1, 1, 6, 1, 1]]
The best we can we do here is to draw a line after the eighth brick, which will only require cutting through the bricks in the third and fifth row.
Given an input consisting of brick lengths for each row such as the one above, return the fewest number of bricks that must be cut to create a vertical line.
- [Easy] Distribute Bonuses – Give the smallest positive amount to each worker and if a developer has written more lines of code than their neighbors that dev earns more. (Try ME)
Question: MegaCorp wants to give bonuses to its employees based on how many lines of codes they have written. They would like to give the smallest positive amount to each worker consistent with the constraint that if a developer has written more lines of code than their neighbor, they should receive more money.
Given an array representing a line of seats of employees at MegaCorp, determine how much each one should get paid.
Example:
Input: [10, 40, 200, 1000, 60, 30]
Output: [1, 2, 3, 4, 2, 1].
- [Easy] LC 448. Find Missing Numbers in an Array – Given an array of size n range from 1 to n inclusive, find all of the elements of [1, n] that do not appear in the array. (Try ME)
Question: Given an array of integers of size n, where all elements are between 1 and n inclusive, find all of the elements of [1, n] that do not appear in the array. Some numbers may appear more than once.
Follow-up: Could you do it without extra space and in O(n) runtime?
Example1:
Input: [4, 3, 2, 7, 8, 2, 3, 1]
Output: [5, 6]
Example2:
Input: [4, 5, 2, 6, 8, 2, 1, 5]
Output: [3, 7]
- [Medium] Find Missing Positive – Given an unsorted integer array, find the first missing positive integer. (Try ME)
Question: Given an unsorted integer array, find the first missing positive integer.
Example 1:
Input: [1, 2, 0]
Output: 3
Example 2:
Input: [3, 4, -1, 1]
Output: 2
- [Easy] LC 121. Best Time to Buy and Sell Stock – Given an array of numbers representing the stock prices of a company, write a function that calculates the maximum profit. (Try ME)
Question: You are given an array. Each element represents the price of a stock on that particular day. Calculate and return the maximum profit you can make from buying and selling that stock only once.
Example:
Input: [9, 11, 8, 5, 7, 10]
Output: 5
Explanation: Here, the optimal trade is to buy when the price is 5, and sell when it is 10, so the return value should be 5 (profit = 10 - 5 = 5).
- [Medium] LC 1014. Best Sightseeing Pair – The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them. (Try ME)
Question: Given an array A
of positive integers, A[i]
represents the value of the i-th sightseeing spot, and two sightseeing spots i
and j
have distance j - i
between them.
The score of a pair (i < j
) of sightseeing spots is (A[i] + A[j] + i - j
) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
- [Hard] Exclusive Product – Given an array of integers, return a new array such that each element at index i of the new array is the product of all the numbers except i. (Try ME)
Question: Given an array of integers, return a new array such that each element at index i of the new array is the product of all the numbers in the original array except the one at i.
For example, if our input was [1, 2, 3, 4, 5]
, the expected output would be [120, 60, 40, 30, 24]
. If our input was [3, 2, 1]
, the expected output would be [2, 3, 6]
.
Follow-up: what if you can’t use division?
- [Easy] Delete Columns to Make Sorted I – Given a 2D matrix of lowercase letters. Determine the minimum number of columns that can be removed to ensure that each column is sorted. (Try ME)
Question: You are given an N by M 2D matrix of lowercase letters. Determine the minimum number of columns that can be removed to ensure that each row is ordered from top to bottom lexicographically. That is, the letter at each column is lexicographically later as you go down each row. It does not matter whether each row itself is ordered lexicographically.
Example 1:
Given the following table:
cba
daf
ghi
This is not ordered because of the a in the center. We can remove the second column to make it ordered:
ca
df
gi
So your function should return 1, since we only needed to remove 1 column.
Example 2:
Given the following table:
abcdef
Your function should return 0, since the rows are already ordered (there's only one row).
Example 3:
Given the following table:
zyx
wvu
tsr
Your function should return 3, since we would need to remove all the columns to order it.
- [Medium] LC 665. Off-by-One Non-Decreasing Array – Given an array of integers, write a function to determine whether the array could become non-decreasing by modifying at most 1 element. (Try ME)
Question: Given an array of integers, write a function to determine whether the array could become non-decreasing by modifying at most 1 element.
For example, given the array [10, 5, 7]
, you should return true, since we can modify the 10
into a 1
to make the array non-decreasing.
Given the array [10, 5, 1]
, you should return false, since we can’t modify any one element to get a non-decreasing array.
- [Easy] Witness of The Tall People – There are n people lined up, a murder has happened right in front of them. How many witnesses are there? (Try ME)
Question: There are n
people lined up, and each have a height represented as an integer. A murder has happened right in front of them, and only people who are taller than everyone in front of them are able to see what has happened. How many witnesses are there?
Example:
Input: [3, 6, 3, 4, 1]
Output: 3
Explanation: Only [6, 4, 1] were able to see in front of them.
#
#
# #
####
####
#####
36341
- [Medium] LC 54. Spiral Matrix – Given a matrix of n x m elements (n rows, m columns), return all elements of the matrix in spiral order. (Try ME)
Question: Given a matrix of n x m elements (n rows, m columns), return all elements of the matrix in spiral order.
Example 1:
Input:
[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
Output: [1, 2, 3, 6, 9, 8, 7, 4, 5]
Example 2:
Input:
[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]
]
Output: [1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7]
- [Easy] Record the Last N Orders – You run an e-commerce website and want to record the last N order ids in a log. (Try ME)
Question: You run an e-commerce website and want to record the last N
order ids in a log. Implement a data structure to accomplish this, with the following API:
record(order_id)
: adds the order_id to the logget_last(i)
: gets the ith last element from the log.i
is guaranteed to be smaller than or equal toN
.
You should be as efficient with time and space as possible.
- [Easy] In-place Array Rotation – Write a function that rotates an array by k elements. (Try ME)
Write a function that rotates an array by k
elements.
For example, [1, 2, 3, 4, 5, 6]
rotated by two becomes [3, 4, 5, 6, 1, 2]
.
Try solving this without creating a copy of the array. How many swap or move operations do you need?
- [Medium] Longest Subarray with Sum Divisible by K – Find the length of the longest subarray with sum of the elements divisible by the given value k. (Try ME)
Question: Given an arr[] containing n integers and a positive integer k. The problem is to find the length of the longest subarray with sum of the elements divisible by the given value k.
Example:
Input : arr[] = {2, 7, 6, 1, 4, 5}, k = 3
Output : 4
The subarray is {7, 6, 1, 4} with sum 18, which is divisible by 3.
- [Medium] LC 152. Maximum Product Subarray – Given an array that contains both positive and negative integers, find the product of the maximum product subarray. (Try ME)
Question: Given an array that contains both positive and negative integers, find the product of the maximum product subarray.
Example 1:
Input: [6, -3, -10, 0, 2]
Output: 180
Explanation: The subarray is [6, -3, -10]
Example 2:
Input: [-1, -3, -10, 0, 60]
Output: 60
Explanation: The subarray is [60]
Example 3:
Input: [-2, -3, 0, -2, -40]
Output: 80
Explanation: The subarray is [-2, -40]
- [Medium] Majority Element – A majority element is an element that appears more than half the time. Given a list with a majority element, find the majority element. (Try ME)
Question: A majority element is an element that appears more than half the time. Given a list with a majority element, find the majority element.
Example:
majority_element([3, 5, 3, 3, 2, 4, 3]) # gives 3
- [Easy] Valid Mountain Array – Given an array of heights, determine whether the array forms a “mountain” pattern. A mountain pattern goes up and then down. (Try ME)
Question: Given an array of heights, determine whether the array forms a “mountain” pattern. A mountain pattern goes up and then down.
Example:
validMountainArray([1, 2, 3, 2, 1]) # True
validMountainArray([1, 2, 3]) # False
- [Easy] Matrix Rotation – Given an N by N matrix, rotate it by 90 degrees clockwise. (Try ME)
Question: Given an N by N matrix, rotate it by 90 degrees clockwise.
For example, given the following matrix:
[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
you should return:
[[7, 4, 1],
[8, 5, 2],
[9, 6, 3]]
Follow-up: What if you couldn’t use any extra space?
Interval
- [Medium] Amazing Number – Given a circular array, find the starting position, such that the total number of amazing numbers in the array is maximized. (Try ME)
Question: Define amazing number as: its value is less than or equal to its index. Given a circular array, find the starting position, such that the total number of amazing numbers in the array is maximized.
Follow-up: Should get a solution with time complexity less than O(N^2)
Example 1:
Input: [0, 1, 2, 3]
Ouptut: 0. When starting point at position 0, all the elements in the array are equal to its index. So all the numbers are amazing number.
Example 2:
Input: [1, 0, 0]
Output: 1. When starting point at position 1, the array becomes 0, 0, 1. All the elements are amazing number.
If there are multiple positions, return the smallest one.
- [Hard] LC 352. Data Stream as Disjoint Intervals – Given a data stream input of non-negative integers a1, a2, …, an, …, summarize the numbers seen so far as a list of disjoint intervals. (Try ME)
Question: Given a data stream input of non-negative integers a1, a2, ..., an, ...
, summarize the numbers seen so far as a list of disjoint intervals.
For example, suppose the integers from the data stream are 1, 3, 7, 2, 6, ...
, then the summary will be:
[1, 1]
[1, 1], [3, 3]
[1, 1], [3, 3], [7, 7]
[1, 3], [7, 7]
[1, 3], [6, 7]
- [Easy] LC 228. Extract Range – Given a sorted list of numbers, return a list of strings that represent all of the consecutive numbers. (Try ME)
Question: Given a sorted list of numbers, return a list of strings that represent all of the consecutive numbers.
Example:
Input: [0, 1, 2, 5, 7, 8, 9, 9, 10, 11, 15]
Output: ['0->2', '5', '7->11', '15']
- [Easy] Merge Overlapping Intervals – Given a list of possibly overlapping intervals, return a new list of intervals where all overlapping intervals have been merged. (Try ME)
Question: Given a list of possibly overlapping intervals, return a new list of intervals where all overlapping intervals have been merged.
The input list is not necessarily ordered in any way.
For example, given [(1, 3), (5, 8), (4, 10), (20, 25)]
, you should return [(1, 3), (4, 10), (20, 25)]
.
- [Easy] LC 253. Minimum Lecture Rooms – Given an array of time intervals (start, end) for classroom lectures (possibly overlapping), find the minimum number of rooms required. (Try ME)
Questions: Given an array of time intervals (start, end)
for classroom lectures (possibly overlapping), find the minimum number of rooms required.
For example, given [(30, 75), (0, 50), (60, 150)]
, you should return 2
.
- [Easy] Busiest Period in the Building – Given a list of data entries represent entries and exits of groups of people into a building. Find the busiest period in the building. (Try ME)
Question: You are given a list of data entries that represent entries and exits of groups of people into a building.
Find the busiest period in the building, that is, the time with the most people in the building. Return it as a pair of (start, end) timestamps.
You can assume the building always starts off and ends up empty, i.e. with 0 people inside.
An entry looks like this:
{"timestamp": 1526579928, count: 3, "type": "enter"}
This means 3 people entered the building. An exit looks like this:
{"timestamp": 1526580382, count: 2, "type": "exit"}
This means that 2 people exited the building. timestamp is in Unix time.
- [Medium] LC 986. Interval List Intersections – Given two lists of closed intervals, each list of intervals is pairwise disjoint and in sorted order. (Try ME)
Question: Given two lists of closed intervals, each list of intervals is pairwise disjoint and in sorted order.
Return the intersection of these two interval lists.
Example:
Input: A = [[0,2],[5,10],[13,23],[24,25]], B = [[1,5],[8,12],[15,24],[25,26]]
Output: [[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]
- [Hard] LC 57. Insert Interval – Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). (Try ME)
Question: Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).
You may assume that the intervals were initially sorted according to their start times.
Example 1:
Input: intervals = [[1,3],[6,9]], newInterval = [2,5]
Output: [[1,5],[6,9]]
Example 2:
Input: intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8]
Output: [[1,2],[3,10],[12,16]]
Explanation: Because the new interval [4,8] overlaps with [3,5],[6,7],[8,10].
- [Medium] LC 435. Non-overlapping Intervals – Given a collection of intervals, find the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping. (Try ME)
Question: Given a collection of intervals, find the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping.
Intervals can “touch”, such as [0, 1]
and [1, 2]
, but they won’t be considered overlapping.
For example, given the intervals (7, 9), (2, 4), (5, 8)
, return 1
as the last interval can be removed and the first two won’t overlap.
The intervals are not necessarily sorted in any order.
Binary Search
- [Easy] Markov Chain – Run the Markov chain starting from start for num_steps and compute the number of times we visited each state. (Try ME)
Question: You are given a starting state start, a list of transition probabilities for a Markov chain, and a number of steps num_steps. Run the Markov chain starting from start for num_steps and compute the number of times we visited each state.
For example, given the starting state a, number of steps 5000, and the following transition probabilities:
[
('a', 'a', 0.9),
('a', 'b', 0.075),
('a', 'c', 0.025),
('b', 'a', 0.15),
('b', 'b', 0.8),
('b', 'c', 0.05),
('c', 'a', 0.25),
('c', 'b', 0.25),
('c', 'c', 0.5)
]
One instance of running this Markov chain might produce { 'a': 3012, 'b': 1656, 'c': 332 }.
- [Medium] Allocate Minimum Number of Pages – The task is to assign books in such a way that the maximum number of pages assigned to a student is minimum. (Try ME)
Question: Given number of pages in n different books and m students. The books are arranged in ascending order of number of pages. Every student is assigned to read some consecutive books. The task is to assign books in such a way that the maximum number of pages assigned to a student is minimum.
Example:
Input : pages[] = {12, 34, 67, 90}
m = 2
Output : 113
Explanation:
There are 2 number of students. Books can be distributed
in following fashion :
1) [12] and [34, 67, 90]
Max number of pages is allocated to student
2 with 34 + 67 + 90 = 191 pages
2) [12, 34] and [67, 90]
Max number of pages is allocated to student
2 with 67 + 90 = 157 pages
3) [12, 34, 67] and [90]
Max number of pages is allocated to student
1 with 12 + 34 + 67 = 113 pages
Of the 3 cases, Option 3 has the minimum pages = 113.
- [Medium] Power of 4 – Given a 32-bit positive integer
N
, determine whether it is a power of four in faster than O(log N)
time. (Try ME)
N
, determine whether it is a power of four in faster than O(log N)
time. (Try ME)Questions: Given a 32-bit positive integer N, determine whether it is a power of four in faster than O(log N)
time.
Example1:
Input: 16
Output: 16 is a power of 4
Example2:
Input: 20
Output: 20 is not a power of 4
- [Medium] H-Index II – The h-index is if a scholar has at least h of their papers cited h times. Given a sorted list of citation number, calculate h-index. (Try ME)
Question: The h-index is a metric that attempts to measure the productivity and citation impact of the publication of a scholar. The definition of the h-index is if a scholar has at least h of their papers cited h times.
Given a list of publications of the number of citations a scholar has, find their h-index.
Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize your algorithm?
Example:
Input: [0, 1, 3, 3, 5]
Output: 3
Explanation:
There are 3 publications with 3 or more citations, hence the h-index is 3.
- [Medium] Minimum Days to Bloom Roses – Given an array of roses. Return the earliest day that we can get n bouquets of roses. (Try ME)
Question: Given an array of roses. roses[i]
means rose i
will bloom on day roses[i]
. Also given an int k
, which is the minimum number of adjacent bloom roses required for a bouquet, and an int n
, which is the number of bouquets we need. Return the earliest day that we can get n
bouquets of roses.
Example:
Input: roses = [1, 2, 4, 9, 3, 4, 1], k = 2, n = 2
Output: 4
Explanation:
day 1: [b, n, n, n, n, n, b]
The first and the last rose bloom.
day 2: [b, b, n, n, n, n, b]
The second rose blooms. Here the first two bloom roses make a bouquet.
day 3: [b, b, n, n, b, n, b]
day 4: [b, b, b, n, b, b, b]
Here the last three bloom roses make a bouquet, meeting the required n = 2 bouquets of bloom roses. So return day 4.
- [Medium] Stores and Houses – Given 2 arrays representing integer locations of stores and houses. For each house, find the store closest to it. (Try ME)
Question: You are given 2 arrays representing integer locations of stores and houses (each location in this problem is one-dementional). For each house, find the store closest to it. Return an integer array result where result[i] should denote the location of the store closest to the i-th house. If many stores are equidistant from a particular house, choose the store with the smallest numerical location. Note that there may be multiple stores and houses at the same location.
Example 1:
Input: houses = [5, 10, 17], stores = [1, 5, 20, 11, 16]
Output: [5, 11, 16]
Explanation:
The closest store to the house at location 5 is the store at the same location.
The closest store to the house at location 10 is the store at the location 11.
The closest store to the house at location 17 is the store at the location 16.
Example 2:
Input: houses = [2, 4, 2], stores = [5, 1, 2, 3]
Output: [2, 3, 2]
Example 3:
Input: houses = [4, 8, 1, 1], stores = [5, 3, 1, 2, 6]
Output: [3, 6, 1, 1]
- [Medium] K-th Missing Number in Sorted Array – Given a sorted, define the missing numbers to be the gap among numbers. Calculate K-th missing number. (Try ME)
Question: Given a sorted without any duplicate integer array, define the missing numbers to be the gap among numbers. Write a function to calculate K-th missing number. If such number does not exist, then return null.
For example, original array: [2,4,7,8,9,15]
, within the range defined by original array, all missing numbers are: [3,5,6,10,11,12,13,14]
- the 1st missing number is 3,
- the 2nd missing number is 5,
- the 3rd missing number is 6
- [Medium] K Closest Elements – Given a list of sorted numbers, and two integers k and x, find k closest numbers to the pivot x. (Try ME)
Question: Given a list of sorted numbers, and two integers k
and x
, find k
closest numbers to the pivot x
.
Example:
closest_nums([1, 3, 7, 8, 9], 3, 5) # gives [7, 3, 8]
- [Hard] Maximize the Minimum of Subarray Sum – Given an array of numbers N and integer k, split N into k partitions such that the maximize sum of any partition is minimized. (Try ME)
Question: Given an array of numbers N
and an integer k
, your task is to split N
into k
partitions such that the maximum sum of any partition is minimized. Return this sum.
For example, given N = [5, 1, 2, 7, 3, 4]
and k = 3
, you should return 8
, since the optimal partition is [5, 1, 2], [7], [3, 4]
.
- [Easy] Fixed Point – Given a sorted array of distinct elements, return a fixed point, if one exists. Otherwise, return False. (Try ME)
Questions: A fixed point in an array is an element whose value is equal to its index. Given a sorted array of distinct elements, return a fixed point, if one exists. Otherwise, return False
.
For example, given [-6, 0, 2, 40]
, you should return 2
. Given [1, 5, 7, 8]
, you should return False
.
- [Medium] Find Minimum Element in a Sorted and Rotated Array – Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. Find the minimum element. (Try ME)
Question: Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. Find the minimum element in O(log N)
time. You may assume the array does not contain duplicates.
For example, given [5, 7, 10, 3, 4]
, return 3
.
- [Medium] Searching in Rotated Array – A sorted array of integers was rotated an unknown number of times. Find the index of the element in the array. (Try ME)
Question: A sorted array of integers was rotated an unknown number of times. Given such an array, find the index of the element in the array in faster than linear time. If the element doesn’t exist in the array, return null.
For example, given the array [13, 18, 25, 2, 8, 10]
and the element 8, return 4 (the index of 8 in the array).
You can assume all the integers in the array are unique.
- [Hard] LC 668. Kth Smallest Number in Multiplication Table – Find out the k-th smallest number quickly from the multiplication table. (Try ME)
Question: Find out the k-th smallest number quickly from the multiplication table.
Given the height m and the length n of a m * n Multiplication Table, and a positive integer k, you need to return the k-th smallest number in this table.
Example 1:
Input: m = 3, n = 3, k = 5
Output:
Explanation:
The Multiplication Table:
1 2 3
2 4 6
3 6 9
The 5-th smallest number is 3 (1, 2, 2, 3, 3).
Example 2:
Input: m = 2, n = 3, k = 6
Output:
Explanation:
The Multiplication Table:
1 2 3
2 4 6
The 6-th smallest number is 6 (1, 2, 2, 3, 4, 6).
- [Easy] First and Last Indices of an Element in a Sorted Array – Given a sorted array, A, with possibly duplicated elements, find the indices of the first and last occurrences of a target element. (Try ME)
Question: Given a sorted array, A, with possibly duplicated elements, find the indices of the first and last occurrences of a target element, x. Return -1 if the target is not found.
Examples:
Input: A = [1, 3, 3, 5, 7, 8, 9, 9, 9, 15], target = 9
Output: [6, 8]
Input: A = [100, 150, 150, 153], target = 150
Output: [1, 2]
Input: A = [1, 2, 3, 4, 5, 6, 10], target = 9
Output: [-1, -1]
- [Hard] LC 300. The Longest Increasing Subsequence – Given an array of numbers, find the length of the longest increasing subsequence in the array. (Try ME)
Question: Given an array of numbers, find the length of the longest increasing subsequence in the array. The subsequence does not necessarily have to be contiguous.
For example, given the array [0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15]
, the longest increasing subsequence has length 6
ie. [0, 2, 6, 9, 11, 15]
.
- [Hard] Increasing Subsequence of Length K – Given an int array nums of length n and an int k. Return an increasing subsequence of length k (KIS). (Try ME)
Question: Given an int array nums of length n and an int k. Return an increasing subsequence of length k (KIS). Expected time complexity O(nlogk)
.
Example 1:
Input: nums = [10, 1, 4, 8, 2, 9], k = 3
Output: [1, 4, 8] or [1, 4, 9] or [1, 8, 9]
Example 2:
Input: nums = [10, 1, 4, 8, 2, 9], k = 4
Output: [1, 4, 8, 9]
- [Easy] LC 162. Find a Peak Element – Given an unsorted array, in which all elements are distinct, find a “peak” element in O(log N) time. (Try ME)
Question: Given an unsorted array, in which all elements are distinct, find a “peak” element in O(log N)
time.
An element is considered a peak if it is greater than both its left and right neighbors. It is guaranteed that the first and last elements are lower than all others.
Example 1:
Input: [5, 10, 20, 15]
Output: 20
The element 20 has neighbours 10 and 15,
both of them are less than 20.
Example 2:
Input: [10, 20, 15, 2, 23, 90, 67]
Output: 20 or 90
The element 20 has neighbours 10 and 15,
both of them are less than 20, similarly 90 has neighbous 23 and 67.
- [Medium] LC 163. Missing Ranges – Given a sorted list of numbers, and two integers low and high, return a list of (inclusive) ranges where the numbers are missing. (Try ME)
Question: Given a sorted list of numbers, and two integers low and high representing the lower and upper bound of a range, return a list of (inclusive) ranges where the numbers are missing. A range should be represented by a tuple in the format of (lower, upper).
Example:
missing_ranges(nums=[1, 3, 5, 10], lower=1, upper=10)
# returns [(2, 2), (4, 4), (6, 9)]
Two Pointers
- [Hard] LC 296. Best Meeting Point – A group of two or more people wants to meet and minimize the total travel distance. (Try ME)
Question: A group of two or more people wants to meet and minimize the total travel distance. You are given a 2D grid of values 0 or 1, where each 1 marks the home of someone in the group. The distance is calculated using Manhattan Distance, where distance(p1, p2) = |p2.x - p1.x| + |p2.y - p1.y|
.
Hint: Try to solve it in one dimension first. How can this solution apply to the two dimension case?
Example:
Input:
1 - 0 - 0 - 0 - 1
| | | | |
0 - 0 - 0 - 0 - 0
| | | | |
0 - 0 - 1 - 0 - 0
Output: 6
Explanation: Given three people living at (0,0), (0,4), and (2,2):
The point (0,2) is an ideal meeting point, as the total travel distance
of 2+2+2=6 is minimal. So return 6.
- [Easy] String Compression – Given an array of characters with repeats, compress it in place. (Try ME)
Question: Given an array of characters with repeats, compress it in place. The length after compression should be less than or equal to the original array.
Example:
Input: ['a', 'a', 'b', 'c', 'c', 'c']
Output: ['a', '2', 'b', 'c', '3']
- [Medium] Mininum Adjacent Swaps to Make Palindrome – Given a string, what is the minimum number of adjacent swaps required to convert a string into a palindrome. (Try ME)
Question: Given a string, what is the minimum number of adjacent swaps required to convert a string into a palindrome. If not possible, return -1.
Example 1:
Input: "mamad"
Output: 3
Example 2:
Input: "asflkj"
Output: -1
Example 3:
Input: "aabb"
Output: 2
Example 4:
Input: "ntiin"
Output: 1
Explanation: swap 't' with 'i' => "nitin"
- [Medium] LC 16. Closest to 3 Sum – Given a list of numbers and a target number n, find 3 numbers in the list that sums closest to the target number n. (Try ME)
Question: Given a list of numbers and a target number n, find 3 numbers in the list that sums closest to the target number n. There may be multiple ways of creating the sum closest to the target number, you can return any combination in any order.
Example:
Input: [2, 1, -5, 4], -1
Output: [-5, 1, 2]
Explanation: Closest sum is -5+1+2 = -2 OR -5+1+4 = 0
- [Medium] 4 Sum – Given a list of numbers, and a target number n, find all unique combinations of a, b, c, d, such that a + b + c + d = n. (Try ME)
Question: Given a list of numbers, and a target number n, find all unique combinations of a, b, c, d, such that a + b + c + d = n.
Example 1:
fourSum([1, 1, -1, 0, -2, 1, -1], 0)
# returns [[-1, -1, 1, 1], [-2, 0, 1, 1]]
Example 2:
fourSum([3, 0, 1, -5, 4, 0, -1], 1)
# returns [[-5, -1, 3, 4]]
Example 3:
fourSum([0, 0, 0, 0, 0], 0)
# returns [[0, 0, 0, 0]]
- [Easy] Minimum Distance between Two Words – Find an efficient algorithm to find the smallest distance (measured in number of words) between any two given words in a string. (Try ME)
Question: Find an efficient algorithm to find the smallest distance (measured in number of words) between any two given words in a string.
For example, given words "hello"
, and "world"
and a text content of "dog cat hello cat dog dog hello cat world"
, return 1
because there’s only one word "cat"
in between the two words.
- [Hard] Count Elements in Sorted Matrix – Let A be a sorted matrix. Given i1, j1, i2, and j2, compute the number of elements smaller than A[i1, j1] and larger than A[i2, j2]. (Try ME)
Question: Let A be an N
by M
matrix in which every row and every column is sorted.
Given i1
, j1
, i2
, and j2
, compute the number of elements smaller than A[i1, j1]
and larger than A[i2, j2]
.
Example:
Given the following matrix:
[[ 1, 3, 6, 10, 15, 20],
[ 2, 7, 9, 14, 22, 25],
[ 3, 8, 10, 15, 25, 30],
[10, 11, 12, 23, 30, 35],
[20, 25, 30, 35, 40, 45]]
And i1 = 1, j1 = 1, i2 = 3, j2 = 3
return 15 as there are 15 numbers in the matrix smaller than 7 or greater than 23.
- [Easy] LC 859. Buddy Strings – Given two string A, B. Return true if and only if we can swap two letters in A so that the result equals B. (Try ME)
Question: Given two strings A and B of lowercase letters, return true if and only if we can swap two letters in A so that the result equals B.
Example 1:
Input: A = "ab", B = "ba"
Output: true
Example 2:
Input: A = "ab", B = "ab"
Output: false
Example 3:
Input: A = "aa", B = "aa"
Output: true
Example 4:
Input: A = "aaaaaaabc", B = "aaaaaaacb"
Output: true
Example 5:
Input: A = "", B = "aa"
Output: false
- [Medium] Find Pythagorean Triplets – Given a list of numbers, find if there exists a Pythagorean triplet in that list. (Try ME)
Question: Given a list of numbers, find if there exists a pythagorean triplet in that list. A pythagorean triplet is 3
variables a
, b
, c
where a * a + b * b = c * c
.
Example:
Input: [3, 5, 12, 5, 13]
Output: True
Here, 5 * 5 + 12 * 12 = 13 * 13.
- [Medium] LT 640. One Edit Distance – Given two strings S and T, determine if they are both one edit distance apart. (Try ME)
Question: Given two strings S and T, determine if they are both one edit distance apart.
Example 1:
Input: s = "aDb", t = "adb"
Output: True
Example 2:
Input: s = "ab", t = "ab"
Output: False
Explanation:
s=t, so they aren't one edit distance apart
- [Easy] Array of Equal Parts – Given an array with only positive integers, determine if exist two numbers such that removing them can break array into 3 equal-sum sub-arrays. (Try ME)
Question: Given an array containing only positive integers, return if you can pick two integers from the array which cuts the array into three pieces such that the sum of elements in all pieces is equal.
Example 1:
Input: [2, 4, 5, 3, 3, 9, 2, 2, 2]
Output: True
Explanation: choosing the number 5 and 9 results in three pieces [2, 4], [3, 3] and [2, 2, 2]. Sum = 6.
Example 2:
Input: [1, 1, 1, 1]
Output: False
Sliding Window
- [Medium] More Than 3 Times Badge-access In One-hour Period – Given an unordered list of names and entry times over a single day. Finds anyone who badged into the room three or more times in a one-hour period (Try ME)
Question: We are working on a security system for a badged-access room in our company’s building.
We want to find employees who badged into our secured room unusually often. We have an unordered list of names and entry times over a single day. Access times are given as numbers up to four digits in length using 24-hour time, such as “800” or “2250”.
Write a function that finds anyone who badged into the room three or more times in a one-hour period, and returns each time that they badged in during that period. (If there are multiple one-hour periods where this was true, just return the first one.)
Example:
badge_times = [
["Paul", 1355],
["Jennifer", 1910],
["John", 830],
["Paul", 1315],
["John", 1615],
["John", 1640],
["John", 835],
["Paul", 1405],
["John", 855],
["John", 930],
["John", 915],
["John", 730],
["Jennifer", 1335],
["Jennifer", 730],
["John", 1630],
]
Expected output (in any order)
John: 830 835 855 915 930
Paul: 1315 1355 1405
- [Medium] LC 838. Push Dominoes – Figure out the final position of the dominoes. If there are dominoes that get pushed on both ends, the force cancels out and that domino remains upright. (Try ME)
Question: Given a string with the initial condition of dominoes, where:
.
represents that the domino is standing stillL
represents that the domino is falling to the left sideR
represents that the domino is falling to the right side
Figure out the final position of the dominoes. If there are dominoes that get pushed on both ends, the force cancels out and that domino remains upright.
Example 1:
Input: "..R...L..R."
Output: "..RR.LL..RR"
Example 2:
Input: "RR.L"
Output: "RR.L"
Explanation: The first domino expends no additional force on the second domino.
Example 3:
Input: ".L.R...LR..L.."
Output: "LL.RR.LLRRLL.."
- [Medium] LC 1525. Number of Good Ways to Split a String – Return the number of ways S can be split such that the number of unique characters between S1 and S2 are the same. (Try ME)
Question: Given a string S, we can split S into 2 strings: S1 and S2. Return the number of ways S can be split such that the number of unique characters between S1 and S2 are the same.
Example 1:
Input: "aaaa"
Output: 3
Explanation: we can get a - aaa, aa - aa, aaa- a
Example 2:
Input: "bac"
Output: 0
Example 3:
Input: "ababa"
Output: 2
Explanation: ab - aba, aba - ba
- [Medium] All Max-size Subarrays with Distinct Elements – Given an array of integers, print all maximum size sub-arrays having all distinct elements in them. (Try ME)
Question: Given an array of integers, print all maximum size sub-arrays having all distinct elements in them.
Example:
Input: [5, 2, 3, 5, 4, 3]
Output: [[5, 2, 3], [2, 3, 5, 4], [5, 4, 3]]
- [Medium] Smallest Window Contains Every Distinct Character – Given a string, find the length of the smallest window that contains every distinct character. (Try ME)
Question: Given a string, find the length of the smallest window that contains every distinct character. Characters may appear more than once in the window.
For example, given "jiujitsu"
, you should return 5
, corresponding to the final five letters.
- [Medium] LC 438. Anagram Indices Problem – Given a word W and a string S, find all starting indices in S which are anagrams of W. (Try ME)
Question: Given a word W and a string S, find all starting indices in S which are anagrams of W.
For example, given that W is "ab"
, and S is "abxaba"
, return 0
, 3
, and 4
.
- [Medium] LC 239. Sliding Window Maximum – Given an array nums, return the max sliding window. (Try ME)
Question: Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position. Return the max sliding window.
Example:
Input: nums = [1, 3, -1, -3, 5, 3, 6, 7], and k = 3
Output: [3, 3, 5, 5, 6, 7]
Explanation:
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 3
1 [3 -1 -3] 5 3 6 7 3
1 3 [-1 -3 5] 3 6 7 5
1 3 -1 [-3 5 3] 6 7 5
1 3 -1 -3 [5 3 6] 7 6
1 3 -1 -3 5 [3 6 7] 7
- [Medium] Substrings with Exactly K Distinct Characters – Given a string s and an int k, return an int representing the number of substrings (not unique) of s with exactly k distinct characters. (Try ME)
Question: Given a string s
and an int k
, return an int representing the number of substrings (not unique) of s
with exactly k
distinct characters.
If the given string doesn’t have k
distinct characters, return 0
.
Example 1:
Input: s = "pqpqs", k = 2
Output: 7
Explanation: ["pq", "pqp", "pqpq", "qp", "qpq", "pq", "qs"]
Example 2:
Input: s = "aabab", k = 3
Output: 0
- [Medium] LT 386. Longest Substring with At Most K Distinct Characters – Given a string, find the longest substring that contains at most k unique characters. (Try ME)
Question: Given a string, find the longest substring that contains at most k unique characters.
For example, given "abcbbbbcccbdddadacb"
, the longest substring that contains 2 unique character is "bcbbbbcccb"
.
- [Easy] Longest Subarray Consisiting of Unique Elements from an Array – Given an array of elements, return the length of the longest subarray where all its elements are distinct. (Try ME)
Question: Given an array of elements, return the length of the longest subarray where all its elements are distinct.
For example, given the array [5, 1, 3, 5, 2, 3, 4, 1]
, return 5
as the longest subarray of distinct elements is [5, 2, 3, 4, 1]
.
- [Medium] Longest Substring without Repeating Characters – Given a string, find the length of the longest substring without repeating characters. (Try ME)
Question: Given a string, find the length of the longest substring without repeating characters.
Note: Can you find a solution in linear time?
Example:
lengthOfLongestSubstring("abrkaabcdefghijjxxx") # => 10 as len("abcdefghij") == 10
- [Medium] LC 209. Minimum Size Subarray Sum – Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of which the sum >= s. (Try ME)
Question: Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of which the sum >= s. If there isn’t one, return 0 instead.
Example:
Input: s = 7, nums = [2, 3, 1, 2, 4, 3]
Output: 2
Explanation: the subarray [4,3] has the minimal length under the problem constraint.
- [Easy] LC 383. Ransom Note – Given a magazine of letters and the note he wants to write, determine whether he can construct the word. (Try ME)
Question: A criminal is constructing a ransom note. In order to disguise his handwriting, he is cutting out letters from a magazine.
Given a magazine of letters and the note he wants to write, determine whether he can construct the word.
Example 1:
Input: ['a', 'b', 'c', 'd', 'e', 'f'], 'bed'
Output: True
Example 2:
Input: ['a', 'b', 'c', 'd', 'e', 'f'], 'cat'
Output: False
List
Singly Linked List
- [Easy] Determine If Singly Linked List is Palindrome – Determine whether a singly linked list is a palindrome. (Try ME)
Question: Determine whether a singly linked list is a palindrome.
For example, 1 -> 4 -> 3 -> 4 -> 1
returns True
while 1 -> 4
returns False
.
- [Easy] Swap Even and Odd Nodes – Given the head of a singly linked list, swap every two nodes and return its head. (Try ME)
Question: Given the head of a singly linked list, swap every two nodes and return its head.
Note: Make sure it’s acutally nodes that get swapped not value.
Example:
Given 1 -> 2 -> 3 -> 4, return 2 -> 1 -> 4 -> 3.
- [Medium] LC 138. Deepcopy List with Random Pointer – A linked list is given such that each node contains an additional random pointer points to any node in the list or null. Deepcopy that list. (Try ME)
Question: A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.
Return a deep copy of the list.
Example:
Input:
{"$id":"1","next":{"$id":"2","next":null,"random":{"$ref":"2"},"val":2},"random":{"$ref":"2"},"val":1}
Explanation:
Node 1's value is 1, both of its next and random pointer points to Node 2.
Node 2's value is 2, its next pointer points to null and its random pointer points to itself.
- [Easy] Reverse a Linked List – Given a singly-linked list, reverse the list. This can be done iteratively or recursively. Can you get both solutions? (Try ME)
Question: Given a singly-linked list, reverse the list. This can be done iteratively or recursively. Can you get both solutions?
Example:
Input: 4 -> 3 -> 2 -> 1 -> 0 -> NULL
Output: 0 -> 1 -> 2 -> 3 -> 4 -> NULL
- [Easy] Add Two Numbers as a Linked List – Given two linked-lists representing two non-negative integers. Add the two numbers and return it as a linked list. (Try ME)
Question: You are given two linked-lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
Example:
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.
- [Easy] Remove Duplicates From Sorted Linked List – Given a sorted linked list of integers, remove all the duplicate elements in the linked list so that all elements are unique. (Try ME)
Question: Given a sorted linked list, remove all duplicate values from the linked list.
Example 1:
Input: 1 -> 1 -> 2 -> 3 -> 4 -> 4 -> 4 -> 4 -> 4 -> 5 -> 5 -> 6 -> 7 -> 9
Output: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 9
Example 2:
Input: 1 -> 1 -> 1 -> 1
Output: 1
- [Easy] Remove Duplicates From Linked List – Given a linked list, remove all duplicate values from the linked list. (Try ME)
Question: Given a linked list, remove all duplicate values from the linked list.
For instance, given 1 -> 2 -> 3 -> 3 -> 4
, then we wish to return the linked list 1 -> 2 -> 4
.
- [Medium] LC 86. Partitioning Linked List – Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. (Try ME)
Question: Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.
You should preserve the original relative order of the nodes in each of the two partitions.
Example:
Input: head = 1->4->3->2->5->2, x = 3
Output: 1->2->2->4->3->5
- [Easy] Zig-Zag Distinct LinkedList – Rearrange distinct linked-list such that they appear in alternating order. (Try ME)
Question: Given a linked list with DISTINCT value, rearrange the node values such that they appear in alternating low -> high -> low -> high ...
form. For example, given 1 -> 2 -> 3 -> 4 -> 5
, you should return 1 -> 3 -> 2 -> 5 -> 4
.
- [Easy] Remove K-th Last Element from Singly Linked-list – Given a singly linked list and an integer k, remove the kth last element from the list. (Try ME)
Question: Given a singly linked list and an integer k, remove the kth last element from the list. k is guaranteed to be smaller than the length of the list.
Note:
- The list is very long, so making more than one pass is prohibitively expensive.
- Do this in constant space and in one pass.
- [Easy] Intersection of N Arrays – Given n arrays, find the intersection of them. (Try ME)
Question: Given n arrays, find the intersection of them.
Example:
intersection([1, 2, 3, 4], [2, 4, 6, 8], [3, 4, 5]) # returns [4]
- [Easy] Intersection of Linked Lists – Given two singly linked lists. The lists intersect at some node. Find, and return the node. Note: the lists are non-cyclical. (Try ME)
Question: You are given two singly linked lists. The lists intersect at some node. Find, and return the node. Note: the lists are non-cyclical.
Example:
A = 1 -> 2 -> 3 -> 4
B = 6 -> 3 -> 4
# This should return 3 (you may assume that any nodes with the same value are the same node)
Doubly Linked List
- [Medium] XOR Linked List – An XOR linked list is a more memory efficient doubly linked list. (Try ME)
Question: An XOR linked list is a more memory efficient doubly linked list. Instead of each node holding next and prev fields, it holds a field named both, which is an XOR of the next node and the previous node. Implement an XOR linked list; it has an add(element)
which adds the element to the end, and a get(index)
which returns the node at index.
If using a language that has no pointers (such as Python), you can assume you have access to get_pointer
and dereference_pointer
functions that converts between nodes and memory addresses.
- [Easy] Determine If Linked List is Palindrome – You are given a doubly linked list. Determine if it is a palindrome. (Try ME)
Question: You are given a doubly linked list. Determine if it is a palindrome.
- [Hard] First Unique Character from a Stream – Given a stream of characters, find the first unique (non-repeating) character from stream. (Try ME)
Question: Given a stream of characters, find the first unique (non-repeating) character from stream. You need to tell the first unique character in O(1) time at any moment.
Example:
Input: Stream.of('abracadabra')
Output: Stream.of('aaabbbbbrcc')
Explanation:
a => a
abr => a
abra => b
abracadab => r
abracadabra => c
Circular Linked List
- [Medium] Insert into Sorted Circular Linked List – Insert a new value into a sorted circular linked list (last element points to head). Return the node with smallest value. (Try ME)
Question: Insert a new value into a sorted circular linked list (last element points to head). Return the node with smallest value.
Fast-slow Pointers
- [Easy] Move Zeros – Given an array nums, write a function to move all 0’s to the end of it while maintaining the relative order of the non-zero elements. (Try ME)
Question: Given an array nums, write a function to move all 0’s to the end of it while maintaining the relative order of the non-zero elements.
You must do this in-place without making a copy of the array. Minimize the total number of operations.
Example:
Input: [0,1,0,3,12]
Output: [1,3,12,0,0]
- [Easy] Remove k-th Last Element From Linked List – You are given a singly linked list and an integer k. Return the linked list, removing the k-th last element from the list. (Try ME)
Question: You are given a singly linked list and an integer k. Return the linked list, removing the k-th last element from the list.
- [Easy] Rotate Linked List – Given a linked list and a number k, rotate the linked list by k places. (Try ME)
Question: Given a linked list and a positive integer k
, rotate the list to the right by k places.
For example, given the linked list 7 -> 7 -> 3 -> 5
and k = 2
, it should become 3 -> 5 -> 7 -> 7
.
Given the linked list 1 -> 2 -> 3 -> 4 -> 5
and k = 3
, it should become 3 -> 4 -> 5 -> 1 -> 2
.
- [Medium] Detect Linked List Cycle – Given a linked list, determine if the linked list has a cycle in it. (Try ME)
Question: Given a linked list, determine if the linked list has a cycle in it.
Example:
Input: 4 -> 3 -> 2 -> 1 -> 3 ...
Output: True
- [Medium] LC 287. Find the Duplicate Number – Given an array of length
n + 1
whose elements belong to the set {1, 2, ..., n}
. Find any duplicate element. (Try ME)
n + 1
whose elements belong to the set {1, 2, ..., n}
. Find any duplicate element. (Try ME)You are given an array of length n + 1
whose elements belong to the set {1, 2, ..., n}
. By the pigeonhole principle, there must be a duplicate. Find it in linear time and space.
Sort
- [Easy] LC 937. Reorder Log Files – Reorder the logs so that all of the letter-logs come before any digit-log. (Try ME)
Question: You have an array of logs. Each log is a space delimited string of words.
For each log, the first word in each log is an alphanumeric identifier. Then, either:
- Each word after the identifier will consist only of lowercase letters, or;
- Each word after the identifier will consist only of digits.
We will call these two varieties of logs letter-logs and digit-logs. It is guaranteed that each log has at least one word after its identifier.
Reorder the logs so that all of the letter-logs come before any digit-log. The letter-logs are ordered lexicographically ignoring identifier, with the identifier used in case of ties. The digit-logs should be put in their original order.
Return the final order of the logs.
Example:
Input: ["a1 9 2 3 1","g1 act car","zo4 4 7","ab1 off key dog","a8 act zoo"]
Output: ["g1 act car","a8 act zoo","ab1 off key dog","a1 9 2 3 1","zo4 4 7"]
- [Easy] Sorted Square of Integers – Given a sorted list of integers, square the elements and give the output in sorted order. (Try ME)
Question: Given a sorted list of integers, square the elements and give the output in sorted order.
For example, given [-9, -2, 0, 2, 3]
, return [0, 4, 4, 9, 81]
.
Additonal Requirement: Do it in-place. i.e. Space Complexity O(1).
- [Easy] Make the Largest Number – Given a number of integers, combine them so it would create the largest number. (Try ME)
Question: Given a number of integers, combine them so it would create the largest number.
Example:
Input: [17, 7, 2, 45, 72]
Output: 77245217
- [Easy] Sorting Window Range – Given a list of numbers, find the smallest window to sort such that the whole list will be sorted. (Try ME)
Question: Given a list of numbers, find the smallest window to sort such that the whole list will be sorted. If the list is already sorted return (0, 0).
Example:
Input: [2, 4, 7, 5, 6, 8, 9]
Output: (2, 4)
Explanation: Sorting the window (2, 4) which is [7, 5, 6] will also means that the whole list is sorted.
Merge Sort
- [Hard] Inversion Pairs – Count total number of pairs such that a smaller element appears after a larger element. (Try ME)
Question: We can determine how “out of order” an array A is by counting the number of inversions it has. Two elements A[i]
and A[j]
form an inversion if A[i] > A[j]
but i < j
. That is, a smaller element appears after a larger element. Given an array, count the number of inversions it has. Do this faster than O(N^2)
time. You may assume each element in the array is distinct.
For example, a sorted list has zero inversions. The array [2, 4, 1, 3, 5]
has three inversions: (2, 1)
, (4, 1)
, and (4, 3)
. The array [5, 4, 3, 2, 1]
has ten inversions: every distinct pair forms an inversion.
- [Medium] Sort Linked List – Given a linked list, sort it in O(n log n) time and constant space. (Try ME)
Question: Given a linked list, sort it in O(n log n)
time and constant space.
For example, the linked list 4 -> 1 -> -3 -> 99
should become -3 -> 1 -> 4 -> 99
.
Quick Select & Quick Sort
- [Easy] Find the K-th Largest Number – Find the k-th largest number in a sequence of unsorted numbers. (Try ME)
Question: Find the k-th largest number in a sequence of unsorted numbers. Can you do this in linear time?
Example:
Input: 3, [8, 7, 2, 3, 4, 1, 5, 6, 9, 0]
Output: 7
- [Medium] Sorting a List With 3 Unique Numbers – Given a list of numbers with only 3 unique numbers (1, 2, 3), sort the list in O(n) time. (Try ME)
Question: Given a list of numbers with only 3
unique numbers (1, 2, 3)
, sort the list in O(n)
time.
Example:
Input: [3, 3, 2, 1, 3, 2, 1]
Output: [1, 1, 2, 2, 3, 3, 3]
- [Hard] RGB Element Array Swap – Given an array of strictly the characters ‘R’, ‘G’, and ‘B’, sort array in RGB order. (Try ME)
Question: Given an array of strictly the characters ‘R’, ‘G’, and ‘B’, segregate the values of the array so that all the Rs come first, the Gs come second, and the Bs come last. You can only swap elements of the array.
Do this in linear time and in-place.
For example, given the array ['G', 'B', 'R', 'R', 'B', 'R', 'G']
, it should become ['R', 'R', 'R', 'G', 'G', 'B', 'B']
.
Counting Sort
- [Hard] The Most Efficient Way to Sort a Million 32-bit Integers – Given an array of a million integers between zero and a billion, out of order, how can you efficiently sort it? (Try ME)
Question: Given an array of a million integers between zero and a billion, out of order, how can you efficiently sort it?
- [Easy] LC 451. Sort Characters By Frequency – Given a string, sort it in decreasing order based on the frequency of characters. (Try ME)
Question: Given a string, sort it in decreasing order based on the frequency of characters. If there are multiple possible solutions, return any of them.
For example, given the string tweet
, return tteew
. eettw
would also be acceptable.
String
- [Easy] LC 696. Count Binary Substrings – Give a binary string s, return the number of non-empty substrings that have the same number of 0’s and 1’s (Try ME)
Question: Give a binary string s, return the number of non-empty substrings that have the same number of 0’s and 1’s, and all the 0’s and all the 1’s in these substrings are grouped consecutively.
Substrings that occur multiple times are counted the number of times they occur.
Example 1:
Input: s = "00110011"
Output: 6
Explanation: There are 6 substrings that have equal number of consecutive 1's and 0's: "0011", "01", "1100", "10", "0011", and "01".
Notice that some of these substrings repeat and are counted the number of times they occur.
Also, "00110011" is not a valid substring because all the 0's (and 1's) are not grouped together.
Example 2:
Input: s = "10101"
Output: 4
Explanation: There are 4 substrings: "10", "01", "10", "01" that have equal number of consecutive 1's and 0's.
- [Medium] Break Sentence – Given a string s and an integer k, break up the string into multiple lines such that each line has a length of k or less. (Try ME)
Question: Given a string s and an integer k, break up the string into multiple lines such that each line has a length of k or less. You must break it up so that words don’t break across lines. Each line has to have the maximum possible amount of words. If there’s no way to break the text up, then return null.
You can assume that there are no spaces at the ends of the string and that there is exactly one space between each word.
For example, given the string “the quick brown fox jumps over the lazy dog” and k = 10
, you should return: ["the quick", "brown fox", "jumps over", "the lazy", "dog"]
. No string in the list has a length of more than 10.
- [Medium] LC 394. Decode String – Given a string with a certain rule:
k[string]
should be expanded to string k
times. (Try ME)
k[string]
should be expanded to string k
times. (Try ME)Question: Given a string with a certain rule: k[string]
should be expanded to string k
times. So for example, 3[abc]
should be expanded to abcabcabc
. Nested expansions can happen, so 2[a2[b]c]
should be expanded to abbcabbc
.
- [Medium] LC 394. Decode String (Invariant) – Given an encoded string in form of
"ab[cd]{2}def"
. You have to return decoded string "abcdcddef"
(Try ME)
"ab[cd]{2}def"
. You have to return decoded string "abcdcddef"
(Try ME)Question: Given an encoded string in form of "ab[cd]{2}def"
. You have to return decoded string "abcdcddef"
Notice that if there is a number inside curly braces, then it means preceding string in square brackets has to be repeated the same number of times. It becomes tricky where you have nested braces.
Example 1:
Input: "ab[cd]{2}"
Output: "abcdcd"
Example 2:
Input: "def[ab[cd]{2}]{3}ghi"
Output: "defabcdcdabcdcdabcdcdghi"
- [Easy] Longest Common Prefix – Given a list of strings, find the longest common prefix between all strings. (Try ME)
Question: Given a list of strings, find the longest common prefix between all strings.
Example:
longest_common_prefix(['helloworld', 'hellokitty', 'helly'])
# returns 'hell'
- [Easy] LC 392. Is Subsequence – Given a string s and a string t, check if s is subsequence of t. (Try ME)
Question: Given a string s and a string t, check if s is subsequence of t.
A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, “ace” is a subsequence of “abcde” while “aec” is not).
Example 1:
s = "abc", t = "ahbgdc"
Return true.
Example 2:
s = "axc", t = "ahbgdc"
Return false.
- [Medium] LC 722. Remove Comments – Given a C/C++ program, remove comments from it. (Try ME)
Question: Given a C/C++ program, remove comments from it.
Example 1:
Input:
source = ["/*Test program */", "int main()", "{ ", " // variable declaration ", "int a, b, c;", "/* This is a test", " multiline ", " comment for ", " testing */", "a = b + c;", "}"]
Output: ["int main()","{ "," ","int a, b, c;","a = b + c;","}"]
Example 2:
Input:
source = ["a/*comment", "line", "more_comment*/b"]
Output: ["ab"]
- [Medium] LC 821. Shortest Distance to Character – Given a string s and a character c, find the distance for all characters in the string to the character c in the string s. (Try ME)
Question: Given a string s and a character c, find the distance for all characters in the string to the character c in the string s.
You can assume that the character c will appear at least once in the string.
Example:
shortest_dist('helloworld', 'l')
# returns [2, 1, 0, 0, 1, 2, 2, 1, 0, 1]
- [Hard] Efficiently Manipulate a Very Long String – Design a tree-based data structure to efficiently manipulate a very long string (Try ME)
Question: Design a tree-based data structure to efficiently manipulate a very long string that supports the following operations:
char char_at(int index)
, return char at indexLongString substring_at(int start_index, int end_index)
, return substring based on start and end indexvoid delete(int start_index, int end_index)
, deletes the substring
- [Hard] Reverse Words Keep Delimiters – Given a string and a set of delimiters, reverse the words in the string while maintaining the relative order of the delimiters. (Try ME)
Question: Given a string and a set of delimiters, reverse the words in the string while maintaining the relative order of the delimiters. For example, given “hello/world:here”, return “here/world:hello”
Follow-up: Does your solution work for the following cases: “hello/world:here/”, “hello//world:here”
- [Hard] Regular Expression: Period and Asterisk – Implement regular expression matching with the following special characters: . (period) and * (asterisk) (Try ME)
Question: Implement regular expression matching with the following special characters:
-
.
(period) which matches any single character -
*
(asterisk) which matches zero or more of the preceding element
That is, implement a function that takes in a string and a valid regular expression and returns whether or not the string matches the regular expression.
For example, given the regular expression “ra.” and the string “ray”, your function should return true. The same regular expression on the string “raymond” should return false.
Given the regular expression “.*at” and the string “chat”, your function should return true. The same regular expression on the string “chats” should return false.
- [Hard] LC 65. Valid Number – Given a string that may represent a number, determine if it is a number. (Try ME)
Question: Given a string, return whether it represents a number. Here are the different kinds of numbers:
- “10”, a positive integer
- “-10”, a negative integer
- “10.1”, a positive real number
- “-10.1”, a negative real number
- “1e5”, a number in scientific notation
And here are examples of non-numbers:
- “a”
- “x 1”
- “a -2”
- ”-“
- [Medium] LC 151. Reverse Words in a String – Given an input string, reverse the string word by word. (Try ME)
Question: Given an input string, reverse the string word by word.
Note:
- A word is defined as a sequence of non-space characters.
- Input string may contain leading or trailing spaces. However, your reversed string should not contain leading or trailing spaces.
- You need to reduce multiple spaces between two words to a single space in the reversed string.
Example 1:
Input: "the sky is blue"
Output: "blue is sky the"
Example 2:
Input: " hello world! "
Output: "world! hello"
Explanation: Your reversed string should not contain leading or trailing spaces.
Example 3:
Input: "a good example"
Output: "example good a"
Explanation: You need to reduce multiple spaces between two words to a single space in the reversed string.
- [Medium] Implement Soundex – Soundex is an algorithm used to categorize phonetically, such that two names that sound alike but are spelled differently have the same repr… (Try ME)
Question: Soundex is an algorithm used to categorize phonetically, such that two names that sound alike but are spelled differently have the same representation.
Soundex maps every name to a string consisting of one letter and three numbers, like M460.
One version of the algorithm is as follows:
- Remove consecutive consonants with the same sound (for example, change ck -> c).
- Keep the first letter. The remaining steps only apply to the rest of the string.
- Remove all vowels, including y, w, and h.
- Replace all consonants with the following digits:
- b, f, p, v → 1
- c, g, j, k, q, s, x, z → 2
- d, t → 3
- l → 4
- m, n → 5
- r → 6
- If you don’t have three numbers yet, append zeros until you do. Keep the first three numbers.
Using this scheme, Jackson and Jaxen both map to J250.
- [Easy] Run-length String Encode and Decode – Implement run-length encoding and decoding. (Try ME)
Question: Run-length encoding is a fast and simple method of encoding strings. The basic idea is to represent repeated successive characters as a single count and character. For example, the string "AAAABBBCCDAA"
would be encoded as "4A3B2C1D2A"
.
Implement run-length encoding and decoding. You can assume the string to be encoded have no digits and consists solely of alphabetic characters. You can assume the string to be decoded is valid.
- [Medium] Zig-Zag String – Given a string and a number of lines k, print the string in zigzag form. (Try ME)
Question: Given a string and a number of lines k, print the string in zigzag form. In zigzag, characters are printed out diagonally from top left to bottom right until reaching the kth line, then back up to top right, and so on.
Example:
Given the sentence "thisisazigzag" and k = 4, you should print:
t a g
h s z a
i i i z
s g
- [Medium] Tokenization – Given a dictionary of words and a string made up of those words (no spaces), return the original sentence in a list. (Try ME)
Questions: Given a dictionary of words and a string made up of those words (no spaces), return the original sentence in a list. If there is more than one possible reconstruction, return any of them. If there is no possible reconstruction, then return null.
Example 1:
Input: ['quick', 'brown', 'the', 'fox'], 'thequickbrownfox'
Output: ['the', 'quick', 'brown', 'fox']
Example 2:
Input: ['bed', 'bath', 'bedbath', 'and', 'beyond'], 'bedbathandbeyond'
Output: Either ['bed', 'bath', 'and', 'beyond'] or ['bedbath', 'and', 'beyond']
Parenthesis
- [Easy] Fix Brackets – Given a string with only
(
and )
, find the minimum number of characters to add or subtract to fix the string such that the brackets are balanced. (Try ME)
(
and )
, find the minimum number of characters to add or subtract to fix the string such that the brackets are balanced. (Try ME)Question: Given a string with only (
and )
, find the minimum number of characters to add or subtract to fix the string such that the brackets are balanced.
Example:
Input: '(()()'
Output: 1
Explanation:
The fixed string could either be ()() by deleting the first bracket, or (()()) by adding a bracket.
These are not the only ways of fixing the string, there are many other ways by adding it in different positions!
- [Easy] Balanced Brackets – Given a string of round, curly, and square open and closing brackets, return whether the brackets are balanced (well-formed). (Try ME)
Question: Given a string of round, curly, and square open and closing brackets, return whether the brackets are balanced (well-formed).
For example, given the string "([])[]({})"
, you should return true.
Given the string "([)]"
or "((()"
, you should return false.
- [Easy] Invalid Parentheses to Remove – Write a function to compute the minimum number of parentheses to be removed to make the string valid. (Try ME)
Question: Given a string of parentheses, write a function to compute the minimum number of parentheses to be removed to make the string valid (i.e. each open parenthesis is eventually closed).
For example, given the string "()())()"
, you should return 1
. Given the string ")("
, you should return 2
, since we must remove all of them.
- [Hard] LC 301. Remove Invalid Parentheses – Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results. (Try ME)
Question: Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results.
Note: The input string may contain letters other than the parentheses (
and )
.
Example 1:
Input: "()())()"
Output: ["()()()", "(())()"]
Example 2:
Input: "(a)())()"
Output: ["(a)()()", "(a())()"]
Example 3:
Input: ")("
Output: [""]
- [Medium] LC 678. Balanced Parentheses with Wildcard – Given string contains * that can represent either a (, ), or an empty string. Determine whether the parentheses are balanced. (Try ME)
Question: You’re given a string consisting solely of (
, )
, and *
. *
can represent either a (
, )
, or an empty string. Determine whether the parentheses are balanced.
For example, (()*
and (*)
are balanced. )*(
is not balanced.
- [Easy] LC 1021. Remove One Layer of Parenthesis – Given a valid parenthesis string, remove the outermost layers of the parenthesis string and return the new parenthesis string. (Try ME)
Question: Given a valid parenthesis string (with only ‘(‘ and ‘)’, an open parenthesis will always end with a close parenthesis, and a close parenthesis will never start first), remove the outermost layers of the parenthesis string and return the new parenthesis string.
If the string has multiple outer layer parenthesis (ie (())()), remove all outer layers and construct the new string. So in the example, the string can be broken down into (()) + (). By removing both components outer layer we are left with () + ‘’ which is simply (), thus the answer for that input would be ().
Example 1:
Input: '(())()'
Output: '()'
Example 2:
Input: '(()())'
Output: '()()'
Example 3:
Input: '()()()'
Output: ''
Anagram
- [Easy] Step Word Anagram – Given a dictionary of words and an input word, create a function that returns all valid step words (word adding a letter, and anagramming). (Try ME)
Question: A step word is formed by taking a given word, adding a letter, and anagramming the result. For example, starting with the word "APPLE"
, you can add an "A"
and anagram to get "APPEAL"
.
Given a dictionary of words and an input word, create a function that returns all valid step words.
- [Easy] Is Anagram of Palindrome – Given a string, determine whether any permutation of it is a palindrome. (Try ME)
Question: Given a string, determine whether any permutation of it is a palindrome.
For example, 'carrace'
should return True, since it can be rearranged to form 'racecar'
, which is a palindrome. 'daily'
should return False, since there’s no rearrangement that can form a palindrome.
Palindrome
- [Hard] LC 336. Palindrome Pairs – Given a list of words, find all pairs of unique indices such that the concatenation of the two words is a palindrome. (Try ME)
Question: Given a list of words, find all pairs of unique indices such that the concatenation of the two words is a palindrome.
For example, given the list ["code", "edoc", "da", "d"]
, return [(0, 1), (1, 0), (2, 3)]
.
- [Hard] K-Palindrome – Given a string which we can delete at most k, return whether you can make a palindrome. (Try ME)
Question: Given a string which we can delete at most k, return whether you can make a palindrome.
For example, given 'waterrfetawx'
and a k of 2, you could delete f and x to get 'waterretaw'
.
- [Medium] LC 131. Palindrome Partitioning – Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. (Try ME)
Question: Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s.
A palindrome string is a string that reads the same backward as forward.
Example 1:
Input: s = "aab"
Output: [["a","a","b"],["aa","b"]]
Example 2:
Input: s = "a"
Output: [["a"]]
- [Hard] Minimum Palindrome Substring – Given a string, split it into as few strings as possible such that each string is a palindrome. (Try ME)
Question: Given a string, split it into as few strings as possible such that each string is a palindrome.
For example, given the input string "racecarannakayak"
, return ["racecar", "anna", "kayak"]
.
Given the input string "abc"
, return ["a", "b", "c"]
.
- [Hard] Longest Palindromic Substring – Given a string, find the longest palindromic contiguous substring. If there are more than one with the maximum length, return any one. (Try ME)
Question: Given a string, find the longest palindromic contiguous substring. If there are more than one with the maximum length, return any one.
For example, the longest palindromic substring of "aabcdcb"
is "bcdcb"
. The longest palindromic substring of "bananas"
is "anana"
.
- [Easy] LC 680. Remove Character to Create Palindrome – Given a string, determine if you can remove any character to create a palindrome. (Try ME)
Question: Given a string, determine if you can remove any character to create a palindrome.
Example 1:
Input: "abcdcbea"
Output: True
Explanation: Remove 'e' gives "abcdcba"
Example 2:
Input: "abccba"
Output: True
Example 3:
Input: "abccaa"
Output: False
Tree
- [Medium] Lazy Binary Tree Generation – Generate a finite, but an arbitrarily large binary tree quickly in
O(1)
. (Try ME)
O(1)
. (Try ME)Question: Generate a finite, but an arbitrarily large binary tree quickly in O(1)
.
That is, generate()
should return a tree whose size is unbounded but finite.
- [Medium] LC 388. Longest Absolute File Path – Given a string representing the file system in certain format, return the length of the longest absolute path to a file in that fs. (Try ME)
Question: Suppose we represent our file system by a string in the following manner:
The string "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext"
represents:
dir
subdir1
subdir2
file.ext
The directory dir contains an empty sub-directory subdir1 and a sub-directory subdir2 containing a file file.ext.
The string "dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext"
represents:
dir
subdir1
file1.ext
subsubdir1
subdir2
subsubdir2
file2.ext
The directory dir contains two sub-directories subdir1 and subdir2. subdir1 contains a file file1.ext and an empty second-level sub-directory subsubdir1. subdir2 contains a second-level sub-directory subsubdir2 containing a file file2.ext.
We are interested in finding the longest (number of characters) absolute path to a file within our file system. For example, in the second example above, the longest absolute path is "dir/subdir2/subsubdir2/file2.ext"
, and its length is 32
(not including the double quotes).
Given a string representing the file system in the above format, return the length of the longest absolute path to a file in the abstracted file system. If there is no file in the system, return 0
.
Note:
- The name of a file contains at least a period and an extension.
- The name of a directory or sub-directory will not contain a period.
- [Medium] Invert a Binary Tree – Given the root of a binary tree. Invert the binary tree in place. (Try ME)
Question: You are given the root of a binary tree. Invert the binary tree in place. That is, all left children should become right children, and all right children should become left children.
Example:
Given the following tree:
a
/ \
b c
/ \ /
d e f
should become:
a
/ \
c b
\ / \
f e d
- [Medium] Locking in Binary Tree – Implement locking in a binary tree. A binary tree node can be locked or unlocked only if all of its descendants or ancestors are not locked. (Try ME)
Question: Implement locking in a binary tree. A binary tree node can be locked or unlocked only if all of its descendants or ancestors are not locked.
Design a binary tree node class with the following methods:
is_locked
, which returns whether the node is lockedlock
, which attempts to lock the node. If it cannot be locked, then it should return false. Otherwise, it should lock it and return true.unlock
, which unlocks the node. If it cannot be unlocked, then it should return false. Otherwise, it should unlock it and return true.
You may augment the node to add parent pointers or any other property you would like. You may assume the class is used in a single-threaded program, so there is no need for actual locks or mutexes. Each method should run in O(h), where h is the height of the tree.
- [Easy] Depth of Binary Tree in Peculiar String Representation – Given a binary tree in a peculiar string representation, determine the depth of the tree. (Try ME)
Question: You are given a binary tree in a peculiar string representation. Each node is written in the form (lr)
, where l
corresponds to the left child and r
corresponds to the right child.
If either l
or r
is null, it will be represented as a zero. Otherwise, it will be represented by a new (lr)
pair.
Given this representation, determine the depth of the tree.
Here are a few examples:
A root node with no children: (00)
A root node with two children: ((00)(00))
An unbalanced tree with three consecutive left children: ((((00)0)0)0)
BST
- [Medium] LC 981. Time Based Key-Value Store – Write a map implementation with a get function that lets you retrieve the value of a key at a particular time. (Try ME)
Question: Write a map implementation with a get function that lets you retrieve the value of a key at a particular time.
It should contain the following methods:
set(key, value, time)
: sets key to value for t = time.get(key, time)
: gets the key at t = time.
The map should work like this. If we set a key at a particular time, it will maintain that value forever or until it gets set at a later time. In other words, when we get a key at a time, it should return the value that was set for that key set at the most recent time.
- [Medium] Distance Between 2 Nodes in BST – Write a function that given a BST, it will return the distance (number of edges) between 2 nodes. (Try ME)
Question: Write a function that given a BST, it will return the distance (number of edges) between 2 nodes.
Example:
Given the following tree:
5
/ \
3 6
/ \ \
2 4 7
/ \
1 8
The distance between 1 and 4 is 3: [1 -> 2 -> 3 -> 4]
The distance between 1 and 8 is 6: [1 -> 2 -> 3 -> 5 -> 6 -> 7 -> 8]
- [Medium] Largest BST in a Binary Tree – You are given the root of a binary tree. Find and return the largest subtree of that tree, which is a valid binary search tree. (Try ME)
Question: You are given the root of a binary tree. Find and return the largest subtree of that tree, which is a valid binary search tree.
Example1:
Input:
5
/ \
2 4
/ \
1 3
Output:
2
/ \
1 3
Example2:
Input:
50
/ \
30 60
/ \ / \
5 20 45 70
/ \
65 80
Output:
60
/ \
45 70
/ \
65 80
- [Easy] Floor and Ceiling of BST – Given an integer k and a binary search tree, find the floor (less than or equal to) of k, and the ceiling (larger than or equal to) of k. (Try ME)
Question: Given an integer k
and a binary search tree, find the floor
(less than or equal to) of k
, and the ceiling
(larger than or equal to) of k
. If either does not exist, then print them as None.
Example:
8
/ \
4 12
/ \ / \
2 6 10 14
k: 11 Floor: 10 Ceil: 12
k: 1 Floor: None Ceil: 2
k: 6 Floor: 6 Ceil: 6
k: 15 Floor: 14 Ceil: None
- [Medium] Number of Smaller Elements to the Right – Given number array, return the number of smaller elements to the right of each element in the original input array. (Try ME)
Question: Given an array of integers, return a new array where each element in the new array is the number of smaller elements to the right of that element in the original input array.
For example, given the array [3, 4, 9, 6, 1]
, return [1, 1, 2, 1, 0]
, since:
- There is 1 smaller element to the right of 3
- There is 1 smaller element to the right of 4
- There are 2 smaller elements to the right of 9
- There is 1 smaller element to the right of 6
- There are no smaller elements to the right of 1
- [Easy] Generate Binary Search Trees – Given an integer N, construct all possible binary search trees with N nodes. (Try ME)
Question: Given an integer N, construct all possible binary search trees with N nodes.
- [Easy] Second Largest in BST – Given the root to a binary search tree, find the second largest node in the tree. (Try ME)
Question: Given the root to a binary search tree, find the second largest node in the tree.
Recursion
- [Easy] Valid Binary Search Tree – You are given the root of a binary tree. Find and return the largest subtree of that tree, which is a valid binary search tree. (Try ME)
Question: Determine whether a tree is a valid binary search tree.
A binary search tree is a tree with two children, left and right, and satisfies the constraint that the key in the left child must be less than or equal to the root and the key in the right child must be greater than or equal to the root.
- [Easy] LC 390. Josephus Problem – There are N prisoners standing in a circle, waiting to be executed. Determine where a prisoner should stand in order to be the last survivor. (Try ME)
Question: There are N
prisoners standing in a circle, waiting to be executed. The executions are carried out starting with the kth
person, and removing every successive kth
person going clockwise until there is no one left.
Given N
and k
, write an algorithm to determine where a prisoner should stand in order to be the last survivor.
For example, if N = 5
and k = 2
, the order of executions would be [2, 4, 1, 5, 3]
, so you should return 3
.
Note: if k = 2, exists O(log N)
solution
- [Medium] Subtree with Maximum Average – Given an N-ary tree, find the subtree with the maximum average. Return the root of the subtree. (Try ME)
Question: Given an N-ary tree, find the subtree with the maximum average. Return the root of the subtree.
A subtree of a tree is the node which have at least 1 child plus all its descendants. The average value of a subtree is the sum of its values, divided by the number of nodes.
Example:
Input:
_20_
/ \
12 18
/ | \ / \
11 2 3 15 8
Output: 18
Explanation:
There are 3 nodes which have children in this tree:
12 => (11 + 2 + 3 + 12) / 4 = 7
18 => (18 + 15 + 8) / 3 = 13.67
20 => (12 + 11 + 2 + 3 + 18 + 15 + 8 + 20) / 8 = 11.125
18 has the maximum average so output 18.
- [Easy] Height-balanced Binary Tree – Given a binary tree, determine whether or not it is height-balanced. (Try ME)
Question: Given a binary tree, determine whether or not it is height-balanced. A height-balanced binary tree can be defined as one in which the heights of the two subtrees of any node never differ by more than one.
- [Easy] Count Complete Binary Tree – Given a complete binary tree, count the number of nodes in faster than O(n) time. (Try ME)
Question: Given a complete binary tree, count the number of nodes in faster than O(n)
time.
Recall that a complete binary tree has every level filled except the last, and the nodes in the last level are filled starting from the left.
- [Medium] Split a Binary Search Tree – Given a BST and a value s, split the BST into 2 trees, where one tree has all values less than or equal to s, and the others. (Try ME)
Question: Given a binary search tree (BST) and a value s, split the BST into 2 trees, where one tree has all values less than or equal to s, and the other tree has all values greater than s while maintaining the tree structure of the original BST. You can assume that s will be one of the node’s value in the BST. Return both tree’s root node as a tuple.
Example:
Given the following tree, and s = 2
3
/ \
1 4
\ \
2 5
Split into two trees:
1 And 3
\ \
2 4
\
5
- [Easy] Filter Binary Tree Leaves – Given a binary tree and an integer k, filter the binary tree such that its leaves don’t contain the value k. (Try ME)
Questions: Given a binary tree and an integer k, filter the binary tree such that its leaves don’t contain the value k. Here are the rules:
- If a leaf node has a value of k, remove it.
- If a parent node has a value of k, and all of its children are removed, remove it.
Example:
Given the tree to be the following and k = 1:
1
/ \
1 1
/ /
2 1
After filtering the result should be:
1
/
1
/
2
- [Easy] Making a Height Balanced Binary Search Tree – Given a sorted list, create a height balanced binary search tree, meaning the height differences of each node can only differ by at most 1. (Try ME)
Question: Given a sorted list, create a height balanced binary search tree, meaning the height differences of each node can only differ by at most 1.
- [Medium] Root to Leaf Numbers Summed – Given a binary tree, sum all the numbers that can be constructed from the root to all leaves. (Try ME)
Question: A number can be constructed by a path from the root to a leaf. Given a binary tree, sum all the numbers that can be constructed from the root to all leaves.
Example:
Input:
1
/ \
2 3
/ \
4 5
Output: 262
Explanation: 124 + 125 + 13 = 262
- [Medium] Maximum Path Sum in Binary Tree – Given the root of a binary tree. Find the path between 2 nodes that maximizes the sum of all the nodes in the path, and return the sum. (Try ME)
Question: You are given the root of a binary tree. Find the path between 2 nodes that maximizes the sum of all the nodes in the path, and return the sum. The path does not necessarily need to go through the root.
Example:
Given the following binary tree, the result should be 42 = 20 + 2 + 10 + 10.
*10
/ \
*2 *10
/ \ \
*20 1 -25
/ \
3 4
(* denotes the max path)
- [Medium] Most Frequent Subtree Sum – Given the root of a binary tree, find the most frequent subtree sum. (Try ME)
Question: Given a binary tree, find the most frequent subtree sum.
If there is a tie between the most frequent sum, return the smaller one.
Example:
3
/ \
1 -3
The above tree has 3 subtrees.:
The root node with 3, and the 2 leaf nodes, which gives us a total of 3 subtree sums.
The root node has a sum of 1 (3 + 1 + -3).
The left leaf node has a sum of 1, and the right leaf node has a sum of -3.
Therefore the most frequent subtree sum is 1.
- [Medium] LC 236. Lowest Common Ancestor of a Binary Tree – Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. (Try ME)
Question: Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.
Example:
1
/ \
2 3
/ \ / \
4 5 6 7
LCA(4, 5) = 2
LCA(4, 6) = 1
LCA(3, 4) = 1
LCA(2, 4) = 2
- [Easy] Tree Isomorphism Problem – Write a function to detect if two trees are isomorphic. (Try ME)
Question: Write a function to detect if two trees are isomorphic. Two trees are called isomorphic if one of them can be obtained from other by a series of flips, i.e. by swapping left and right children of a number of nodes. Any number of nodes at any level can have their children swapped. Two empty trees are isomorphic.
Example:
The following two trees are isomorphic with following sub-trees flipped: 2 and 3, NULL and 6, 7 and 8.
Tree1:
1
/ \
2 3
/ \ /
4 5 6
/ \
7 8
Tree2:
1
/ \
3 2
\ / \
6 4 5
/ \
8 7
- [Easy] Full Binary Tree – Given a binary tree, remove the nodes in which there is only 1 child, so that the binary tree is a full binary tree. (Try ME)
Question: Given a binary tree, remove the nodes in which there is only 1 child, so that the binary tree is a full binary tree.
So leaf nodes with no children should be kept, and nodes with 2 children should be kept as well.
Example:
Given this tree:
1
/ \
2 3
/ / \
0 9 4
We want a tree like:
1
/ \
0 3
/ \
9 4
- [Easy] LC 938. Range Sum of BST – Given a binary search tree and a range [a, b] (inclusive), return the sum of the elements of the binary search tree within the range. (Try ME)
Question: Given a binary search tree and a range [a, b]
(inclusive), return the sum of the elements of the binary search tree within the range.
Example:
Given the range [4, 9] and the following tree:
5
/ \
3 8
/ \ / \
2 4 6 10
return 23 (5 + 4 + 6 + 8).
Traversal
- [Easy] Inorder Successor in BST – Given a node in a binary search tree (may not be the root), find the next largest node in the binary search tree. (Try ME)
Question: Given a node in a binary search tree (may not be the root), find the next largest node in the binary search tree (also known as an inorder successor). The nodes in this binary search tree will also have a parent field to traverse up the tree.
Example:
Given the following BST:
20
/ \
8 22
/ \
4 12
/ \
10 14
inorder successor of 8 is 10,
inorder successor of 10 is 12 and
inorder successor of 14 is 20.
- [Easy] BST Nodes Sum up to K – Given the root of a binary search tree, and a target K, return two nodes in the tree whose sum equals K. (Try ME)
Question: Given the root of a binary search tree, and a target K, return two nodes in the tree whose sum equals K.
Example:
Given the following tree and K of 20
10
/ \
5 15
/ \
11 15
Return the nodes 5 and 15.
- [Medium] LC 987. Vertical Order Traversal of a Binary Tree – Given a binary tree, return the vertical order traversal of its nodes’ values. (ie, from top to bottom, column by column). (Try ME)
Question: Given a binary tree, return the vertical order traversal of its nodes’ values. (ie, from top to bottom, column by column).
If two nodes are in the same row and column, the order should be from left to right.
Example 1:
Given binary tree:
3
/ \
9 20
/ \
15 7
return its vertical order traversal as:
[
[9],
[3,15],
[20],
[7]
]
Example 2:
Given binary tree:
_3_
/ \
9 20
/ \ / \
4 5 2 7
return its vertical order traversal as:
[
[4],
[9],
[3,5,2],
[20],
[7]
]
- [Hard] Morris Traversal – Write a program to compute the in-order traversal of a binary tree using
O(1)
space. (Try ME)
O(1)
space. (Try ME)Question: Typically, an implementation of in-order traversal of a binary tree has O(h)
space complexity, where h
is the height of the tree. Write a program to compute the in-order traversal of a binary tree using O(1)
space.
- [Hard] Construct Cartesian Tree from Inorder Traversal – Given in-order traversal, construct a Cartesian tree. A Cartesian tree is heap-ordered, so that each parent value is smaller than children. (Try ME)
Question: A Cartesian tree with sequence S is a binary tree defined by the following two properties:
- It is heap-ordered, so that each parent value is strictly less than that of its children.
- An in-order traversal of the tree produces nodes with values that correspond exactly to S.
Given a sequence S, construct the corresponding Cartesian tree.
Example:
Given the sequence [3, 2, 6, 1, 9], the resulting Cartesian tree would be:
1
/ \
2 9
/ \
3 6
- [Easy] Count Number of Unival Subtrees – A unival tree is a tree where all the nodes have the same value. Given a binary tree, return the number of unival subtrees in the tree. (Try ME)
Question: A unival tree is a tree where all the nodes have the same value. Given a binary tree, return the number of unival subtrees in the tree.
Example 1:
The following tree should return 5:
0
/ \
1 0
/ \
1 0
/ \
1 1
The 5 trees are:
- The three single '1' leaf nodes. (+3)
- The single '0' leaf node. (+1)
- The [1, 1, 1] tree at the bottom. (+1)
Example 2:
Input: root of below tree
5
/ \
1 5
/ \ \
5 5 5
Output: 4
There are 4 subtrees with single values.
Example 3:
Input: root of below tree
5
/ \
4 5
/ \ \
4 4 5
Output: 5
There are five subtrees with single values.
- [Easy] Find Corresponding Node in Cloned Tree – Given two binary trees that are duplicates of one another, and given a node in one tree, find that corresponding node in the second tree. (Try ME)
Question: Given two binary trees that are duplicates of one another, and given a node in one tree, find that corresponding node in the second tree.
There can be duplicate values in the tree (so comparing node1.value == node2.value isn’t going to work).
- [Easy] LC 653. Two Sum in BST – Given the root of a binary search tree, and a target K, return two nodes in the tree whose sum equals K. (Try ME)
Question: Given the root of a binary search tree, and a target K, return two nodes in the tree whose sum equals K.
Example:
Given the following tree and K of 20:
10
/ \
5 15
/ \
11 15
Return the nodes 5 and 15.
- [Medium] LC 230. Kth Smallest Element in a BST – Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. (Try ME)
Question: Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.
Example 1:
Input:
3
/ \
1 4
\
2
k = 1
Output: 1
Example 2:
Input:
5
/ \
3 6
/ \
2 4
/
1
k = 3
Output: 3
- [Medium] Tree Serialization – Given the root of a binary tree. You need to implement 2 functions: Serialize and Deserialize. (Try ME)
Question: You are given the root of a binary tree. You need to implement 2 functions:
serialize(root)
which serializes the tree into a string representationdeserialize(s)
which deserializes the string back to the original tree that it represents
For this problem, often you will be asked to design your own serialization format. However, for simplicity, let’s use the pre-order traversal of the tree.
Example:
1
/ \
3 4
/ \ \
2 5 7
serialize(tree)
# returns "1 3 2 # # 5 # # 4 # 7 # #"
- [Easy] Symmetric K-ary Tree – Given a k-ary tree, figure out if the tree is symmetrical. (Try ME)
Question: Given a k-ary tree, figure out if the tree is symmetrical.
A k-ary tree is a tree with k-children, and a tree is symmetrical if the data of the left side of the tree is the same as the right side of the tree.
Here’s an example of a symmetrical k-ary tree.
4
/ \
3 3
/ | \ / | \
9 4 1 1 4 9
- [Easy] LC 872. Leaf-Similar Trees – Given two trees, whether they are “leaf similar”. Two trees are considered “leaf-similar” if their leaf orderings are the same. (Try ME)
Question: Given two trees, whether they are "leaf similar"
. Two trees are considered "leaf-similar"
if their leaf orderings are the same.
For instance, the following two trees are considered leaf-similar because their leaves are [2, 1]
:
# Tree1
3
/ \
5 1
\
2
# Tree2
7
/ \
2 1
\
2
- [Medium] Construct BST from Post-order Traversal – Given the sequence of keys visited by a postorder traversal of a binary search tree, reconstruct the tree. (Try ME)
Question: Given the sequence of keys visited by a postorder traversal of a binary search tree, reconstruct the tree.
Example:
Given the sequence 2, 4, 3, 8, 7, 5, you should construct the following tree:
5
/ \
3 7
/ \ \
2 4 8
- [Medium] In-order & Post-order Binary Tree Traversal – Given Postorder and Inorder traversals, construct the tree. (Try ME)
Question: Given Postorder and Inorder traversals, construct the tree.
Examples 1:
Input:
in_order = [2, 1, 3]
post_order = [2, 3, 1]
Output:
1
/ \
2 3
Example 2:
Input:
in_order = [4, 8, 2, 5, 1, 6, 3, 7]
post_order = [8, 4, 5, 2, 6, 7, 3, 1]
Output:
1
/ \
2 3
/ \ / \
4 5 6 7
\
8
- [Medium] Pre-order & In-order Binary Tree Traversal – Given pre-order and in-order traversals of a binary tree, write a function to reconstruct the tree. (Try ME)
Question: Given pre-order and in-order traversals of a binary tree, write a function to reconstruct the tree.
For example, given the following preorder traversal:
[a, b, d, e, c, f, g]
And the following inorder traversal:
[d, b, e, a, f, c, g]
You should return the following tree:
a
/ \
b c
/ \ / \
d e f g
Range Query
- [Medium] 24-Hour Hit Counter – You are given an array of length 24, where each element represents the number of new subscribers during the corresponding hour. (Try ME)
Question: You are given an array of length 24, where each element represents the number of new subscribers during the corresponding hour. Implement a data structure that efficiently supports the following:
update(hour: int, value: int)
: Increment the element at index hour by value.query(start: int, end: int)
: Retrieve the number of subscribers that have signed up between start and end (inclusive). You can assume that all values get cleared at the end of the day, and that you will not be asked for start and end values that wrap around midnight.
- [Medium] LC 307. Range Sum Query - Mutable – Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive. (Try ME)
Question: Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.
The update(i, val) function modifies nums by updating the element at index i to val.
Example:
Given nums = [1, 3, 5]
sumRange(0, 2) -> 9
update(1, 2)
sumRange(0, 2) -> 8
- [Hard] LC 308. Range Sum Query 2D - Mutable – Given a 2D matrix matrix, find the sum of the elements inside the rectangle. (Try ME)
Question: Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2).
Example:
Given matrix = [
[3, 0, 1, 4, 2],
[5, 6, 3, 2, 1],
[1, 2, 0, 1, 5],
[4, 1, 0, 1, 7],
[1, 0, 3, 0, 5]
]
sum_region(2, 1, 4, 3) # returns 8
update(3, 2, 2)
sum_region(2, 1, 4, 3) # returns 10
Trie
- [Medium] Forward DNS Look Up Cache – Forward DNS look up is getting IP address for a given domain name typed in the web browser. e.g. Given “www.samsung.com” should return “107.108.11.123” (Try ME)
Question: Forward DNS look up is getting IP address for a given domain name typed in the web browser. e.g. Given “www.samsung.com” should return “107.108.11.123”
The cache should do the following operations:
- Add a mapping from URL to IP address
- Find IP address for a given URL.
Note:
- You can assume all domain name contains only lowercase characters
Hint:
- The idea is to store URLs in Trie nodes and store the corresponding IP address in last or leaf node.
- [Medium] Maximum Distance among Binary Strings – The distance between 2 binary strings is the sum of their lengths after removing the common prefix. (Try ME)
Question: The distance between 2 binary strings is the sum of their lengths after removing the common prefix. For example: the common prefix of 1011000
and 1011110
is 1011
so the distance is len("000") + len("110") = 3 + 3 = 6
.
Given a list of binary strings, pick a pair that gives you maximum distance among all possible pair and return that distance.
- [Easy] Ternary Search Tree – Implement insertion and search functions for a ternary search tree. (Try ME)
Question: A ternary search tree is a trie-like data structure where each node may have up to three children:
- left child nodes link to words lexicographically earlier than the parent prefix
- right child nodes link to words lexicographically later than the parent prefix
- middle child nodes continue the current word
Implement insertion and search functions for a ternary search tree.
Example:
Input: code, cob, be, ax, war, and we.
Output:
c
/ | \
b o w
/ | | |
a e d a
| / | | \
x b e r e
since code is the first word inserted in the tree, and cob lexicographically precedes cod, cob is represented as a left child extending from cod.
- [Medium] Add Bold Tag in String – Takes in a string s and list of substrings lst, and wraps all substrings in s with an HTML bold tag
<b>
and </b>
. (Try ME)
<b>
and </b>
. (Try ME)Question: Implement the function embolden(s, lst)
which takes in a string s and list of substrings lst, and wraps all substrings in s
with an HTML bold tag <b>
and </b>
.
If two bold tags overlap or are contiguous, they should be merged.
Example 1:
Input: s = 'abcdefg', lst = ['bc', 'ef']
Output: 'a<b>bc</b>d<b>ef</b>g'
Example 2:
Input: s = 'abcdefg', lst = ['bcd', 'def']
Output: 'a<b>bcdef</b>g'
Example 3:
Input: s = 'abcxyz123', lst = ['abc', '123']
Output:
'<b>abc</b>xyz<b>123</b>'
Example 4:
Input: s = 'aaabbcc', lst = ['aaa','aab','bc']
Output: "<b>aaabbc</b>c"
- [Hard] LC 30. Substring with Concatenation of All Words – Given a string s and a list of same length words, find all index of substring of s that is permutation of words. (Try ME)
Question: Given a string s
and a list of words words, where each word is the same length, find all starting indices of substrings in s that is a concatenation of every word in words exactly once. The order of the indices does not matter.
Example 1:
Input: s = "dogcatcatcodecatdog", words = ["cat", "dog"]
Output: [0, 13]
Explanation: "dogcat" starts at index 0 and "catdog" starts at index 13.
Example 2:
Input: s = "barfoobazbitbyte", words = ["dog", "cat"]
Output: []
Explanation: there are no substrings composed of "dog" and "cat" in s.
- [Medium] Shortest Unique Prefix – Given an array of words, find all shortest unique prefixes to represent each word in the given array. Assume no word is prefix of another. (Try ME)
Question: Given an array of words, find all shortest unique prefixes to represent each word in the given array. Assume that no word is prefix of another.
Example:
Input: ['zebra', 'dog', 'duck', 'dove']
Output: ['z', 'dog', 'du', 'dov']
Explanation: dog => dog
dove = dov
duck = du
z => zebra
- [Easy] Implement Prefix Map Sum – Implement a PrefixMapSum class with the following methods: insert(key: str, value: int), sum(prefix: str) (Try ME)
Question: Implement a PrefixMapSum class with the following methods:
insert(key: str, value: int)
: Set a given key’s value in the map. If the key already exists, overwrite the value.sum(prefix: str)
: Return the sum of all values of keys that begin with a given prefix.
Example:
mapsum.insert("columnar", 3)
assert mapsum.sum("col") == 3
mapsum.insert("column", 2)
assert mapsum.sum("col") == 5
- [Hard] LC 212. Word Search II – Given an m x n board of characters and a list of strings words, return all words on the board. (Try ME)
Question: Given an m x n board of characters and a list of strings words, return all words on the board.
Each word must be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically neighboring. The same letter cell may not be used more than once in a word.
Example 1:
Input: words = ["oath","pea","eat","rain"], board = [
["o","a","a","n"],
["e","t","a","e"],
["i","h","k","r"],
["i","f","l","v"]]
Output: ["eat", "oath"]
Example 2:
Input: words = ["abcb"], board = [
["a","b"],
["c","d"]]
Output: []
- [Medium] LC 421. Maximum XOR of Two Numbers in an Array – Given an array of integers, find the maximum XOR of any two elements. (Try ME)
Question: Given an array of integers, find the maximum XOR of any two elements.
Example:
Input: nums = [3,10,5,25,2,8]
Output: 28
Explanation: The maximum result is 5 XOR 25 = 28.
- [Medium] Group Words that are Anagrams – Given a list of words, group the words that are anagrams of each other. (An anagram are words made up of the same letters). (Try ME)
Question: Given a list of words, group the words that are anagrams of each other. (An anagram are words made up of the same letters).
Example:
Input: ['abc', 'bcd', 'cba', 'cbd', 'efg']
Output: [['abc', 'cba'], ['bcd', 'cbd'], ['efg']]
- [Medium] Autocompletion – Given a large set of words and then a single word prefix, find all words that it can complete to. (Try ME)
Question: Implement auto-completion. Given a large set of words (for instance 1,000,000 words) and then a single word prefix, find all words that it can complete to.
Example:
class Solution:
def build(self, words):
# Fill this in.
def autocomplete(self, word):
# Fill this in.
s = Solution()
s.build(['dog', 'dark', 'cat', 'door', 'dodge'])
s.autocomplete('do') # Return ['dog', 'door', 'dodge']
Stack
- [Medium] Implement a Quack Using Three Stacks – A quack is a data structure combining properties of both stacks and queues. Implement a quack using three stacks and O(1) additional memory. (Try ME)
Question: A quack is a data structure combining properties of both stacks and queues. It can be viewed as a list of elements written left to right such that three operations are possible:
push(x)
: add a new item x to the left end of the listpop()
: remove and return the item on the left end of the listpull()
: remove the item on the right end of the list.
Implement a quack using three stacks and O(1)
additional memory, so that the amortized time for any push, pop, or pull operation is O(1)
.
- [Medium] LC 224.Basic Calculator – Given a string consisting of parentheses, single digits, and positive and negative signs, convert and evaluate mathematical expression. (Try ME)
Question: Given a string consisting of parentheses, single digits, and positive and negative signs, convert the string into a mathematical expression to obtain the answer.
Don’t use eval or a similar built-in parser.
For example, given '-1 + (2 + 3)'
, you should return 4
.
- [Medium] Evaluate Expression in Reverse Polish Notation – Given an arithmetic expression in Reverse Polish Notation, write a program to evaluate it. (Try ME)
Question: Given an arithmetic expression in Reverse Polish Notation, write a program to evaluate it.
The expression is given as a list of numbers and operands.
Example 1:
[5, 3, '+'] should return 5 + 3 = 8.
Example 2:
[15, 7, 1, 1, '+', '-', '/', 3, '*', 2, 1, 1, '+', '+', '-'] should return 5,
since it is equivalent to ((15 / (7 - (1 + 1))) * 3) - (2 + (1 + 1)) = 5.
- [Medium] Normalize Pathname – Given an absolute pathname that may have . or .. as part of it, return the shortest standardized path. (Try ME)
Question: Given an absolute pathname that may have "."
or ".."
as part of it, return the shortest standardized path.
For example, given "/usr/bin/../bin/./scripts/../"
, return "/usr/bin"
.
- [Medium] Largest Rectangular Area in a Histogram – Given a histogram consisting of rectangles of different heights. Determine the area of the largest rectangle that can be formed. (Try ME)
Question: You are given a histogram consisting of rectangles of different heights. Determine the area of the largest rectangle that can be formed only from the bars of the histogram.
Example:
These heights are represented in an input list, such that [1, 3, 2, 5] corresponds to the following diagram:
x
x
x x
x x x
x x x x
For the diagram above, for example, this would be six, representing the 2 x 3 area at the bottom right.
- [Medium] Interleave Stacks – Given a stack of N elements, interleave the first half of the stack with the second half reversed using only one other queue. (Try ME)
Question: Given a stack of N elements, interleave the first half of the stack with the second half reversed using only one other queue. This should be done in-place.
Recall that you can only push or pop from a stack, and enqueue or dequeue from a queue.
For example, if the stack is [1, 2, 3, 4, 5]
, it should become [1, 5, 2, 4, 3]
. If the stack is [1, 2, 3, 4]
, it should become [1, 4, 2, 3]
.
Hint: Try working backwards from the end state.
- [Medium] Maximum In A Stack – Implement a class for a stack that supports all the regular functions (push, pop) and an additional function of max(). (Try ME)
Question: Implement a class for a stack that supports all the regular functions (push
, pop
) and an additional function of max()
which returns the maximum element in the stack (return None if the stack is empty). Each method should run in constant time.
Example:
s = MaxStack()
s.push(1)
s.push(2)
s.push(3)
s.push(2)
print s.max() # 3
s.pop()
s.pop()
print s.max() # 2
- [Medium] Nearest Larger Number – Given an array of numbers and an index i, return the index of the nearest larger number of the number at index i. (Try ME)
Question: Given an array of numbers and an index i
, return the index of the nearest larger number of the number at index i
, where distance is measured in array indices.
For example, given [4, 1, 3, 5, 6]
and index 0
, you should return 3
.
If two distances to larger numbers are the equal, then return any one of them. If the array at i doesn’t have a nearest larger integer, then return null.
Follow-up: If you can preprocess the array, can you do this in constant time?
- [Easy] LC 1047. Remove Adjacent Duplicate Characters – Given a string, we want to remove 2 adjacent characters that are the same, and repeat the process with the new string until we can no longer (Try ME)
Question: Given a string, we want to remove 2 adjacent characters that are the same, and repeat the process with the new string until we can no longer perform the operation.
Example:
remove_adjacent_dup("cabba")
# Start with cabba
# After remove bb: caa
# After remove aa: c
# Returns c
- [Medium] Index of Larger Next Number – Given a list of numbers, for each element find the next element that is larger than the current element. (Try ME)
Question: Given a list of numbers, for each element find the next element that is larger than the current element. Return the answer as a list of indices. If there are no elements larger than the current element, then use -1 instead.
Example:
larger_number([3, 2, 5, 6, 9, 8])
# return [2, 2, 3, 4, -1, -1]
- [Medium] The Celebrity Problem – Given a list of N people and the above operation, find a way to identify the celebrity in O(N) time. (Try ME)
Question: At a party, there is a single person who everyone knows, but who does not know anyone in return (the “celebrity”). To help figure out who this is, you have access to an O(1)
method called knows(a, b)
, which returns True
if person a
knows person b
, else False
.
Given a list of N
people and the above operation, find a way to identify the celebrity in O(N)
time.
Heap / Priority Queue
- [Easy] Min Steps to Make Piles Equal Height – Determine the minimum number of steps required to make all of the piles equal in height. (Try ME)
Question: Alexa is given n piles of equal or unequal heights. In one step, Alexa can remove any number of boxes from the pile which has the maximum height and try to make it equal to the one which is just lower than the maximum height of the stack. Determine the minimum number of steps required to make all of the piles equal in height.
Example:
Input: piles = [5, 2, 1]
Output: 3
Explanation:
Step 1: reducing 5 -> 2 [2, 2, 1]
Step 2: reducing 2 -> 1 [2, 1, 1]
Step 3: reducing 2 -> 1 [1, 1, 1]
So final number of steps required is 3.
- [Medium] LT 612. K Closest Points – Given some points and a point origin in two dimensional space, find k points out of the some points which are nearest to origin. (Try ME)
Question: Given some points and a point origin in two dimensional space, find k points out of the some points which are nearest to origin.
Return these points sorted by distance, if they are same with distance, sorted by x-axis, otherwise sorted by y-axis.
Example:
Given points = [[4, 6], [4, 7], [4, 4], [2, 5], [1, 1]], origin = [0, 0], k = 3
return [[1, 1], [2, 5], [4, 4]]
- [Easy] Huffman Coding – Huffman coding is a method of encoding characters based on their frequency. (Try ME)
Question: Huffman coding is a method of encoding characters based on their frequency. Each letter is assigned a variable-length binary string, such as 0101
or 111110
, where shorter lengths correspond to more common letters. To accomplish this, a binary tree is built such that the path from the root to any leaf uniquely maps to a character. When traversing the path, descending to a left child corresponds to a 0
in the prefix, while descending right corresponds to 1.
Here is an example tree (note that only the leaf nodes have letters):
*
/ \
* *
/ \ / \
* a t *
/ \
c s
With this encoding, cats would be represented as 0000110111
.
Given a dictionary of character frequencies, build a Huffman tree, and use it to determine a mapping between characters and their encoded binary strings.
- [Medium] LC 274. H-Index – The definition of the h-index is if a scholar has at least h of their papers cited h times. (Try ME)
Question: The h-index is a metric that attempts to measure the productivity and citation impact of the publication of a scholar. The definition of the h-index is if a scholar has at least h of their papers cited h times.
Given a list of publications of the number of citations a scholar has, find their h-index.
Example:
Input: [3, 5, 0, 1, 3]
Output: 3
Explanation:
There are 3 publications with 3 or more citations, hence the h-index is 3.
- [Easy] LT 1859. Minimum Amplitude – Return the smallest amplitude of array A that we can achieve by performing at most three moves. (Try ME)
Question: Given an array A consisting of N integers. In one move, we can choose any element in this array and replace it with any value. The amplitude of an array is the difference between the largest and the smallest values it contains.
Return the smallest amplitude of array A that we can achieve by performing at most three moves.
Example 1:
Input: A = [-9, 8, -1]
Output: 0
Explanation: We can replace -9 and 8 with -1 so that all element are equal to -1, and then the amplitude is 0
Example 2:
Input: A = [14, 10, 5, 1, 0]
Output: 1
Explanation: To achieve an amplitude of 1, we can replace 14, 10 and 5 with 1 or 0.
Example 3:
Input: A = [11, 0, -6, -1, -3, 5]
Output: 3
Explanation: This can be achieved by replacing 11, -6 and 5 with three values of -2.
- [Medium] Merge K Sorted Lists – Given k sorted singly linked lists, write a function to merge all the lists into one sorted singly linked list. (Try ME)
Question: Given k sorted singly linked lists, write a function to merge all the lists into one sorted singly linked list.
- [Medium] Running Median of a Number Stream – Print the running median of a number stream (Try ME)
Question: Compute the running median of a sequence of numbers. That is, given a stream of numbers, print out the median of the list so far on each new element.
Recall that the median of an even-numbered list is the average of the two middle numbers.
For example, given the sequence [2, 1, 5, 7, 2, 0, 5]
, your algorithm should print out:
2
1.5
2
3.5
2
2
2
- [Medium] LC 692. Top K Frequent words – Given a non-empty list of words, return the k most frequent words. (Try ME)
Question: Given a non-empty list of words, return the k most frequent words. The output should be sorted from highest to lowest frequency, and if two words have the same frequency, the word with lower alphabetical order comes first. Input will contain only lower-case letters.
Example 1:
Input: ["i", "love", "leapcode", "i", "love", "coding"], k = 2
Output: ["i", "love"]
Explanation: "i" and "love" are the two most frequent words.
Note that "i" comes before "love" due to a lower alphabetical order.
Example 2:
Input: ["the", "day", "is", "sunny", "the", "the", "the", "sunny", "is", "is"], k = 4
Output: ["the", "is", "sunny", "day"]
Explanation: "the", "is", "sunny" and "day" are the four most frequent words,
with the number of occurrence being 4, 3, 2 and 1 respectively.
- [Medium] Sort a K-Sorted Array – Given a list of N numbers, in which each number is located at most k places away from its sorted position. Sort this array in O(N log k). (Try ME)
Question: You are given a list of N
numbers, in which each number is located at most k
places away from its sorted position. For example, if k = 1
, a given element at index 4
might end up at indices 3
, 4
, or 5
.
Come up with an algorithm that sorts this list in O(N log k) time.
Example:
Input: [3, 2, 6, 5, 4], k=2
Output: [2, 3, 4, 5, 6]
As seen above, every number is at most 2 indexes away from its proper sorted index.
- [Hard] LC 218. City Skyline – Given a list of building in the form of (left, right, height), return what the skyline should look like. (Try ME)
Question: Given a list of building in the form of (left, right, height)
, return what the skyline should look like. The skyline should be in the form of a list of (x-axis, height)
, where x-axis is the point where there is a change in height starting from 0, and height is the new height starting from the x-axis.
Example:
Input: [(2, 8, 3), (4, 6, 5)]
Output: [(2, 3), (4, 5), (7, 3), (9, 0)]
Explanation:
2 2 2
2 2
1 1 2 1 2 1 1
1 2 2 1
1 2 2 1
pos: 0 1 2 3 4 5 6 7 8 9
We have two buildings: one has height 3 and the other 5. The city skyline is just the outline of combined looking.
The result represents the scanned height of city skyline from left to right.
- [Medium] Similar Websites – You are given a list of (website, user) pairs that represent users visiting websites. Come up with a program that identifies the top k pairs (Try ME)
Question: You are given a list of (website, user) pairs that represent users visiting websites. Come up with a program that identifies the top k pairs of websites with the greatest similarity.
Note: The similarity metric bewtween two sets equals intersection / union.
Example:
Suppose k = 1, and the list of tuples is:
[('a', 1), ('a', 3), ('a', 5),
('b', 2), ('b', 6),
('c', 1), ('c', 2), ('c', 3), ('c', 4), ('c', 5),
('d', 4), ('d', 5), ('d', 6), ('d', 7),
('e', 1), ('e', 3), ('e', 5), ('e', 6)]
Then a reasonable similarity metric would most likely conclude that a and e are the most similar, so your program should return [('a', 'e')].
- [Medium] M Smallest in K Sorted Lists – Given k sorted arrays of possibly different sizes, find m-th smallest value in the merged array. (Try ME)
Question: Given k sorted arrays of possibly different sizes, find m-th smallest value in the merged array.
Example 1:
Input: [[1, 3], [2, 4, 6], [0, 9, 10, 11]], m = 5
Output: 4
Explanation: The merged array would be [0, 1, 2, 3, 4, 6, 9, 10, 11].
The 5-th smallest element in this merged array is 4.
Example 2:
Input: [[1, 3, 20], [2, 4, 6]], m = 2
Output: 2
Example 3:
Input: [[1, 3, 20], [2, 4, 6]], m = 6
Output: 20
- [Easy] Largest Product of 3 Elements – You are given an array of integers. Return the largest product that can be made by multiplying any 3 integers in the array. (Try ME)
Question: You are given an array of integers. Return the largest product that can be made by multiplying any 3 integers in the array.
Example:
Input: [-4, -4, 2, 8]
Output: 128
Explanation: the largest product can be made by multiplying -4 * -4 * 8 = 128.
Scheduling
- [Medium] Craft Sentence – Given a sequence of words and an integer line length k, write an algorithm to justify text. (Try ME)
Question: Write an algorithm to justify text. Given a sequence of words and an integer line length k, return a list of strings which represents each line, fully justified.
More specifically, you should have as many words as possible in each line. There should be at least one space between each word. Pad extra spaces when necessary so that each line has exactly length k. Spaces should be distributed as equally as possible, with the extra spaces, if any, distributed starting from the left.
If you can only fit one word on a line, then you should pad the right-hand side with spaces.
Each word is guaranteed not to be longer than k.
For example, given the list of words ["the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"]
and k = 16
, you should return the following:
["the quick brown",
"fox jumps over",
"the lazy dog"]
- [Medium] Rearrange String with Repeated Characters – Given a string with repeated characters, rearrange the string so that no two adjacent characters are the same. (Try ME)
Question: Given a string with repeated characters, rearrange the string so that no two adjacent characters are the same. If this is not possible, return None.
For example, given "aaabbc"
, you could return "ababac"
. Given "aaab"
, return None
.
- [Hard] LC 358. Rearrange String K Distance Apart – Given a non-empty string str and an integer k, rearrange the string such that the same characters are at least distance k from each other. (Try ME)
Question: Given a non-empty string str and an integer k, rearrange the string such that the same characters are at least distance k from each other.
All input strings are given in lowercase letters. If it is not possible to rearrange the string, return an empty string “”.
Example 1:
str = "aabbcc", k = 3
Result: "abcabc"
The same letters are at least distance 3 from each other.
Example 2:
str = "aaabc", k = 3
Answer: ""
It is not possible to rearrange the string.
Example 3:
str = "aaadbbcc", k = 2
Answer: "abacabcd"
Another possible answer is: "abcabcda"
The same letters are at least distance 2 from each other.
- [Medium] LC 621. Task Scheduler – Given a char array representing tasks CPU need to do.You need to return the least number of intervals the CPU will take to finish all tasks. (Try ME)
Question: Given a char array representing tasks CPU need to do. It contains capital letters A to Z where different letters represent different tasks. Tasks could be done without original order. Each task could be done in one interval. For each interval, CPU could finish one task or just be idle.
However, there is a non-negative cooling interval n that means between two same tasks, there must be at least n intervals that CPU are doing different tasks or just be idle.
You need to return the least number of intervals the CPU will take to finish all the given tasks.
Example:
Input: tasks = ["A", "A", "A", "B", "B", "B"], n = 2
Output: 8
Explanation: A -> B -> idle -> A -> B -> idle -> A -> B.
Hashmap
- [Medium] Max Number of Equal Sum Pairs – Given an array A of N integers, returns the maximum possible number of pairs with the same sum. (Try ME)
Question: You are given an array of integers. Your task is to create pairs of them, such that every created pair has the same sum. This sum is NOT specified, but the number of created pairs should be the maximum possible. Each array element may belong to one pair only.
Write a function: public int solution(int[] A)
that given an array A of N integers, returns the maximum possible number of pairs with the same sum.
Example 1:
Input: A = [1, 9, 8, 100, 2]
Return: 2
Explanation: the pairs are [1, 9] and [8, 2] for a sum of 10
Example 2:
Input: A = [2, 2, 2, 3]
Return: 1
Explanation: [2, 2] (sum 4) OR [2, 3] (sum 5).
Notice we can only form sum 4 once since there is overlap between the elements
Example 3:
Input: A = [2, 2, 2, 2, 2]
Return: 2
Explanation: [2, 2] and [2, 2] for a sum for 4.
The fifth 2 is not used, while the first four 2's are used.
- [Medium] LC 525. Largest Subarray with Equal Number of 0s and 1s – Given an array containing only 0s and 1s, find the largest subarray which contain equal number of 0s and 1s. (Try ME)
Question: Given an array containing only 0s and 1s, find the largest subarray which contain equal number of 0s and 1s. Expected time complexity is O(n).
Example 1:
Input: arr[] = [1, 0, 1, 1, 1, 0, 0]
Output: 1 to 6 (Starting and Ending indexes of output subarray)
Example 2:
Input: arr[] = [1, 1, 1, 1]
Output: No such subarray
Example 3:
Input: arr[] = [0, 0, 1, 1, 0]
Output: 0 to 3 Or 1 to 4
- [Medium] Favorite Genres – Given a userMap, return a map, where the key is a username and the value is a list of the user’s favorite genres. (Try ME)
Question: Given a map Map<String, List<String>>
userMap, where the key is a username and the value is a list of user’s songs.
Also given a map Map<String, List<String>>
genreMap, where the key is a genre and the value is a list of songs belonging to this genre.
The task is to return a map Map<String, List<String>>
, where the key is a username and the value is a list of the user’s favorite genres. Favorite genre is a genre with the most song.
Example 1:
Input:
userMap = {
"David": ["song1", "song2", "song3", "song4", "song8"],
"Emma": ["song5", "song6", "song7"]
},
genreMap = {
"Rock": ["song1", "song3"],
"Dubstep": ["song7"],
"Techno": ["song2", "song4"],
"Pop": ["song5", "song6"],
"Jazz": ["song8", "song9"]
}
Output: {
"David": ["Rock", "Techno"],
"Emma": ["Pop"]
}
Explanation:
David has 2 Rock, 2 Techno and 1 Jazz song. So he has 2 favorite genres.
Emma has 2 Pop and 1 Dubstep song. Pop is Emma's favorite genre.
Example 2:
Input:
userMap = {
"David": ["song1", "song2"],
"Emma": ["song3", "song4"]
},
genreMap = {}
Output: {
"David": [],
"Emma": []
}
- [Medium] Numbers With Equal Digit Sum – Find two integers a, b such that sum of digits of a and b is equal. Return maximum sum of a and b. (Try ME)
Question: Given an array containing integers, find two integers a, b such that sum of digits of a and b is equal. Return maximum sum of a and b. Return -1 if no such numbers exist.
Example 1:
Input: [51, 71, 17, 42, 33, 44, 24, 62]
Output: 133
Explanation: Max sum can be formed by 71 + 62 which has same digit sum of 8
Example 2:
Input: [51, 71, 17, 42]
Output: 93
Explanation: Max sum can be formed by 51 + 42 which has same digit sum of 6
Example 3:
Input: [42, 33, 60]
Output: 102
Explanation: Max sum can be formed by 42 + 60 which has same digit sum of 6
Example 4:
Input: [51, 32, 43]
Output: -1
Explanation: There are no 2 numbers with same digit sum
- [Medium] Fixed Order Task Scheduler with Cooldown – Given a list of tasks with a cooldown period, find how long it will take to complete the tasks in the order they are input. (Try ME)
Question: We have a list of tasks to perform, with a cooldown period. We can do multiple of these at the same time, but we cannot run the same task simultaneously.
Given a list of tasks, find how long it will take to complete the tasks in the order they are input.
Example:
tasks = [1, 1, 2, 1]
cooldown = 2
output: 7 (order is 1 _ _ 1 2 _ 1)
- [Easy] Common Characters – Given n strings, find the common characters in all the strings. Display them in alphabetical order. (Try ME)
Question: Given n strings, find the common characters in all the strings. In simple words, find characters that appear in all the strings and display them in alphabetical order or lexicographical order.
Example:
common_characters(['google', 'facebook', 'youtube'])
# ['e', 'o']
- [Easy] Word Ordering in a Different Alphabetical Order – Given a list of words, and an arbitrary alphabetical order, verify that the words are in order of the alphabetical order. (Try ME)
Question: Given a list of words, and an arbitrary alphabetical order, verify that the words are in order of the alphabetical order.
Example 1:
Input:
words = ["abcd", "efgh"]
order="zyxwvutsrqponmlkjihgfedcba"
Output: False
Explanation: 'e' comes before 'a' so 'efgh' should come before 'abcd'
Example 2:
Input:
words = ["zyx", "zyxw", "zyxwy"]
order="zyxwvutsrqponmlkjihgfedcba"
Output: True
Explanation: The words are in increasing alphabetical order
- [Medium] LRU Cache – Design and implement an LRU cache class with the 2 functions ‘put’ and ‘get’. (Try ME)
Question: Implement an LRU (Least Recently Used) cache. It should be able to be initialized with a cache size n, and contain the following methods:
put(key, value)
: sets key to value. If there are already n items in the cache and we are adding a new item, then it should also remove the least recently used item.get(key)
: gets the value at key. If no such key exists, return null.
Each operation should run in O(1) time.
Example:
cache = LRUCache(2)
cache.put(3, 3)
cache.put(4, 4)
cache.get(3) # returns 3
cache.get(2) # returns None
cache.put(2, 2)
cache.get(4) # returns None (pre-empted by 2)
cache.get(3) # returns 3
- [Hard] LFU Cache – Designs and implements data structures that use the least frequently used (LFU) cache. (Try ME)
Question: Implement an LFU (Least Frequently Used) cache. It should be able to be initialized with a cache size n, and contain the following methods:
put(key, value)
: sets key to value. If there are already n items in the cache and we are adding a new item, then it should also remove the least frequently used item. If there is a tie, then the least recently used key should be removed.get(key)
: gets the value at key. If no such key exists, return null. Each operation should run in O(1) time.
- [Medium] Fixed Order Task Scheduler with Cooldown – Given a list of tasks to perform, with a cooldown period. Return execution order. (Try ME)
Question: We have a list of tasks to perform, with a cooldown period. We can do multiple of these at the same time, but we cannot run the same task simultaneously.
Given a list of tasks, find how long it will take to complete the tasks in the order they are input.
Example:
tasks = [1, 1, 2, 1]
cooldown = 2
output: 7 (order is 1 _ _ 1 2 _ 1)
- [Medium] LC 763. Partition Labels – Partition the given string into as many parts as possible so that each letter appears in at most one part. Return length for each part. (Try ME)
Question: A string S of lowercase English letters is given. We want to partition this string into as many parts as possible so that each letter appears in at most one part, and return a list of integers representing the size of these parts.
Example:
Input: S = "ababcbacadefegdehijhklij"
Output: [9, 7, 8]
Explanation: The partition is "ababcbaca", "defegde", "hijhklij".
This is a partition so that each letter appears in at most one part.
A partition like "ababcbacadefegde", "hijhklij" is incorrect, because it splits S into less parts.
- [Medium] LC 1171. Remove Consecutive Nodes that Sum to 0 – Given a linked list of integers, remove all consecutive nodes that sum up to 0. (Try ME)
Question: Given a linked list of integers, remove all consecutive nodes that sum up to 0.
Example 1:
Input: 10 -> 5 -> -3 -> -3 -> 1 -> 4 -> -4
Output: 10
Explanation: The consecutive nodes 5 -> -3 -> -3 -> 1 sums up to 0 so that sequence should be removed. 4 -> -4 also sums up to 0 too so that sequence should also be removed.
Example 2:
Input: 1 -> 2 -> -3 -> 3 -> 1
Output: 3 -> 1
Note: 1 -> 2 -> 1 would also be accepted.
Example 3:
Input: 1 -> 2 -> 3 -> -3 -> 4
Output: 1 -> 2 -> 4
Example 4:
Input: 1 -> 2 -> 3 -> -3 -> -2
Output: 1
- [Medium] LC 560. Subarray Sum Equals K – Given a list of integers and a number K, return which contiguous elements of the list sum to K. (Try ME)
Question: Given a list of integers and a number K
, return which contiguous elements of the list sum to K
.
For example, if the list is [1, 2, 3, 4, 5]
and K
is 9
, then it should return [2, 3, 4]
, since 2 + 3 + 4 = 9
.
Greedy
- [Easy] Max of Min Pairs – Given an array of even length, divide the array into pairs such that the sum of smaller integers in each pair is maximized. (Try ME)
Question: Given an array of length 2 * n (even length)
that consists of random integers, divide the array into pairs such that the sum of smaller integers in each pair is maximized. Return such sum.
Example:
Input: [3, 4, 2, 5]
Ouput: 6
Explanation: The maximum sum of pairs is 6 = min(2, 3) + min(4, 5)
- [Medium] Rescue Boat Problem – If at most 2 people can fit in a rescue boat, and the maximum weight limit for a given boat is k, determine how many boats will be needed to save everyone. (Try ME)
Question: An imminent hurricane threatens the coastal town of Codeville. If at most 2 people can fit in a rescue boat, and the maximum weight limit for a given boat is k
, determine how many boats will be needed to save everyone.
For example, given a population with weights [100, 200, 150, 80]
and a boat limit of 200
, the smallest number of boats required will be three.
- [Medium] LC 134. Gas Station – There are N gas stations along a circular route. Return the starting gas station’s index if you can travel around the circuit once in the clockwise direction. (Try ME)
Question: There are N
gas stations along a circular route, where the amount of gas at station i
is gas[i]
.
You have a car with an unlimited gas tank and it costs cost[i]
of gas to travel from station i
to its next station (i+1)
. You begin the journey with an empty tank at one of the gas stations.
Return the starting gas station’s index if you can travel around the circuit once in the clockwise direction, otherwise return -1
.
Note:
- If there exists a solution, it is guaranteed to be unique.
- Both input arrays are non-empty and have the same length.
- Each element in the input arrays is a non-negative integer.
Example 1:
Input:
gas = [1,2,3,4,5]
cost = [3,4,5,1,2]
Output: 3
Explanation:
Start at station 3 (index 3) and fill up with 4 unit of gas. Your tank = 0 + 4 = 4
Travel to station 4. Your tank = 4 - 1 + 5 = 8
Travel to station 0. Your tank = 8 - 2 + 1 = 7
Travel to station 1. Your tank = 7 - 3 + 2 = 6
Travel to station 2. Your tank = 6 - 4 + 3 = 5
Travel to station 3. The cost is 5. Your gas is just enough to travel back to station 3.
Therefore, return 3 as the starting index.
Example 2:
Input:
gas = [2,3,4]
cost = [3,4,3]
Output: -1
Explanation:
You can't start at station 0 or 1, as there is not enough gas to travel to the next station.
Let's start at station 2 and fill up with 4 unit of gas. Your tank = 0 + 4 = 4
Travel to station 0. Your tank = 4 - 3 + 2 = 3
Travel to station 1. Your tank = 3 - 3 + 3 = 3
You cannot travel back to station 2, as it requires 4 unit of gas but you only have 3.
Therefore, you can't travel around the circuit once no matter where you start.
- [Hard] Stable Marriage Problem – Match the men and women together such that there are no two people of opposite sex who would both rather have each other. (Try ME)
Question: The stable marriage problem is defined as follows:
Suppose you have N
men and N
women, and each person has ranked their prospective opposite-sex partners in order of preference.
For example, if N = 3
, the input could be something like this:
guy_preferences = {
'andrew': ['caroline', 'abigail', 'betty'],
'bill': ['caroline', 'betty', 'abigail'],
'chester': ['betty', 'caroline', 'abigail'],
}
gal_preferences = {
'abigail': ['andrew', 'bill', 'chester'],
'betty': ['bill', 'andrew', 'chester'],
'caroline': ['bill', 'chester', 'andrew']
}
Write an algorithm that pairs the men and women together in such a way that no two people of opposite sex would both rather be with each other than with their current partners.
- [Medium] Minimum Number of Jumps to Reach End – Each element represents max jump. Return the minimum number of jumps you must take in order to get from the start to the end of the array. (Try ME)
Question: You are given an array of integers, where each element represents the maximum number of steps that can be jumped going forward from that element.
Write a function to return the minimum number of jumps you must take in order to get from the start to the end of the array.
For example, given [6, 2, 4, 0, 5, 1, 1, 4, 2, 9]
, you should return 2
, as the optimal solution involves jumping from 6 to 5
, and then from 5 to 9
.
- [Hard] Decreasing Subsequences – Given an int array nums of length n. Split it into strictly decreasing subsequences. Output the min number of subsequences you can get. (Try ME)
Question: Given an int array nums of length n. Split it into strictly decreasing subsequences. Output the min number of subsequences you can get by splitting.
Example 1:
Input: [5, 2, 4, 3, 1, 6]
Output: 3
Explanation:
You can split this array into: [5, 2, 1], [4, 3], [6]. And there are 3 subsequences you get.
Or you can split it into [5, 4, 3], [2, 1], [6]. Also 3 subsequences.
But [5, 4, 3, 2, 1], [6] is not legal because [5, 4, 3, 2, 1] is not a subsuquence of the original array.
Example 2:
Input: [2, 9, 12, 13, 4, 7, 6, 5, 10]
Output: 4
Explanation: [2], [9, 4], [12, 10], [13, 7, 6, 5]
Example 3:
Input: [1, 1, 1]
Output: 3
Explanation: Because of the strictly descending order you have to split it into 3 subsequences: [1], [1], [1]
- [Medium] Minimum Number of Boats to Save Population – If at most two people can fit in a rescue boat, and the maximum weight limit for a given boat is k, determine how many boats will be needed. (Try ME)
Question: An imminent hurricane threatens the coastal town of Codeville. If at most two people can fit in a rescue boat, and the maximum weight limit for a given boat is k, determine how many boats will be needed to save everyone.
For example, given a population with weights [100, 200, 150, 80]
and a boat limit of 200
, the smallest number of boats required will be three.
- [Easy] Mouse Holes – N mice and N holes placed at integer points along a line. Find he largest number of mice-to-holes steps any mouse takes is minimized. (Try ME)
Question: Consider the following scenario: there are N mice and N holes placed at integer points along a line. Given this, find a method that maps mice to holes such that the largest number of steps any mouse takes is minimized.
Each move consists of moving one mouse one unit to the left or right, and only one mouse can fit inside each hole.
For example, suppose the mice are positioned at [1, 4, 9, 15]
, and the holes are located at [10, -5, 0, 16]
. In this case, the best pairing would require us to send the mouse at 1
to the hole at -5
, so our function should return 6
.
- [Hard] Smallest Stab Set – P “stabs” X if every interval in X contains at least one point in P. Compute the smallest set of points that stabs X. (Try ME)
Question: Let X
be a set of n
intervals on the real line. We say that a set of points P
“stabs” X
if every interval in X
contains at least one point in P
. Compute the smallest set of points that stabs X.
For example, given the intervals [(1, 4), (4, 5), (7, 9), (9, 12)]
, you should return [4, 9]
.
- [Medium] LC 1647. Minimum Deletions to Make Character Frequencies Unique – Given a string s, return the minimum number of characters you need to delete to make s good. s is good if no two letters have same count. (Try ME)
Question: A string s is called good if there are no two different characters in s that have the same frequency.
Given a string s, return the minimum number of characters you need to delete to make s good.
The frequency of a character in a string is the number of times it appears in the string. For example, in the string “aab”, the frequency of ‘a’ is 2, while the frequency of ‘b’ is 1.
Example 1:
Input: s = "aab"
Output: 0
Explanation: s is already good.
Example 2:
Input: s = "aaabbbcc"
Output: 2
Explanation: You can delete two 'b's resulting in the good string "aaabcc".
Another way it to delete one 'b' and one 'c' resulting in the good string "aaabbc".
Example 3:
Input: s = "ceabaacb"
Output: 2
Explanation: You can delete both 'c's resulting in the good string "eabaab".
Note that we only care about characters that are still in the string at the end (i.e. frequency of 0 is ignored).
Divide and Conquer
- [Easy] First and Last Indices of an Element in a Sorted Array – Given a sorted array, A, with possibly duplicated elements, find the indices of the first and last occurrences of a target element. (Try ME)
Question: Given a sorted array, A, with possibly duplicated elements, find the indices of the first and last occurrences of a target element, x. Return -1 if the target is not found.
Examples:
Input: A = [1, 3, 3, 5, 7, 8, 9, 9, 9, 15], target = 9
Output: [6, 8]
Input: A = [100, 150, 150, 153], target = 150
Output: [1, 2]
Input: A = [1, 2, 3, 4, 5, 6, 10], target = 9
Output: [-1, -1]
- [Easy] Max and Min with Limited Comparisons – Find the maximum and minimum of the list using less than
2 * (n - 1)
comparisons. (Try ME)
2 * (n - 1)
comparisons. (Try ME)Question: Given a list of numbers of size n
, where n
is greater than 3
, find the maximum and minimum of the list using less than 2 * (n - 1)
comparisons.
Example:
Input: [3, 5, 1, 2, 4, 8]
Output: (1, 8)
- [Medium] LC 240. Search a 2D Matrix II – Write an efficient algorithm that searches for a value in an m x n matrix. (Try ME)
Question: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:
- Integers in each row are sorted in ascending from left to right.
- Integers in each column are sorted in ascending from top to bottom.
Example:
Consider the following matrix:
[
[ 1, 4, 7, 11, 15],
[ 2, 5, 8, 12, 19],
[ 3, 6, 9, 16, 22],
[10, 13, 14, 17, 24],
[18, 21, 23, 26, 30]
]
Given target = 5, return True.
Given target = 20, return False.
- [Medium] Maximum Subarray Sum – Given array that may contain both positive and negative integers, find the sum of contiguous subarray of numbers which has the largest sum. (Try ME)
Question: You are given a one dimensional array that may contain both positive and negative integers, find the sum of contiguous subarray of numbers which has the largest sum.
For example, if the given array is [-2, -5, 6, -2, -3, 1, 5, -6]
, then the maximum subarray sum is 7 as sum of [6, -2, -3, 1, 5]
equals 7
Solve this problem with Divide and Conquer as well as DP separately.
- [Medium] The Tower of Hanoi – The Tower of Hanoi is a puzzle game with three rods.Write a function that prints out all the steps necessary to complete the Tower of Hanoi. (Try ME)
Question: The Tower of Hanoi is a puzzle game with three rods and n disks, each a different size.
All the disks start off on the first rod in a stack. They are ordered by size, with the largest disk on the bottom and the smallest one at the top.
The goal of this puzzle is to move all the disks from the first rod to the last rod while following these rules:
- You can only move one disk at a time.
- A move consists of taking the uppermost disk from one of the stacks and placing it on top of another stack.
- You cannot place a larger disk on top of a smaller disk.
Write a function that prints out all the steps necessary to complete the Tower of Hanoi.
- You should assume that the rods are numbered, with the first rod being 1, the second (auxiliary) rod being 2, and the last (goal) rod being 3.
Example:
with n = 3, we can do this in 7 moves:
Move 1 to 3
Move 1 to 2
Move 3 to 2
Move 1 to 3
Move 2 to 1
Move 2 to 3
Move 1 to 3
Graph
BFS
- [Medium] Max Number of Edges Added to Tree to Stay Bipartite – Maximum number of edges to be added to a tree so that it stays a Bipartite graph. (Try ME)
Question: Maximum number of edges to be added to a tree so that it stays a Bipartite graph
A tree is always a Bipartite Graph as we can always break into two disjoint sets with alternate levels. In other words we always color it with two colors such that alternate levels have same color. The task is to compute the maximum no. of edges that can be added to the tree so that it remains Bipartite Graph.
Example 1:
Input : Tree edges as vertex pairs
1 2
1 3
Output : 0
Explanation :
The only edge we can add is from node 2 to 3.
But edge 2, 3 will result in odd cycle, hence
violation of Bipartite Graph property.
Example 2:
Input : Tree edges as vertex pairs
1 2
1 3
2 4
3 5
Output : 2
Explanation : On colouring the graph, {1, 4, 5}
and {2, 3} form two different sets. Since, 1 is
connected from both 2 and 3, we are left with
edges 4 and 5. Since, 4 is already connected to
2 and 5 to 3, only options remain {4, 3} and
{5, 2}.
- [Medium] Jumping Numbers – Print all jumping numbers smaller than or equal to n. A number is called a jumping number if all adjacent digits in it differ by 1 (Try ME)
Question: Given a positive int n, print all jumping numbers smaller than or equal to n. A number is called a jumping number if all adjacent digits in it differ by 1. For example, 8987 and 4343456 are jumping numbers, but 796 and 89098 are not. All single digit numbers are considered as jumping numbers.
Example:
Input: 105
Output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 21, 23, 32, 34, 43, 45, 54, 56, 65, 67, 76, 78, 87, 89, 98, 101]
- [Easy] ZigZag Binary Tree – Given a binary tree, write an algorithm to print the nodes in zigzag order. (Try ME)
Questions: In Ancient Greece, it was common to write text with the first line going left to right, the second line going right to left, and continuing to go back and forth. This style was called “boustrophedon”.
Given a binary tree, write an algorithm to print the nodes in boustrophedon order.
Example:
Given the following tree:
1
/ \
2 3
/ \ / \
4 5 6 7
You should return [1, 3, 2, 4, 5, 6, 7].
- [Medium] LC 127. Word Ladder – Find the shortest transformation sequence from start to end such that only one letter is changed at each step of the sequence. (Try ME)
Question: Given a start
word, an end
word, and a dictionary of valid words, find the shortest transformation sequence from start
to end
such that only one letter is changed at each step of the sequence, and each transformed word exists in the dictionary. If there is no possible transformation, return null. Each word in the dictionary have the same length as start and end and is lowercase.
For example, given start = "dog"
, end = "cat"
, and dictionary = {"dot", "dop", "dat", "cat"}
, return ["dog", "dot", "dat", "cat"]
.
Given start = "dog"
, end = "cat"
, and dictionary = {"dot", "tod", "dat", "dar"}
, return null
as there is no possible transformation from dog
to cat
.
- [Medium] LC 286. Walls and Gates – Given a m x n 2D grid, fill each empty room with the distance to its nearest gate. (Try ME)
Question: You are given a m x n 2D grid initialized with these three possible values.
- -1 - A wall or an obstacle.
- 0 - A gate.
- INF - Infinity means an empty room. We use the value
2^31 - 1 = 2147483647
to represent INF as you may assume that the distance to a gate is less than 2147483647.
Fill each empty room with the distance to its nearest gate. If it is impossible to reach a gate, it should be filled with INF.
Example:
Given the 2D grid:
INF -1 0 INF
INF INF INF -1
INF -1 INF -1
0 -1 INF INF
After running your function, the 2D grid should be:
3 -1 0 1
2 2 1 -1
1 -1 2 -1
0 -1 3 4
- [Easy] Deepest Node in a Binary Tree – You are given the root of a binary tree. Return the deepest node (the furthest node from the root). (Try ME)
Question: You are given the root of a binary tree. Return the deepest node (the furthest node from the root).
Example:
a
/ \
b c
/
d
The deepest node in this tree is d at depth 3.
- [Medium] Maximum Number of Connected Colors – Given a grid with cells in different colors, find the maximum number of same color cells that are connected. (Try ME)
Question: Given a grid with cells in different colors, find the maximum number of same color cells that are connected.
Note: two cells are connected if they are of the same color and adjacent to each other: left, right, top or bottom. To stay simple, we use integers to represent colors:
The following grid have max 4 connected colors. [color 3: (1, 2), (1, 3), (2, 1), (2, 2)]
[
[1, 1, 2, 2, 3],
[1, 2, 3, 3, 1],
[2, 3, 3, 1, 2]
]
- [Medium] Find All Cousins in Binary Tree – Given a binary tree and a particular node, find all cousins of that node. (Try ME)
Question: Two nodes in a binary tree can be called cousins if they are on the same level of the tree but have different parents.
Given a binary tree and a particular node, find all cousins of that node.
Example:
In the following diagram 4 and 6 are cousins:
1
/ \
2 3
/ \ \
4 5 6
- [Hard] Longest Path in Binary Tree – Given a binary tree, return any of the longest path. (Try ME)
Question: Given a binary tree, return any of the longest path.
Example 1:
Input: 1
/ \
2 3
/ \
4 5
Output: [4, 2, 1, 3] or [5, 2, 1, 3]
Example 2:
Input: 1
/ \
2 3
/ \ \
4 5 6
Output: [4, 2, 1, 3, 6] or [5, 2, 1, 3, 6]
Level-based BFS
- [Easy] Grid Path – Given this matrix, a start coordinate, and an end coordinate, return the minimum number of steps required to reach the end coordinate from the start. (Try ME)
Question: You are given an M by N matrix consisting of booleans that represents a board. Each True boolean represents a wall. Each False boolean represents a tile you can walk on.
Given this matrix, a start coordinate, and an end coordinate, return the minimum number of steps required to reach the end coordinate from the start. If there is no possible path, then return null. You can move up, left, down, and right. You cannot move through walls. You cannot wrap around the edges of the board.
Example:
Given the following grid:
[
[F, F, F, F],
[T, T, F, T],
[F, F, F, F],
[F, F, F, F]
]
and start = (3, 0) (bottom left) and end = (0, 0) (top left),
The minimum number of steps required to reach the end is 7, since we would need to go through (1, 2) because there is a wall everywhere else on the second row.
- [Easy] Zombie in Matrix – Zombies can turn adjacent human beings into zombies every hour. Find out how many hours does it take to infect all humans? (Try ME)
Question: Given a 2D grid, each cell is either a zombie 1 or a human 0. Zombies can turn adjacent (up/down/left/right) human beings into zombies every hour. Find out how many hours does it take to infect all humans?
Example:
Input:
[[0, 1, 1, 0, 1],
[0, 1, 0, 1, 0],
[0, 0, 0, 0, 1],
[0, 1, 0, 0, 0]]
Output: 2
Explanation:
At the end of the 1st hour, the status of the grid:
[[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[0, 1, 0, 1, 1],
[1, 1, 1, 0, 1]]
At the end of the 2nd hour, the status of the grid:
[[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1]]
- [Easy] Minimum Depth of Binary Tree – * Given a binary tree, find the minimum depth of the binary tree. The minimum depth is the shortest distance from the root to a leaf.* (Try ME)
Question: Given a binary tree, find the minimum depth of the binary tree. The minimum depth is the shortest distance from the root to a leaf.
Example:
Input:
1
/ \
2 3
\
4
Output: 2
- [Medium] Minimum Number of Operations – Given a number x and a number y, find the minimum number of operations needed to go from x to y. (Try ME)
Question: You are only allowed to perform 2 operations:
- either multiply a number by 2;
- or subtract a number by 1.
Given a number x
and a number y
, find the minimum number of operations needed to go from x
to y
.
- [Hard] LC 934. Shortest Bridge – In a given 2D binary array A, there are two islands.Return the smallest number of 0s that must be flipped. (Try ME)
Question: In a given 2D binary array A, there are two islands. (An island is a 4-directionally connected group of 1s not connected to any other 1s.)
Now, we may change 0s to 1s so as to connect the two islands together to form 1 island.
Return the smallest number of 0s that must be flipped. (It is guaranteed that the answer is at least 1.)
Example 1:
Input:
[
[0, 1],
[1, 0]
]
Output: 1
Example 2:
Input:
[
[0, 1, 0],
[0, 0, 0],
[0, 0, 1]
]
Output: 2
Example 3:
Input:
[
[1, 1, 1, 1, 1],
[1, 0, 0, 0, 1],
[1, 0, 1, 0, 1],
[1, 0, 0, 0, 1],
[1, 1, 1, 1, 1]
]
Output: 1
DFS
- [Medium] Friend Cycle Problem – Given a friendship list, determine the number of friend groups in the class. (Try ME)
Question: A classroom consists of N
students, whose friendships can be represented in an adjacency list. For example, the following descibes a situation where 0
is friends with 1
and 2
, 3
is friends with 6
, and so on.
{0: [1, 2],
1: [0, 5],
2: [0],
3: [6],
4: [],
5: [1],
6: [3]}
Each student can be placed in a friend group, which can be defined as the transitive closure of that student’s friendship relations. In other words, this is the smallest set such that no student in the group has any friends outside this group. For the example above, the friend groups would be {0, 1, 2, 5}
, {3, 6}
, {4}
.
Given a friendship list such as the one above, determine the number of friend groups in the class.
- [Medium] Unit Converter – Create a data structure that can efficiently convert a certain quantity of one unit to the correct amount of any other unit. (Try ME)
Question: The United States uses the imperial system of weights and measures, which means that there are many different, seemingly arbitrary units to measure distance. There are 12 inches in a foot, 3 feet in a yard, 22 yards in a chain, and so on.
Create a data structure that can efficiently convert a certain quantity of one unit to the correct amount of any other unit. You should also allow for additional units to be added to the system.
- [Medium] LC 133. Deep Copy Graph – Given a node in a connected directional graph, create a deep copy of it. (Try ME)
Question: Given a node in a connected directional graph, create a deep copy of it.
Each node in the graph contains a val (int) and a list (List[Node]) of its neighbors:
class Node(object):
def __init__(self, val, neighbors=None):
self.val = val
self.neighbors = neighbors
- [Medium] Strongly Connected Directed Graph – Given a directed graph, find out whether the graph is strongly connected or not. (Try ME)
Question: Given a directed graph, find out whether the graph is strongly connected or not. A directed graph is strongly connected if there is a path between any two pair of vertices.
Example:
is_SCDG(vertices=5, edges=[(0, 1), (1, 2), (2, 3), (3, 0), (2, 4), (4, 2)]) # returns True
is_SCDG(vertices=4, edges=[(0, 1), (1, 2), (2, 3)]) # returns False
- [Hard] LC 317. Shortest Distance from All Buildings – You want to build a house on an empty land which reaches all buildings in the shortest amount of distance. (Try ME)
Question: You want to build a house on an empty land which reaches all buildings in the shortest amount of distance. You can only move up, down, left and right. You are given a 2D grid of values 0, 1 or 2, where:
- Each 0 marks an empty land which you can pass by freely.
- Each 1 marks a building which you cannot pass through.
- Each 2 marks an obstacle which you cannot pass through.
Note: There will be at least one building. If it is not possible to build such house according to the above rules, return -1.
Example:
Input: [[1,0,2,0,1],[0,0,0,0,0],[0,0,1,0,0]]
1 - 0 - 2 - 0 - 1
| | | | |
0 - 0 - 0 - 0 - 0
| | | | |
0 - 0 - 1 - 0 - 0
Output: 7
Explanation: Given three buildings at (0,0), (0,4), (2,2), and an obstacle at (0,2),
the point (1,2) is an ideal empty land to build a house, as the total
travel distance of 3+3+1=7 is minimal. So return 7.
- [Medium] Maximum Edge Removal to Make Even Forest – Write a function that returns the maximum number of edges you can remove while still satisfying even number of children for each node. (Try ME)
Questions: You are given a tree with an even number of nodes. Consider each connection between a parent and child node to be an “edge”. You would like to remove some of these edges, such that the disconnected subtrees that remain each have an even number of nodes.
For example, suppose your input was the following tree:
1
/ \
2 3
/ \
4 5
/ | \
6 7 8
In this case, removing the edge (3, 4) satisfies our requirement.
Write a function that returns the maximum number of edges you can remove while still satisfying this requirement.
- [Medium] LC 417 Pacific Atlantic Water Flow – Return a list of grid coordinates where water can flow to both the Pacific and Atlantic oceans. (Try ME)
Question: You are given an m x n integer matrix heights representing the height of each unit cell in a continent. The Pacific ocean touches the continent’s left and top edges, and the Atlantic ocean touches the continent’s right and bottom edges.
Water can only flow in four directions: up, down, left, and right. Water flows from a cell to an adjacent one with an equal or lower height.
Return a list of grid coordinates where water can flow to both the Pacific and Atlantic oceans.
Example 1:
Input: heights = [
[1,2,2,3,5],
[3,2,3,4,4],
[2,4,5,3,1],
[6,7,1,4,5],
[5,1,1,2,4]]
Output: [[0,4],[1,3],[1,4],[2,2],[3,0],[3,1],[4,0]]
Example 2:
Input: heights = [
[2,1],
[1,2]]
Output: [[0,0],[0,1],[1,0],[1,1]]
- [Medium] LC 1448. Count Good Nodes in Binary Tree – Given a binary tree root, a node X in the tree is named good if in the path from root to X there are no nodes with a value greater than X. (Try ME)
Question: Given a binary tree root, a node X in the tree is named good if in the path from root to X there are no nodes with a value greater than X.
Return the number of good nodes in the binary tree.
Example:
Input:
3
/ \
1 4
/ / \
3 1 5
Output: 4
Root Node (3) is always a good node.
Node 4 -> (3,4) is the maximum value in the path starting from the root.
Node 5 -> (3,4,5) is the maximum value in the path
Node 3 -> (3,1,3) is the maximum value in the path.
- [Medium] LC 114. Flatten Binary Tree to Linked List – Given a binary tree, flatten it to a linked list in-place. (Try ME)
Question: Given a binary tree, flatten it to a linked list in-place.
For example, given the following tree:
1
/ \
2 5
/ \ \
3 4 6
The flattened tree should look like:
1
\
2
\
3
\
4
\
5
\
6
- [Easy] Target Sum from Root to Leaf – Given a binary tree, and a target number, find if there is a path from the root to any leaf that sums up to the target. (Try ME)
Question: Given a binary tree, and a target number, find if there is a path from the root to any leaf that sums up to the target.
Example:
Input: target = 9
1
/ \
2 3
\ \
6 4
Expected: True
Explanation: path 1 -> 2 -> 6 sum up to 9
- [Hard] Critical Routers (Articulation Point) – Given an undirected connected graph, find all articulation points in the given graph. (Try ME)
Question: You are given an undirected connected graph. An articulation point (or cut vertex) is defined as a vertex which, when removed along with associated edges, makes the graph disconnected (or more precisely, increases the number of connected components in the graph). The task is to find all articulation points in the given graph.
Example1:
Input: vertices = 4, edges = [[0, 1], [1, 2], [2, 3]]
Output: [1, 2]
Explanation:
Removing either vertex 0 or 3 along with edges [0, 1] or [2, 3] does not increase number of connected components.
But removing 1, 2 breaks graph into two components.
Example2:
Input: vertices = 5, edges = [[0, 1], [0, 2], [1, 2], [0, 3], [3, 4]]
Output: [0, 3]
- [Medium] LC 332. Reconstruct Itinerary – Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], reconstruct the itinerary in order. (Try ME)
Questions: Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], reconstruct the itinerary in order. All of the tickets belong to a man who departs from JFK. Thus, the itinerary must begin with JFK.
Note:
- If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string. For example, the itinerary [“JFK”, “LGA”] has a smaller lexical order than [“JFK”, “LGB”].
- All airports are represented by three capital letters (IATA code).
- You may assume all tickets form at least one valid itinerary.
Example 1:
Input: [["MUC", "LHR"], ["JFK", "MUC"], ["SFO", "SJC"], ["LHR", "SFO"]]
Output: ["JFK", "MUC", "LHR", "SFO", "SJC"]
Example 2:
Input: [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]
Output: ["JFK","ATL","JFK","SFO","ATL","SFO"]
Explanation: Another possible reconstruction is ["JFK","SFO","ATL","JFK","ATL","SFO"].
But it is larger in lexical order.
- [Medium] Direction and Position Rule Verification – Given a list of rules, check if the sum of the rules validate. (Try ME)
Question: A rule looks like this:
A NE B
This means this means point A is located northeast of point B.
A SW C
means that point A is southwest of C.
Given a list of rules, check if the sum of the rules validate. For example:
A N B
B NE C
C N A
does not validate, since A cannot be both north and south of C.
A NW B
A N B
is considered valid.
- [Easy] Count Visible Nodes in Binary Tree – In a binary tree, if no node with greater value than A’s along the path, this node is visible. Count total number of such nodes. (Try ME)
Question: In a binary tree, if in the path from root to the node A, there is no node with greater value than A’s, this node A is visible. We need to count the number of visible nodes in a binary tree.
Example 1:
Input:
5
/ \
3 10
/ \ /
20 21 1
Output: 4
Explanation: There are 4 visible nodes: 5, 20, 21, and 10.
Example 2:
Input:
-10
\
-15
\
-1
Output: 2
Explanation: Visible nodes are -10 and -1.
- [Medium] Isolated Islands – Given a matrix of 1s and 0s, return the number of “islands” in the matrix. (Try ME)
Question: Given a matrix of 1s and 0s, return the number of “islands” in the matrix. A 1 represents land and 0 represents water, so an island is a group of 1s that are neighboring whose perimeter is surrounded by water.
For example, this matrix has 4 islands.
1 0 0 0 0
0 0 1 1 0
0 1 1 0 0
0 0 0 0 0
1 1 0 0 1
1 1 0 0 1
- [Easy] Largest Path Sum from Root To Leaf – Given a binary tree, find and return the largest path from root to leaf. (Try ME)
Question: Given a binary tree, find and return the largest path from root to leaf.
Example:
Input:
1
/ \
3 2
\ /
5 4
Output: [1, 3, 5]
- [Hard] LC 403. Frog Jump – A frog is crossing a river. The river is divided into x units and at each unit there may or may not exist a stone. (Try ME)
Question: A frog is crossing a river. The river is divided into x units and at each unit there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.
Given a list of stones’ positions (in units) in sorted ascending order, determine if the frog is able to cross the river by landing on the last stone. Initially, the frog is on the first stone and assume the first jump must be 1 unit.
If the frog’s last jump was k units, then its next jump must be either k - 1, k, or k + 1 units. Note that the frog can only jump in the forward direction.
Example 1:
[0, 1, 3, 5, 6, 8, 12, 17]
There are a total of 8 stones.
The first stone at the 0th unit, second stone at the 1st unit,
third stone at the 3rd unit, and so on...
The last stone at the 17th unit.
Return true. The frog can jump to the last stone by jumping
1 unit to the 2nd stone, then 2 units to the 3rd stone, then
2 units to the 4th stone, then 3 units to the 6th stone,
4 units to the 7th stone, and 5 units to the 8th stone.
Example 2:
[0, 1, 2, 3, 4, 8, 9, 11]
Return false. There is no way to jump to the last stone as
the gap between the 5th and 6th stone is too large.
- [Hard] De Bruijn Sequence – Given a set of characters C and an integer k, find a De Bruijn Sequence. (Try ME)
Question: Given a set of characters C
and an integer k
, a De Bruijn Sequence is a cyclic sequence in which every possible k-length string of characters in C occurs exactly once.
Background: De Bruijn Sequence can be used to shorten a brute-force attack on a PIN-like code lock that does not have an “enter” key and accepts the last n digits entered. For example, a digital door lock with a 4-digit code would have B (10, 4) solutions, with length 10000. Therefore, only at most 10000 + 3 = 10003 (as the solutions are cyclic) presses are needed to open the lock. Trying all codes separately would require 4 × 10000 = 40000 presses.
Example1:
Input: C = [0, 1], k = 3
Output: 0011101000
All possible strings of length three (000, 001, 010, 011, 100, 101, 110 and 111) appear exactly once as sub-strings in C.
Example2:
Input: C = [0, 1], k = 2
Output: 01100
- [Easy] Binary Tree Level Sum – Calculate the sum of the nodes in the target level. (Try ME)
Question: Given a binary tree and an integer which is the depth of the target level. Calculate the sum of the nodes in the target level.
- [Easy] Level of tree with Maximum Sum – Given a binary tree, find the level in the tree where the sum of all nodes on that level is the greatest. (Try ME)
Question: Given a binary tree, find the level in the tree where the sum of all nodes on that level is the greatest.
Example:
The following tree should return level 1:
1 Level 0 - Sum: 1
/ \
4 5 Level 1 - Sum: 9
/ \ / \
3 2 4 -1 Level 2 - Sum: 8
- [Easy] Flatten Nested List Iterator – Implement a 2D iterator class. It will be initialized with an array of arrays, and should implement the following methods: next(), hasNext() (Try ME)
Question: Implement a 2D iterator class. It will be initialized with an array of arrays, and should implement the following methods:
next()
: returns the next element in the array of arrays. If there are no more elements, raise an exception.has_next()
: returns whether or not the iterator still has elements left.
For example, given the input [[1, 2], [3], [], [4, 5, 6]]
, calling next()
repeatedly should output 1, 2, 3, 4, 5, 6
.
Do not use flatten or otherwise clone the arrays. Some of the arrays can be empty.
- [Easy] Flatten a Nested Dictionary – Write a function to flatten a nested dictionary. Namespace the keys with a period. (Try ME)
Question: Write a function to flatten a nested dictionary. Namespace the keys with a period.
Example:
Given the following dictionary:
{
"key": 3,
"foo": {
"a": 5,
"bar": {
"baz": 8
}
}
}
it should become:
{
"key": 3,
"foo.a": 5,
"foo.bar.baz": 8
}
You can assume keys do not contain dots in them, i.e. no clobbering will occur.
- [Medium] Circle of Chained Words – Given a list of words, determine if there is a way to ‘chain’ all the words in a circle. (Try ME)
Question: Two words can be ‘chained’ if the last character of the first word is the same as the first character of the second word.
Given a list of words, determine if there is a way to ‘chain’ all the words in a circle.
Example:
Input: ['eggs', 'karat', 'apple', 'snack', 'tuna']
Output: True
Explanation:
The words in the order of ['apple', 'eggs', 'snack', 'karat', 'tuna'] creates a circle of chained words.
Topological Sort
- [Hard] Longest Path in A Directed Acyclic Graph – Given a Directed Acyclic Graph (DAG), find the longest distances in the given graph. (Try ME)
Question: Given a Directed Acyclic Graph (DAG), find the longest distances in the given graph.
Note: The longest path problem for a general graph is not as easy as the shortest path problem because the longest path problem doesn’t have optimal substructure property. In fact, the Longest Path problem is NP-Hard for a general graph. However, the longest path problem has a linear time solution for directed acyclic graphs. The idea is similar to linear time solution for shortest path in a directed acyclic graph. We use Topological Sorting.
Example:
# Following returns 3, as longest path is: 5, 2, 3, 1
longest_path_in_DAG(vertices=6, edges=[(5, 2), (5, 0), (4, 0), (4, 1), (2, 3), (3, 1)])
- [Hard] Max Path Value in Directed Graph – Given a directed graph, return the largest value path of the graph. Define a path’s value as the number of most frequent letters along path. (Try ME)
Question: In a directed graph, each node is assigned an uppercase letter. We define a path’s value as the number of most frequently-occurring letter along that path. For example, if a path in the graph goes through “ABACA”, the value of the path is 3, since there are 3 occurrences of ‘A’ on the path.
Given a graph with n nodes and m directed edges, return the largest value path of the graph. If the largest value is infinite, then return null.
The graph is represented with a string and an edge list. The i-th character represents the uppercase letter of the i-th node. Each tuple in the edge list (i, j) means there is a directed edge from the i-th node to the j-th node. Self-edges are possible, as well as multi-edges.
Example1:
Input: "ABACA", [(0, 1), (0, 2), (2, 3), (3, 4)]
Output: 3
Explanation: maximum value 3 using the path of vertices [0, 2, 3, 4], ie. A -> A -> C -> A.
Example2:
Input: "A", [(0, 0)]
Output: None
Explanation: we have an infinite loop.
- [Medium] Satisfactory Playlist – Given a set of these ranked lists, interleave them to create a playlist that satisfies everyone’s priorities. (Try ME)
Question: You have access to ranked lists of songs for various users. Each song is represented as an integer, and more preferred songs appear earlier in each list. For example, the list [4, 1, 7]
indicates that a user likes song 4
the best, followed by songs 1
and 7
.
Given a set of these ranked lists, interleave them to create a playlist that satisfies everyone’s priorities.
For example, suppose your input is [[1, 7, 3], [2, 1, 6, 7, 9], [3, 9, 5]]
. In this case a satisfactory playlist could be [2, 1, 6, 7, 3, 9, 5]
.
- [Hard] Order of Alien Dictionary – Give a dictionary of sorted words in an alien language, returns the correct order of letters in this language. (Try ME)
Question: You come across a dictionary of sorted words in a language you’ve never seen before. Write a program that returns the correct order of letters in this language.
For example, given ['xww', 'wxyz', 'wxyw', 'ywx', 'ywz']
, you should return ['x', 'z', 'w', 'y']
.
- [Hard] Order of Course Prerequisites – Given a hashmap of courseId key to a list of courseIds values. Return a sorted ordering of courses such that we can finish all courses. (Try ME)
We’re given a hashmap associating each courseId key with a list of courseIds values, which represents that the prerequisites of courseId are courseIds. Return a sorted ordering of courses such that we can finish all courses.
Return null if there is no such ordering.
For example, given {'CSC300': ['CSC100', 'CSC200'], 'CSC200': ['CSC100'], 'CSC100': []}
, should return ['CSC100', 'CSC200', 'CSC300']
.
- [Hard] LC 1136. Parallel Courses – Given prerequisite relationship between courses. Return the minimum number of semesters needed to study all courses. (Try ME)
Question: There are N courses, labelled from 1 to N.
We are given relations[i] = [X, Y]
, representing a prerequisite relationship between course X and course Y: course X has to be studied before course Y.
In one semester you can study any number of courses as long as you have studied all the prerequisites for the course you are studying.
Return the minimum number of semesters needed to study all courses. If there is no way to study all the courses, return -1.
Example 1:
Input: N = 3, relations = [[1,3],[2,3]]
Output: 2
Explanation:
In the first semester, courses 1 and 2 are studied. In the second semester, course 3 is studied.
Example 2:
Input: N = 3, relations = [[1,2],[2,3],[3,1]]
Output: -1
Explanation:
No course can be studied because they depend on each other.
- [Hard] LC 329. Longest Increasing Path in a Matrix – Given an integer matrix, find the length of the longest increasing path. (Try ME)
Question: Given an integer matrix, find the length of the longest increasing path.
From each cell, you can either move to four directions: left, right, up or down. You may NOT move diagonally or move outside of the boundary (i.e. wrap-around is not allowed).
Example 1:
Input: nums =
[
[9,9,4],
[6,6,8],
[2,1,1]
]
Output: 4
Explanation: The longest increasing path is [1, 2, 6, 9].
Example 2:
Input: nums =
[
[3,4,5],
[3,2,6],
[2,2,1]
]
Output: 4
Explanation: The longest increasing path is [3, 4, 5, 6]. Moving diagonally is not allowed.
- [Hard] Shortest Uphill and Downhill Route – A runner decide the route goes entirely uphill at first, and then entirely downhill. Find the length of the shortest of such route. (Try ME)
Question: A competitive runner would like to create a route that starts and ends at his house, with the condition that the route goes entirely uphill at first, and then entirely downhill.
Given a dictionary of places of the form {location: elevation}
, and a dictionary mapping paths between some of these locations to their corresponding distances, find the length of the shortest route satisfying the condition above. Assume the runner’s home is location 0
.
Example:
Suppose you are given the following input:
elevations = {0: 5, 1: 25, 2: 15, 3: 20, 4: 10}
paths = {
(0, 1): 10,
(0, 2): 8,
(0, 3): 15,
(1, 3): 12,
(2, 4): 10,
(3, 4): 5,
(3, 0): 17,
(4, 0): 10
}
In this case, the shortest valid path would be 0 -> 2 -> 4 -> 0, with a distance of 28.
Union Find
- [Medium] LC 684. Redundant Connection – Given a graph that started as a tree with N nodes, return an edge that can be removed. (Try ME)
Question: In this problem, a tree is an undirected graph that is connected and has no cycles.
The given input is a graph that started as a tree with N nodes (with distinct values 1, 2, …, N), with one additional edge added. The added edge has two different vertices chosen from 1 to N, and was not an edge that already existed.
The resulting graph is given as a 2D-array of edges. Each element of edges is a pair [u, v] with u < v, that represents an undirected edge connecting nodes u and v.
Return an edge that can be removed so that the resulting graph is a tree of N nodes. If there are multiple answers, return the answer that occurs last in the given 2D-array. The answer edge [u, v] should be in the same format, with u < v.
Example 1:
Input: [[1,2], [1,3], [2,3]]
Output: [2,3]
Explanation: The given undirected graph will be like this:
1
/ \
2 - 3
Example 2:
Input: [[1,2], [2,3], [3,4], [1,4], [1,5]]
Output: [1,4]
Explanation: The given undirected graph will be like this:
5 - 1 - 2
| |
4 - 3
- [Hard] LC 685. Redundant Connection II – Given a directed graph that started as a rooted tree with one additional directed edge added. Return that edge that can be removed. (Try ME)
Questions: In this problem, a rooted tree is a directed graph such that, there is exactly one node (the root) for which all other nodes are descendants of this node, plus every node has exactly one parent, except for the root node which has no parents.
The given input is a directed graph that started as a rooted tree with N nodes (with distinct values 1, 2, ..., N
), with one additional directed edge added. The added edge has two different vertices chosen from 1 to N, and was not an edge that already existed.
The resulting graph is given as a 2D-array of edges. Each element of edges is a pair [u, v]
that represents a directed edge connecting nodes u
and v
, where u
is a parent of child v
.
Return an edge that can be removed so that the resulting graph is a rooted tree of N
nodes. If there are multiple answers, return the answer that occurs last in the given 2D-array.
Example 1:
Input: [[1,2], [1,3], [2,3]]
Output: [2,3]
Explanation: The given directed graph will be like this:
1
/ \
v v
2-->3
Example 2:
Input: [[1,2], [2,3], [3,4], [4,1], [1,5]]
Output: [4,1]
Explanation: The given directed graph will be like this:
5 <- 1 -> 2
^ |
| v
4 <- 3
- [Medium] LT 434. Number of Islands II – Given a 2D matrix and an array of pair represents position of new island, return how many island are there in the matrix after each operator. (Try ME)
Question: Given a n,m
which means the row and column of the 2D matrix and an array of pair A( size k)
. Originally, the 2D matrix is all 0 which means there is only sea in the matrix. The list pair has k
operator and each operator has two integer A[i].x, A[i].y
means that you can change the grid matrix[A[i].x][A[i].y]
from sea to island.
Return how many island are there in the matrix after each operator. You need to return an array of size K
.
Example 1:
Input: n = 4, m = 5, A = [[1,1],[0,1],[3,3],[3,4]]
Output: [1,1,2,2]
Explanation:
0. 00000
00000
00000
00000
1. 00000
01000
00000
00000
2. 01000
01000
00000
00000
3. 01000
01000
00000
00010
4. 01000
01000
00000
00011
Example 2:
Input: n = 3, m = 3, A = [[0,0],[0,1],[2,2],[2,1]]
Output: [1,1,2,2]
- [Hard] Teams without Enemies – A teacher must divide a class of students into two teams to play dodgeball. Write an algorithm that finds a satisfactory pair of teams. (Try ME)
Question: A teacher must divide a class of students into two teams to play dodgeball. Unfortunately, not all the kids get along, and several refuse to be put on the same team as that of their enemies.
Given an adjacency list of students and their enemies, write an algorithm that finds a satisfactory pair of teams, or returns False if none exists.
Example 1:
Given the following enemy graph you should return the teams {0, 1, 4, 5} and {2, 3}.
students = {
0: [3],
1: [2],
2: [1, 4],
3: [0, 4, 5],
4: [2, 3],
5: [3]
}
Example 2:
On the other hand, given the input below, you should return False.
students = {
0: [3],
1: [2],
2: [1, 3, 4],
3: [0, 2, 4, 5],
4: [2, 3],
5: [3]
}
- [Hard] Maximum Spanning Tree – Given an undirected graph with weighted edges, compute the maximum weight spanning tree. (Try ME)
Question: Recall that the minimum spanning tree is the subset of edges of a tree that connect all its vertices with the smallest possible total edge weight.
Given an undirected graph with weighted edges, compute the maximum weight spanning tree.
- [Medium] Number of Connected Components – Given a list of undirected edges which represents a graph, find out the number of connected components. (Try ME)
Question: Given a list of undirected edges which represents a graph, find out the number of connected components.
Example:
Input: [(1, 2), (2, 3), (4, 1), (5, 6)]
Output: 2
Explanation: In the above example, vertices 1, 2, 3, 4 are all connected, and 5, 6 are connected, and thus there are 2 connected components in the graph above.
- [Medium] Friend Cycle Problem – Given a friendship list, determine the number of friend groups in the class. (Try ME)
Question: A classroom consists of N
students, whose friendships can be represented in an adjacency list. For example, the following descibes a situation where 0
is friends with 1
and 2
, 3
is friends with 6
, and so on.
{0: [1, 2],
1: [0, 5],
2: [0],
3: [6],
4: [],
5: [1],
6: [3]}
Each student can be placed in a friend group, which can be defined as the transitive closure of that student’s friendship relations. In other words, this is the smallest set such that no student in the group has any friends outside this group. For the example above, the friend groups would be {0, 1, 2, 5}
, {3, 6}
, {4}
.
Given a friendship list such as the one above, determine the number of friend groups in the class.
- [Hard] Power Supply to All Cities – Given a graph of possible electricity connections(each with their own cost)between cities in an area, find the cheapest way to supply power. (Try ME)
Question: Given a graph of possible electricity connections (each with their own cost) between cities in an area, find the cheapest way to supply power to all cities in the area.
Example 1:
Input: cities = ['Vancouver', 'Richmond', 'Burnaby']
cost_btw_cities = [
('Vancouver', 'Richmond', 1),
('Vancouver', 'Burnaby', 1),
('Richmond', 'Burnaby', 2)]
Output: 2
Explanation:
Min cost to supply all cities is to connect the following cities with total cost 1 + 1 = 2:
(Vancouver, Burnaby), (Vancouver, Richmond)
Example 2:
Input: cities = ['Toronto', 'Mississauga', 'Waterloo', 'Hamilton']
cost_btw_cities = [
('Mississauga', 'Toronto', 1),
('Toronto', 'Waterloo', 2),
('Waterloo', 'Hamilton', 3),
('Toronto', 'Hamilton', 2),
('Mississauga', 'Hamilton', 1),
('Mississauga', 'Waterloo', 2)]
Output: 4
Explanation: Min cost to connect to all cities is 4:
(Toronto, Mississauga), (Toronto, Waterloo), (Mississauga, Hamilton)
- [Medium] LC 130. Surrounded Regions – A region is captured by flipping all ‘O’s into ‘X’s in that surrounded region. (Try ME)
Question: Given a 2D board containing ‘X’ and ‘O’ (the letter O), capture all regions surrounded by ‘X’. A region is captured by flipping all ‘O’s into ‘X’s in that surrounded region.
Example:
X X X X
X O O X
X X O X
X O X X
After running your function, the board should be:
X X X X
X X X X
X X X X
X O X X
Explanation:
Surrounded regions shouldn’t be on the border, which means that any 'O' on the border of the board are not flipped to 'X'. Any 'O' that is not on the border and it is not connected to an 'O' on the border will be flipped to 'X'. Two cells are connected if they are adjacent cells connected horizontally or vertically.
- [Medium] Is Bipartite – Given an undirected graph G, check whether it is bipartite. (Try ME)
Question: Given an undirected graph G, check whether it is bipartite. Recall that a graph is bipartite if its vertices can be divided into two independent sets, U and V, such that no edge connects vertices of the same set.
Example:
is_bipartite(vertices=3, edges=[(0, 1), (1, 2), (2, 0)]) # returns False
is_bipartite(vertices=2, edges=[(0, 1), (1, 0)]) # returns True. U = {0}. V = {1}.
- [Medium] LC 261. Graph Valid Tree – Given n nodes and a list of undirected edges, write a function to check whether these edges make up a valid tree. (Try ME)
Question: Given n nodes labeled from 0 to n-1 and a list of undirected edges (each edge is a pair of nodes), write a function to check whether these edges make up a valid tree.
Example 1:
Input: n = 5, and edges = [[0,1], [0,2], [0,3], [1,4]]
Output: True
Example 2:
Input: n = 5, and edges = [[0,1], [1,2], [2,3], [1,3], [1,4]]
Output: False
Uniform-Cost Search / Dijkstra
- [Medium] LC 787. Cheapest Flights Within K Stops – There are n cities connected by m flights. Each fight starts from city u and arrives at v with a price w. Find the cheapest price with up to k stops. (Try ME)
Question: There are n
cities connected by m
flights. Each fight starts from city u
and arrives at v
with a price w
.
Now given all the cities and flights, together with starting city src
and the destination dst
, your task is to find the cheapest price from src
to dst
with up to k
stops. If there is no such route, output -1
.
Example 1:
Input:
n = 3, edges = [[0,1,100],[1,2,100],[0,2,500]]
src = 0, dst = 2, k = 1
Output: 200
Example 2:
Input:
n = 3, edges = [[0,1,100],[1,2,100],[0,2,500]]
src = 0, dst = 2, k = 0
Output: 500
- [Medium] LC 743. Network Delay Time – A network consists of nodes labeled 0 to N. Determine how long it will take for every node to receive a message that begins at node 0. (Try ME)
Question: A network consists of nodes labeled 0 to N. You are given a list of edges (a, b, t)
, describing the time t
it takes for a message to be sent from node a
to node b
. Whenever a node receives a message, it immediately passes the message on to a neighboring node, if possible.
Assuming all nodes are connected, determine how long it will take for every node to receive a message that begins at node 0.
Example:
given N = 5, and the following edges:
edges = [
(0, 1, 5),
(0, 2, 3),
(0, 5, 4),
(1, 3, 8),
(2, 3, 1),
(3, 5, 10),
(3, 4, 5)
]
You should return 9, because propagating the message from 0 -> 2 -> 3 -> 4 will take that much time.
A-Star Search
- [Hard] Sliding Puzzle – An 8-puzzle is a game played on a 3 x 3 board of tiles, with the ninth tile missing. Design the board and solve the puzzle. (Try ME)
Question: An 8-puzzle is a game played on a 3 x 3 board of tiles, with the ninth tile missing. The remaining tiles are labeled 1 through 8 but shuffled randomly. Tiles may slide horizontally or vertically into an empty space, but may not be removed from the board.
Design a class to represent the board, and find a series of steps to bring the board to the state [[1, 2, 3], [4, 5, 6], [7, 8, None]]
.
- [Hard] Minimum Step to Reach One – Given a positive integer N, find the smallest number of steps it will take to reach 1. (Try ME)
Question: Given a positive integer N
, find the smallest number of steps it will take to reach 1
.
There are two kinds of permitted steps:
- You may decrement
N
toN - 1
. - If
a * b = N
, you may decrementN
to the larger ofa
andb
.
For example, given 100
, you can reach 1
in five steps with the following route: 100 -> 10 -> 9 -> 3 -> 2 -> 1
.
Finite State Machine
- [Medium] Sentence Checker – Create a basic sentence checker that takes in a stream of characters and determines whether they form valid sentences. (Try ME)
Question: Create a basic sentence checker that takes in a stream of characters and determines whether they form valid sentences. If a sentence is valid, the program should print it out.
We can consider a sentence valid if it conforms to the following rules:
- The sentence must start with a capital letter, followed by a lowercase letter or a space.
- All other characters must be lowercase letters, separators (
','
,';'
,':'
) or terminal marks ('.'
,'?'
,'!'
,'‽'
). - There must be a single space between each word.
- The sentence must end with a terminal mark immediately following a word.
Backtracking
- [Easy] Generate All Possible Subsequences – Given a string, generate all possible subsequences of the string. (Try ME)
Question: Given a string, generate all possible subsequences of the string.
For example, given the string xyz
, return an array or set with the following strings:
x
y
z
xy
xz
yz
xyz
Note that zx
is not a valid subsequence since it is not in the order of the given string.
- [Medium] LC 698. Partition to K Equal Sum Subsets – Given an integer array and an integer, return if it is possible to divide this array into k non-empty subsets whose sums are all equal. (Try ME)
Question: Given an integer array nums and an integer k, return true if it is possible to divide this array into k non-empty subsets whose sums are all equal.
Example 1:
Input: nums = [4,3,2,3,5,2,1], k = 4
Output: true
Explanation: It is possible to divide it into 4 subsets (5), (1, 4), (2,3), (2,3) with equal sums.
Example 2:
Input: nums = [1,2,3,4], k = 3
Output: false
- LC 140. Word Break II – Given a string s and a dictionary of strings wordDict, add spaces in s to construct a sentence where each word is a valid dictionary word. (Try ME)
Question: Given a string s and a dictionary of strings wordDict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences in any order.
Note that the same word in the dictionary may be reused multiple times in the segmentation.
Example 1:
Input: s = "catsanddog", wordDict = ["cat","cats","and","sand","dog"]
Output: ["cats and dog","cat sand dog"]
Example 2:
Input: s = "pineapplepenapple", wordDict = ["apple","pen","applepen","pine","pineapple"]
Output: ["pine apple pen apple","pineapple pen apple","pine applepen apple"]
Explanation: Note that you are allowed to reuse a dictionary word.
Example 3:
Input: s = "catsandog", wordDict = ["cats","dog","sand","and","cat"]
Output: []
- [Hard] Print Ways to Climb Staircase – You can climb up either 1 or 2 steps at a time. Print out all possible unique ways you can climb the staircase. (Try ME)
Question: There exists a staircase with N steps, and you can climb up either 1 or 2 steps at a time. Given N, write a function that PRINT out all possible unique ways you can climb the staircase. The ORDER of the steps matters.
For example, if N is 4, then there are 5 unique ways (accoding to May 5’s question). This time we print them out as the following:
1, 1, 1, 1
2, 1, 1
1, 2, 1
1, 1, 2
2, 2
What if, instead of being able to climb 1 or 2 steps at a time, you could climb any number from a set of positive integers X?
For example, if N is 6, and X = {2, 5}. You could climb 2 or 5 steps at a time. Then there is only 1 unique way, so we print the following:
2, 2, 2
- [Medium] Boggle Game – Given a boggle grid and dictionary. Find all possible words that can be formed by a sequence of adjacent characters. (Try ME)
Question: Given a dictionary, a method to do a lookup in the dictionary and a M x N board where every cell has one character. Find all possible words that can be formed by a sequence of adjacent characters. Note that we can move to any of 8 adjacent characters, but a word should not have multiple instances of the same cell.
Example 1:
Input: dictionary = ["GEEKS", "FOR", "QUIZ", "GO"]
boggle = [['G', 'I', 'Z'],
['U', 'E', 'K'],
['Q', 'S', 'E']]
Output: Following words of the dictionary are present
GEEKS
QUIZ
Example 2:
Input: dictionary = ["GEEKS", "ABCFIHGDE"]
boggle = [['A', 'B', 'C'],
['D', 'E', 'F'],
['G', 'H', 'I']]
Output: Following words of the dictionary are present
ABCFIHGDE
- [Hard] Unique Sum Combinations – Given a list of numbers and a target number, find all possible unique subsets of the list of numbers that sum up to the target number. (Try ME)
Question: Given a list of numbers and a target number, find all possible unique subsets of the list of numbers that sum up to the target number. The numbers will all be positive numbers.
Example:
sum_combinations([10, 1, 2, 7, 6, 1, 5], 8)
# returns [(2, 6), (1, 1, 6), (1, 2, 5), (1, 7)]
# order doesn't matter
- [Medium] Generate Brackets – Given a number n, generate all possible combinations of n well-formed brackets. (Try ME)
Question: Given a number n, generate all possible combinations of n well-formed brackets.
Example 1:
generate_brackets(1) # returns ['()']
Example 2:
generate_brackets(3) # returns ['((()))', '(()())', '()(())', '()()()', '(())()']
- [Hard] LC 679. 24 Game – Given a list of 4 int, each between 1 and 9. Determine if placing the operators +, -, *, and /, and group by () gives 24. (Try ME)
Question: The 24 game is played as follows. You are given a list of four integers, each between 1 and 9, in a fixed order. By placing the operators +, -, *, and / between the numbers, and grouping them with parentheses, determine whether it is possible to reach the value 24.
For example, given the input [5, 2, 7, 8]
, you should return True
, since (5 * 2 - 7) * 8 = 24
.
Write a function that plays the 24 game.
- [Medium] Number of Android Lock Patterns – One way to unlock an Android phone is through a pattern of swipes across a 1-9 keypad. (Try ME)
Question: One way to unlock an Android phone is through a pattern of swipes across a 1-9 keypad.
For a pattern to be valid, it must satisfy the following:
- All of its keys must be distinct.
- It must not connect two keys by jumping over a third key, unless that key has already been used.
For example, 4 - 2 - 1 - 7 is a valid pattern, whereas 2 - 1 - 7 is not.
Find the total number of valid unlock patterns of length N, where 1 <= N <= 9.
- [Medium] Lazy Bartender – Given a dictionary input such as the one above, return the fewest number of drinks he must learn in order to satisfy all customers. (Try ME)
Question: At a popular bar, each customer has a set of favorite drinks, and will happily accept any drink among this set.
For example, in the following situation, customer 0 will be satisfied with drinks 0, 1, 3, or 6.
preferences = {
0: [0, 1, 3, 6],
1: [1, 4, 7],
2: [2, 4, 7, 5],
3: [3, 2, 5],
4: [5, 8]
}
A lazy bartender working at this bar is trying to reduce his effort by limiting the drink recipes he must memorize.
Given a dictionary input such as the one above, return the fewest number of drinks he must learn in order to satisfy all customers.
For the input above, the answer would be 2, as drinks 1 and 5 will satisfy everyone.
- [Medium] LC 131. Palindrome Partitioning – Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. (Try ME)
Question: Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s.
A palindrome string is a string that reads the same backward as forward.
Example 1:
Input: s = "aab"
Output: [["a","a","b"],["aa","b"]]
Example 2:
Input: s = "a"
Output: [["a"]]
- [Easy] Phone Number to Words Based on The Dictionary – Given a phone number, return all valid words that can be created using that phone number. (Try ME)
Question: Given a phone number, return all valid words that can be created using that phone number.
For instance, given the phone number 364
and dictionary ['dog', 'fish', 'cat', 'fog']
, we can construct the words ['dog', 'fog']
.
- [Easy] Map Digits to Letters – Given a mapping of digits to letters (as in a phone number), and a digit string, return all possible letters the number could represent. (Try ME)
Question: Given a mapping of digits to letters (as in a phone number), and a digit string, return all possible letters the number could represent. You can assume each valid number in the mapping is a single digit.
Example:
Input: {'2': ['a', 'b', 'c'], '3': ['d', 'e', 'f']}, '23'
Output: ['ad', 'ae', 'af', 'bd', 'be', 'bf', 'cd', 'ce', 'cf']
- [Easy] Power Set – Given a set, generate its power set. (Try ME)
Question: The power set of a set is the set of all its subsets. Write a function that, given a set, generates its power set.
For example, given a set represented by a list [1, 2, 3]
, it should return [[], [1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2, 3]]
representing the power set.
- [Medium] LC 77. Combinations – Given two integers n and k, return all possible combinations of k numbers out of 1 … n. (Try ME)
Question: Given two integers n
and k
, return all possible combinations of k
numbers out of 1 ... n
.
Example:
Input: n = 4, k = 2
Output:
[
[2,4],
[3,4],
[2,3],
[1,2],
[1,3],
[1,4],
]
- [Easy] Permutations – Given a number in the form of a list of digits, return all possible permutations. (Try ME)
Question: Given a number in the form of a list of digits, return all possible permutations.
For example, given [1,2,3]
, return [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
.
- [Medium] LC 47. All Distinct Permutations – Print all distinct permutations of a given string with duplicates. (Try ME)
Question: Given a string that may contain duplicates, write a function to return all permutations of given string such that no permutation is repeated in output.
Example 1:
Input: "112"
Output: ["112", "121", "211"]
Example 2:
Input: "AB"
Output: ["AB", "BA"]
Example 3:
Input: "ABC"
Output: ["ABC", "ACB", "BAC", "BCA", "CBA", "CAB"]
Example 4:
Input: "ABA"
Output: ["ABA", "AAB", "BAA"]
- [Hard] LC 301. Remove Invalid Parentheses – Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results. (Try ME)
Question: Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results.
Note: The input string may contain letters other than the parentheses (
and )
.
Example 1:
Input: "()())()"
Output: ["()()()", "(())()"]
Example 2:
Input: "(a)())()"
Output: ["(a)()()", "(a())()"]
Example 3:
Input: ")("
Output: [""]
- [Hard] Knight’s Tour Problem – A knight’s tour is a sequence of moves by a knight on a chessboard such that all squares are visited once. (Try ME)
Question: A knight’s tour is a sequence of moves by a knight on a chessboard such that all squares are visited once.
Given N, write a function to return the number of knight’s tours on an N by N chessboard.
- [Hard] LC 212. Word Search II – Given an m x n board of characters and a list of strings words, return all words on the board. (Try ME)
Question: Given an m x n board of characters and a list of strings words, return all words on the board.
Each word must be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically neighboring. The same letter cell may not be used more than once in a word.
Example 1:
Input: words = ["oath","pea","eat","rain"], board = [
["o","a","a","n"],
["e","t","a","e"],
["i","h","k","r"],
["i","f","l","v"]]
Output: ["eat", "oath"]
Example 2:
Input: words = ["abcb"], board = [
["a","b"],
["c","d"]]
Output: []
- [Medium] All Root to Leaf Paths in Binary Tree – Given a binary tree, return all paths from the root to leaves. (Try ME)
Question: Given a binary tree, return all paths from the root to leaves.
Example:
Given the tree:
1
/ \
2 3
/ \
4 5
Return [[1, 2], [1, 3, 4], [1, 3, 5]]
- [Hard] LC 37. Sudoku Solver – Write a program to solve a Sudoku puzzle by filling the empty cells. (Try ME)
Question: Write a program to solve a Sudoku puzzle by filling the empty cells.
A sudoku solution must satisfy all of the following rules:
- Each of the digits
1-9
must occur exactly once in each row. - Each of the digits
1-9
must occur exactly once in each column. - Each of the the digits
1-9
must occur exactly once in each of the 93x3
sub-boxes of the grid. - Empty cells are indicated
0
.
Example:
Input: [
[3, 0, 6, 5, 0, 8, 4, 0, 0],
[5, 2, 0, 0, 0, 0, 0, 0, 0],
[0, 8, 7, 0, 0, 0, 0, 3, 1],
[0, 0, 3, 0, 1, 0, 0, 8, 0],
[9, 0, 0, 8, 6, 3, 0, 0, 5],
[0, 5, 0, 0, 9, 0, 6, 0, 0],
[1, 3, 0, 0, 0, 0, 2, 5, 0],
[0, 0, 0, 0, 0, 0, 0, 7, 4],
[0, 0, 5, 2, 0, 6, 3, 0, 0]
]
Possible output:
[
[3, 1, 6, 5, 7, 8, 4, 9, 2],
[5, 2, 9, 1, 3, 4, 7, 6, 8],
[4, 8, 7, 6, 2, 9, 5, 3, 1],
[2, 6, 3, 4, 1, 5, 9, 8, 7],
[9, 7, 4, 8, 6, 3, 1, 2, 5],
[8, 5, 1, 7, 9, 2, 6, 4, 3],
[1, 3, 8, 9, 4, 7, 2, 5, 6],
[6, 9, 2, 3, 5, 1, 8, 7, 4],
[7, 4, 5, 2, 8, 6, 3, 1, 9]
]
- [Medium] LC 78. Generate All Subsets – Given a list of unique numbers, generate all possible subsets without duplicates. This includes the empty set as well. (Try ME)
Question: Given a list of unique numbers, generate all possible subsets without duplicates. This includes the empty set as well.
Example:
generate_all_subsets([1, 2, 3])
# [[], [3], [2], [2, 3], [1], [1, 3], [1, 2], [1, 2, 3]]
- [Hard] Graph Coloring – Given an undirected graph and an integer k, determine whether color with k colors such that no two adjacent nodes share the same color. (Try ME)
Question: Given an undirected graph represented as an adjacency matrix and an integer k
, determine whether each node in the graph can be colored such that no two adjacent nodes share the same color using at most k
colors.
- [Medium] LC 93. All Possible Valid IP Address Combinations – Given a string of digits, generate all possible valid IP address combinations. (Try ME)
Question: Given a string of digits, generate all possible valid IP address combinations.
IP addresses must follow the format A.B.C.D, where A, B, C, and D are numbers between 0
and 255
. Zero-prefixed numbers, such as 01
and 065
, are not allowed, except for 0
itself.
For example, given "2542540123"
, you should return ['254.25.40.123', '254.254.0.123']
- [Hard] The N Queens Puzzle – Given N, returns the number of possible arrangements of the board where N queens can be placed on the board without attacking each other. (Try ME)
Question: You have an N by N board. Write a function that, given N, returns the number of possible arrangements of the board where N queens can be placed on the board without threatening each other, i.e. no two queens share the same row, column, or diagonal.
Dynamic Programming
1D DP
- [Medium] LC 413. Arithmetic Slices – Given an integer array nums, return the number of arithmetic subarrays of nums. (Try ME)
Question: An integer array is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same. For example, [1, 3, 5, 7, 9]
, [7, 7, 7, 7]
, and [3, -1, -5, -9]
are arithmetic sequences.
Given an integer array nums, return the number of arithmetic subarrays of nums.
Example 1:
Input: nums = [1,2,3,4]
Output: 3
Explanation: We have 3 arithmetic slices in nums: [1, 2, 3], [2, 3, 4] and [1,2,3,4] itself.
Example 2:
Input: nums = [1]
Output: 0
- [Medium] LC 790. Domino and Tromino Tiling – Given a 2 x N board and instructed to completely cover the board with Dominoes and Trominoes, determine in how many ways this task is possible. (Try ME)
Question: You are given a 2 x N board, and instructed to completely cover the board with the following shapes:
- Dominoes, or 2 x 1 rectangles.
- Trominoes, or L-shapes.
Given an integer N, determine in how many ways this task is possible.
Example:
if N = 4, here is one possible configuration, where A is a domino, and B and C are trominoes.
A B B C
A B C C
- [Medium] LT 867. 4 Keys Keyboard – You can only press four keys: A, ctrl-A, ctrl-C, ctrl-V, find out the maximum numbers of ‘A’ you can print on screen. (Try ME)
Question: Imagine you have a special keyboard with the following keys:
Key 1: (A): Print one 'A' on screen.
Key 2: (Ctrl-A): Select the whole screen.
Key 3: (Ctrl-C): Copy selection to buffer.
Key 4: (Ctrl-V): Print buffer on screen appending it after what has already been printed.
Now, you can only press the keyboard for N times (with the above four keys), find out the maximum numbers of ‘A’ you can print on screen.
Example 1:
Input: 3
Output: 3
Explanation: A, A, A
Example 2:
Input: 7
Output: 9
Explanation: A, A, A, Ctrl A, Ctrl C, Ctrl V, Ctrl V
- [Medium] Climb Staircase – You can climb up either 1 or 2 steps at a time. Returns the total number of unique ways you can climb the staircase. (Try ME)
Question: There exists a staircase with N steps, and you can climb up either 1 or 2 steps at a time. Given N, write a function that returns a number represents the total number of unique ways you can climb the staircase.
For example, if N is 4, then there are 5 unique ways:
- 1, 1, 1, 1
- 2, 1, 1
- 1, 2, 1
- 1, 1, 2
- 2, 2
- [Medium] Number of Moves on a Grid – Write a function to count the number of ways of starting at the top-left corner and getting to the bottom-right corner. (Try ME)
Question: There is an N by M matrix of zeroes. Given N and M, write a function to count the number of ways of starting at the top-left corner and getting to the bottom-right corner. You can only move right or down.
For example, given a 2 by 2 matrix, you should return 2, since there are two ways to get to the bottom-right:
- Right, then down
- Down, then right
Given a 5 by 5 matrix, there are 70 ways to get to the bottom-right.
- [Medium] Cutting a Rod – Given a rod and an array of prices that contains prices of all pieces of size smaller than n. Calculate max value obtainable by cutting up the rod. (Try ME)
Question: Given a rod of length n inches and an array of prices that contains prices of all pieces of size smaller than n. Determine the maximum value obtainable by cutting up the rod and selling the pieces.
For example, if length of the rod is 8 and the values of different pieces are given as following, then the maximum obtainable value is 22 (by cutting in two pieces of lengths 2 and 6) = 5 + 17
length | 1 2 3 4 5 6 7 8
--------------------------------------------
price | 1 5 8 9 10 17 17 20
And if the prices are as following, then the maximum obtainable value is 24 (by cutting in eight pieces of length 1) = 3 + 3 + 3 + 3 + 3 + 3 + 3 + 3
length | 1 2 3 4 5 6 7 8
--------------------------------------------
price | 3 5 8 9 10 17 17 20
- [Hard] LC 42. Trapping Rain Water – Given an array of non-negative integers representing the elevation at each location, Return the amount of water that would accumulate if it rains. (Try ME)
Question: You have a landscape, in which puddles can form. You are given an array of non-negative integers representing the elevation at each location. Return the amount of water that would accumulate if it rains.
For example: [0,1,0,2,1,0,1,3,2,1,2,1]
should return 6 because 6 units of water can get trapped here.
X
X███XX█X
X█XX█XXXXXX
# [0,1,0,2,1,0,1,3,2,1,2,1]
- [Easy] Count Total Set Bits from 1 to n – Write an algorithm that finds the total number of set bits in all integers between 1 and N. (Try ME)
Question: Write an algorithm that finds the total number of set bits in all integers between 1 and N.
Examples:
Input: n = 3
Output: 4
Explanation: The binary representation (01, 10, 11) contains 4 1s.
Input: n = 6
Output: 9
Explanation: The binary representation (01, 10, 11, 100, 101, 110) contains 9 1s.
Input: n = 7
Output: 12
Input: n = 8
Output: 13
- [Medium] Maximum Non Adjacent Sum – Given a list of positive numbers, find the largest possible set such that no elements are adjacent numbers of each other. (Try ME)
Question: Given a list of positive numbers, find the largest possible set such that no elements are adjacent numbers of each other.
Example 1:
max_non_adjacent_sum([3, 4, 1, 1])
# returns 5
# max sum is 4 (index 1) + 1 (index 3)
Example 2:
max_non_adjacent_sum([2, 1, 2, 7, 3])
# returns 9
# max sum is 2 (index 0) + 7 (index 3)
- [Medium] LC 1155. Number of Dice Rolls With Target Sum – Write a function that determines how many ways it is possible to throw N dice with some number of faces each to get a specific total. (Try ME)
Question: You have d
dice, and each die has f
faces numbered 1, 2, ..., f
.
Return the number of possible ways (out of f^d
total ways) modulo 10^9 + 7
to roll the dice so the sum of the face up numbers equals target.
Example 1:
Input: d = 1, f = 6, target = 3
Output: 1
Explanation:
You throw one die with 6 faces. There is only one way to get a sum of 3.
Example 2:
Input: d = 2, f = 6, target = 7
Output: 6
Explanation:
You throw two dice, each with 6 faces. There are 6 ways to get a sum of 7:
1+6, 2+5, 3+4, 4+3, 5+2, 6+1.
Example 3:
Input: d = 2, f = 5, target = 10
Output: 1
Explanation:
You throw two dice, each with 5 faces. There is only one way to get a sum of 10: 5+5.
Example 4:
Input: d = 1, f = 2, target = 3
Output: 0
Explanation:
You throw one die with 2 faces. There is no way to get a sum of 3.
Example 5:
Input: d = 30, f = 30, target = 500
Output: 222616187
Explanation:
The answer must be returned modulo 10^9 + 7.
- [Easy] LC 120. Max Path Sum in Triangle – Given an array of arrays of integers, where each array corresponds to a row in a triangle of number, returns the weight of the maximum path. (Try ME)
Question: You are given an array of arrays of integers, where each array corresponds to a row in a triangle of numbers. For example, [[1], [2, 3], [1, 5, 1]]
represents the triangle:
1
2 3
1 5 1
We define a path in the triangle to start at the top and go down one row at a time to an adjacent value, eventually ending with an entry on the bottom row. For example, 1 -> 3 -> 5
. The weight of the path is the sum of the entries.
Write a program that returns the weight of the maximum weight path.
- [Medium] LC 279. Minimum Number of Squares Sum to N – Given a positive integer n, find the smallest number of squared integers which sum to n. (Try ME)
Question: Given a positive integer n, find the smallest number of squared integers which sum to n.
For example, given n = 13
, return 2
since 13 = 3^2 + 2^2 = 9 + 4
.
Given n = 27
, return 3
since 27 = 3^2 + 3^2 + 3^2 = 9 + 9 + 9
.
- [Medium] Multiset Partition – Given a multiset of integers, return whether it can be partitioned into two subsets whose sums are the same. (Try ME)
Question: Given a multiset of integers, return whether it can be partitioned into two subsets whose sums are the same.
For example, given the multiset {15, 5, 20, 10, 35, 15, 10}
, it would return true, since we can split it up into {15, 5, 10, 15, 10}
and {20, 35}
, which both add up to 55
.
Given the multiset {15, 5, 20, 10, 35}
, it would return false
, since we can’t split it up into two subsets that add up to the same sum.
- [Easy] Smallest Sum Not Subset Sum – Given a sorted list of positive numbers, find the smallest positive number that cannot be a sum of any subset in the list. (Try ME)
Question: Given a sorted list of positive numbers, find the smallest positive number that cannot be a sum of any subset in the list.
Example:
Input: [1, 2, 3, 8, 9, 10]
Output: 7
Numbers 1 to 6 can all be summed by a subset of the list of numbers, but 7 cannot.
- [Medium] LT 879. NBA Playoff Matches – Now, you’re given n teams, and you need to output their final contest matches in the form of a string. (Try ME)
Question: During the NBA playoffs, we always arrange the rather strong team to play with the rather weak team, like make the rank 1 team play with the rank nth team, which is a good strategy to make the contest more interesting. Now, you’re given n teams, and you need to output their final contest matches in the form of a string.
The n teams are given in the form of positive integers from 1 to n, which represents their initial rank. (Rank 1 is the strongest team and Rank n is the weakest team.) We’ll use parentheses () and commas , to represent the contest team pairing - parentheses () for pairing and commas , for partition. During the pairing process in each round, you always need to follow the strategy of making the rather strong one pair with the rather weak one.
We ensure that the input n can be converted into the form
2^k
, where k is a positive integer.
Example 1:
Input: 2
Output: "(1,2)"
Example 2:
Input: 4
Output: "((1,4),(2,3))"
Explanation:
In the first round, we pair the team 1 and 4, the team 2 and 3 together, as we need to make the strong team and weak team together.
And we got (1,4),(2,3).
In the second round, the winners of (1,4) and (2,3) need to play again to generate the final winner, so you need to add the paratheses outside them.
And we got the final answer ((1,4),(2,3)).
Example 3:
Input: 8
Output: "(((1,8),(4,5)),((2,7),(3,6)))"
Explanation:
First round: (1,8),(2,7),(3,6),(4,5)
Second round: ((1,8),(4,5)),((2,7),(3,6))
Third round: (((1,8),(4,5)),((2,7),(3,6)))
- [Medium] Number of Flips to Make Binary String – You are given a string consisting of the letters x and y, such as xyxxxyxyy. Find min flip so that all x goes before y. (Try ME)
Question: You are given a string consisting of the letters x
and y
, such as xyxxxyxyy
. In addition, you have an operation called flip, which changes a single x
to y
or vice versa.
Determine how many times you would need to apply this operation to ensure that all x’s come before all y’s. In the preceding example, it suffices to flip the second and sixth characters, so you should return 2.
- [Easy] Maximum Subarray Sum – Given array that may contain both positive and negative integers, find the sum of contiguous subarray of numbers which has the largest sum. (Try ME)
Question: You are given a one dimensional array that may contain both positive and negative integers, find the sum of contiguous subarray of numbers which has the largest sum.
For example, if the given array is [-2, -5, 6, -2, -3, 1, 5, -6]
, then the maximum subarray sum is 7 as sum of [6, -2, -3, 1, 5]
equals 7
Solve this problem with Divide and Conquer as well as DP separately.
- [Hard] Largest Sum of Non-adjacent Numbers – Given a list of integers, write a function that returns the largest sum of non-adjacent numbers. Numbers can be 0 or negative. (Try ME)
Question: Given a list of integers, write a function that returns the largest sum of non-adjacent numbers. Numbers can be 0 or negative.
For example, [2, 4, 6, 2, 5]
should return 13, since we pick 2, 6, and 5. [5, 1, 1, 5]
should return 10, since we pick 5 and 5.
Follow-up: Can you do this in O(N) time and constant space?
- [Medium] Delete Columns to Make Sorted II – Given 2D matrix, the task is to count the number of columns to be deleted so that all the rows are lexicographically sorted. (Try ME)
Question: You are given an N by M 2D matrix of lowercase letters. The task is to count the number of columns to be deleted so that all the rows are lexicographically sorted.
Example 1:
Given the following table:
hello
geeks
Your function should return 1 as deleting column 1 (index 0)
Now both strings are sorted in lexicographical order:
ello
eeks
Example 2:
Given the following table:
xyz
lmn
pqr
Your function should return 0. All rows are already sorted lexicographically.
- [Hard] Largest Divisible Pairs Subset – Given a set of distinct positive integers, find the largest subset such that every pair of elements has divisible counterpart in the subset. (Try ME)
Question: Given a set of distinct positive integers, find the largest subset such that every pair of elements in the subset (i, j)
satisfies either i % j = 0
or j % i = 0
.
For example, given the set [3, 5, 10, 20, 21]
, you should return [5, 10, 20]
. Given [1, 3, 6, 24]
, return [1, 3, 6, 24]
.
- [Medium] LC 139. Word Break – Given a non-empty string s and a dictionary with a list of non-empty words, determine if s can be segmented into a space-separated sequence. (Try ME)
Question: Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words.
Note:
- The same word in the dictionary may be reused multiple times in the segmentation.
- You may assume the dictionary does not contain duplicate words.
Example 1:
Input: s = "Pseudocode", wordDict = ["Pseudo", "code"]
Output: True
Explanation: Return true because "Pseudocode" can be segmented as "Pseudo code".
Example 2:
Input: s = "applepenapple", wordDict = ["apple", "pen"]
Output: True
Explanation: Return true because "applepenapple" can be segmented as "apple pen apple".
Note that you are allowed to reuse a dictionary word.
Example 3:
Input: s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"]
Output: False
- [Hard] Word Concatenation – Given a set of words, find all words that are concatenations of other words in the set. (Try ME)
Question: Given a set of words, find all words that are concatenations of other words in the set.
Example:
Input: ['rat', 'cat', 'cats', 'dog', 'catsdog', 'dogcat', 'dogcatrat']
Output: ['catsdog', 'dogcat', 'dogcatrat']
- [Medium] LC 91. Decode Ways – A message containing letters from A-Z is being encoded to numbers using the following mapping: ‘A’ -> 1 ‘B’ -> 2 … ‘Z’ -> 26. (Try ME)
Question: A message containing letters from A-Z
is being encoded to numbers using the following mapping:
'A' -> 1
'B' -> 2
...
'Z' -> 26
Given an encoded message containing digits, determine the total number of ways to decode it.
Example 1:
Input: "12"
Output: 2
Explanation: It could be decoded as AB (1 2) or L (12).
Example 2:
Input: "10"
Output: 1
- [Medium] Making Changes – Given a list of possible coins in cents, and an amount (in cents) n, return the minimum number of coins needed to create the amount n. (Try ME)
Question: Given a list of possible coins in cents, and an amount (in cents) n, return the minimum number of coins needed to create the amount n. If it is not possible to create the amount using the given coin denomination, return None.
Example:
make_change([1, 5, 10, 25], 36) # gives 3 coins (25 + 10 + 1)
- [Medium] Reverse Coin Change – Given an array represents the number of ways we can produce
i
units of change, determine the denominations that must be in use. (Try ME)
i
units of change, determine the denominations that must be in use. (Try ME)Question: You are given an array of length N
, where each element i
represents the number of ways we can produce i
units of change. For example, [1, 0, 1, 1, 2]
would indicate that there is only one way to make 0, 2, or 3
units, and two ways of making 4
units.
Given such an array, determine the denominations that must be in use. In the case above, for example, there must be coins with value 2, 3, and 4
.
2D DP
- [Hard] Subset Sum – Given a list of integers S and a target number k, write a function that returns a subset of S that adds up to k. (Try ME)
Question: Given a list of integers S and a target number k, write a function that returns a subset of S that adds up to k. If such a subset cannot be made, then return null.
Integers can appear more than once in the list. You may assume all numbers in the list are positive.
For example, given S = [12, 1, 61, 5, 9, 2]
and k = 24
, return [12, 9, 2, 1]
since it sums up to 24.
- [Hard] Number of Ways to Divide an Array into K Equal Sum Sub-arrays – Given an integer K and an integer array, find the number of ways to split the array into K equal sum sub-arrays of non-zero lengths. (Try ME)
Question: Given an integer K and an array arr[] of N integers, the task is to find the number of ways to split the array into K equal sum sub-arrays of non-zero lengths.
Example 1:
Input: arr[] = [0, 0, 0, 0], K = 3
Output: 3
All possible ways are:
[[0], [0], [0, 0]]
[[0], [0, 0], [0]]
[[0, 0], [0], [0]]
Example 2:
Input: arr[] = [1, -1, 1, -1], K = 2
Output: 1
- [Hard] Ordered Minimum Window Subsequence – Given an array nums and a subsequence sub, find the shortest subarray of nums that contains sub. (Try ME)
Question: Given an array nums and a subsequence sub, find the shortest subarray of nums that contains sub.
- If such subarray does not exist, return -1, -1.
- Note that the subarray must contain the elements of sub in the correct order.
Example:
Input: nums = [1, 2, 3, 5, 8, 7, 6, 9, 5, 7, 3, 0, 5, 2, 3, 4, 4, 7], sub = [5, 7]
Output: start = 8, size = 2
- [Medium] Sum of Squares – Given a number n, find the least number of squares needed to sum up to the number. (Try ME)
Question: Given a number n, find the least number of squares needed to sum up to the number. For example, 13 = 3^2 + 2^2
, thus the least number of squares requried is 2.
- [Hard] LC 312. Burst Balloons – If the you burst balloon i you will get
nums[left] * nums[i] * nums[right]
coins. Find the maximum coins by bursting the balloons wisely. (Try ME)
nums[left] * nums[i] * nums[right]
coins. Find the maximum coins by bursting the balloons wisely. (Try ME)Question: Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by array nums. You are asked to burst all the balloons. If the you burst balloon i you will get nums[left] * nums[i] * nums[right]
coins. Here left and right are adjacent indices of i. After the burst, the left and right then becomes adjacent.
Find the maximum coins you can collect by bursting the balloons wisely.
Note:
You may imagine nums[-1] = nums[n] = 1. They are not real therefore you can not burst them.
Example:
Input: [3,1,5,8]
Output: 167
Explanation: nums = [3,1,5,8] --> [3,5,8] --> [3,8] --> [8] --> []
coins = 3*1*5 + 3*5*8 + 1*3*8 + 1*8*1 = 167
- [Medium] Paint House – Given an N by K matrix where the nth row and kth column represents the cost to build the nth house with kth color, return the minimum cost. (Try ME)
Question: A builder is looking to build a row of N houses that can be of K different colors. He has a goal of minimizing cost while ensuring that no two neighboring houses are of the same color.
Given an N by K matrix where the n-th row and k-th column represents the cost to build the n-th house with k-th color, return the minimum cost which achieves this goal.
- [Medium] Maze Paths – Given a nxm matrix, find the number of ways someone can go from the top left corner to the bottom right corner. (Try ME)
Question: A maze is a matrix where each cell can either be a 0 or 1. A 0 represents that the cell is empty, and a 1 represents a wall that cannot be walked through. You can also only travel either right or down.
Given a nxm matrix, find the number of ways someone can go from the top left corner to the bottom right corner. You can assume the two corners will always be 0.
Example:
Input: [[0, 1, 0], [0, 0, 1], [0, 0, 0]]
# 0 1 0
# 0 0 1
# 0 0 0
Output: 2
The two paths that can only be taken in the above example are: down -> right -> down -> right, and down -> down -> right -> right.
- [Hard] Maximize Sum of the Minimum of K Subarrays – Given an array a of size N and an integer K, divide the array into K segments such that sum of the minimum of K segments is maximized. (Try ME)
Question: Given an array a of size N and an integer K, the task is to divide the array into K segments such that sum of the minimum of K segments is maximized.
Example1:
Input: [5, 7, 4, 2, 8, 1, 6], K = 3
Output: 13
Divide the array at indexes 0 and 1. Then the segments are [5], [7], [4, 2, 8, 1, 6] Sum of the minimus is 5 + 7 + 1 = 13
Example2:
Input: [6, 5, 3, 8, 9, 10, 4, 7, 10], K = 4
Output: 27
[6, 5, 3, 8, 9], [10], [4, 7], [10] => 3 + 10 + 4 + 10 = 27
- [Hard] Minimum Palindrome Substring – Given a string, split it into as few strings as possible such that each string is a palindrome. (Try ME)
Question: Given a string, split it into as few strings as possible such that each string is a palindrome.
For example, given the input string "racecarannakayak"
, return ["racecar", "anna", "kayak"]
.
Given the input string "abc"
, return ["a", "b", "c"]
.
- [Hard] Partition Array to Reach Mininum Difference – Given an array of positive integers, divide the array into two subsets such that the difference between the sum of the subsets reaches min. (Try ME)
Question: Given an array of positive integers, divide the array into two subsets such that the difference between the sum of the subsets is as small as possible.
For example, given [5, 10, 15, 20, 25]
, return the sets [10, 25]
and [5, 15, 20]
, which has a difference of 5
, which is the smallest possible difference.
- [Hard] Longest Common Subsequence of 3 Strings – Write a program that computes the length of the longest common subsequence of three given strings. (Try ME)
Question: Write a program that computes the length of the longest common subsequence of three given strings.
For example, given "epidemiologist"
, "refrigeration"
, and "supercalifragilisticexpialodocious"
, it should return 5
, since the longest common subsequence is "eieio"
.
- [Hard] Non-adjacent Subset Sum – Given an array of unique positive numbers. Find non-adjacent subset of elements sum up to k. (Try ME)
Question: Given an array of size n with unique positive integers and a positive integer K, check if there exists a combination of elements in the array satisfying both of below constraints:
- The sum of all such elements is K
- None of those elements are adjacent in the original array
Example:
Input: K = 14, arr = [1, 9, 8, 3, 6, 7, 5, 11, 12, 4]
Output: [3, 7, 4]
- [Medium] Max Value of Coins to Collect in a Matrix – You are given a 2-d matrix. Find the maximum number of coins you can collect by the bottom right corner. (Try ME)
Question: You are given a 2-d matrix where each cell represents number of coins in that cell. Assuming we start at matrix[0][0]
, and can only move right or down, find the maximum number of coins you can collect by the bottom right corner.
Example:
Given below matrix:
0 3 1 1
2 0 0 4
1 5 3 1
The most we can collect is 0 + 2 + 1 + 5 + 3 + 1 = 12 coins.
- [Medium] Largest Square – Given matrix with 1 and 0. Find the largest square matrix containing only 1’s and return its dimension size. (Try ME)
Question: Given an N by M matrix consisting only of 1’s and 0’s, find the largest square matrix containing only 1’s and return its dimension size.
Example:
Given the following matrix:
[[1, 0, 0, 0],
[1, 1, 1, 1],
[1, 1, 1, 1],
[0, 1, 0, 0]]
Return 2. As the following 1s form the largest square matrix containing only 1s:
[1, 1],
[1, 1]
- [Hard] LC 76. Minimum Window Substring – Given a string and a set of characters, return the shortest substring containing all the characters in the set. (Try ME)
Question: Given a string and a set of characters, return the shortest substring containing all the characters in the set.
For example, given the string "figehaeci"
and the set of characters {a, e, i}
, you should return "aeci"
.
If there is no substring containing all the characters in the set, return null.
- [Hard] LC 727. Minimum Window Subsequence – Given strings S and T, find the minimum (contiguous) substring W of S, so that T is a subsequence of W. (Try ME)
Question: Given strings S and T, find the minimum (contiguous) substring W of S, so that T is a subsequence of W.
If there is no such window in S that covers all characters in T, return the empty string “”. If there are multiple such minimum-length windows, return the one with the left-most starting index.
Example:
Input:
S = "abcdebdde", T = "bde"
Output: "bcde"
Explanation:
"bcde" is the answer because it occurs before "bdde" which has the same length.
"deb" is not a smaller window because the elements of T in the window must occur in order.
- [Medium] Longest Common Subsequence – Given two sequences, find the length of longest subsequence present in both of them. (Try ME)
Question: Given two sequences, find the length of longest subsequence present in both of them.
A subsequence is a sequence that appears in the same relative order, but not necessarily contiguous.
Example 1:
Input: "ABCD" and "EDCA"
Output: 1
Explanation:
LCS is 'A' or 'D' or 'C'
Example 2:
Input: "ABCD" and "EACB"
Output: 2
Explanation:
LCS is "AC"
- [Hard] K-Palindrome – Given a string which we can delete at most k, return whether you can make a palindrome. (Try ME)
Question: Given a string which we can delete at most k, return whether you can make a palindrome.
For example, given 'waterrfetawx'
and a k of 2, you could delete f and x to get 'waterretaw'
.
- [Medium] LC 446. Count Arithmetic Subsequences – Given an array of n positive integers. The task is to count the number of Arithmetic Subsequence in the array. (Try ME)
Question: Given an array of n positive integers. The task is to count the number of Arithmetic Subsequence in the array. Note: Empty sequence or single element sequence is also Arithmetic Sequence.
Example 1:
Input : arr[] = [1, 2, 3]
Output : 8
Arithmetic Subsequence from the given array are:
[], [1], [2], [3], [1, 2], [2, 3], [1, 3], [1, 2, 3].
Example 2:
Input : arr[] = [10, 20, 30, 45]
Output : 12
Example 3:
Input : arr[] = [1, 2, 3, 4, 5]
Output : 23
- [Medium] LC 718. Longest Common Sequence of Browsing Histories – Write a function that takes two users’ browsing histories as input and returns the longest contiguous sequence of URLs that appear in both. (Try ME)
Question: We have some historical clickstream data gathered from our site anonymously using cookies. The histories contain URLs that users have visited in chronological order.
Write a function that takes two users’ browsing histories as input and returns the longest contiguous sequence of URLs that appear in both.
For example, given the following two users’ histories:
user1 = ['/home', '/register', '/login', '/user', '/one', '/two']
user2 = ['/home', '/red', '/login', '/user', '/one', '/pink']
You should return the following:
['/login', '/user', '/one']
- [Hard] Edit Distance – The edit distance between two strings refers to the minimum number of character insertions, deletions, and substitutions required to change one string to the other. (Try ME)
Question: The edit distance between two strings refers to the minimum number of character insertions, deletions, and substitutions required to change one string to the other. For example, the edit distance between “kitten” and “sitting” is three: substitute the “k” for “s”, substitute the “e” for “i”, and append a “g”.
Given two strings, compute the edit distance between them.
DP+
- [Hard] LT 623. K Edit Distanc – Return all the strings for each the edit distance with the target no greater than k. (Try ME)
Question: Given a set of strings which just has lower case letters and a target string, output all the strings for each the edit distance with the target no greater than k. You have the following 3 operations permitted on a word:
- Insert a character
- Delete a character
- Replace a character
Example 1:
Given words = ["abc", "abd", "abcd", "adc"] and target = "ac", k = 1
Return ["abc", "adc"]
Explanation:
- "abc" remove "b"
- "adc" remove "d"
Example 2:
Given words = ["acc","abcd","ade","abbcd"] and target = "abc", k = 2
Return ["acc","abcd","ade","abbcd"]
Explanation:
- "acc" turns "c" into "b"
- "abcd" remove "d"
- "ade" turns "d" into "b" turns "e" into "c"
- "abbcd" gets rid of "b" and "d"
- [Hard] LC 975. Odd Even Jump – Count number of start indices so that making a series of jumps based on even and odd turns can reach the end. (Try ME)
Question: You are given an integer array A
. From some starting index, you can make a series of jumps. The (1st, 3rd, 5th, …) jumps in the series are called odd numbered jumps, and the (2nd, 4th, 6th, …) jumps in the series are called even numbered jumps.
You may from index i
jump forward to index j
(with i < j
) in the following way:
- During odd numbered jumps (ie. jumps 1, 3, 5, …), you jump to the index
j
such thatA[i] <= A[j]
andA[j]
is the smallest possible value. If there are multiple such indexesj
, you can only jump to the smallest such indexj
. - During even numbered jumps (ie. jumps 2, 4, 6, …), you jump to the index
j
such thatA[i] >= A[j]
andA[j]
is the largest possible value. If there are multiple such indexesj
, you can only jump to the smallest such indexj
. - (It may be the case that for some index
i
, there are no legal jumps.)
A starting index is good if, starting from that index, you can reach the end of the array (index A.length - 1)
by jumping some number of times (possibly 0 or more than once.)
Return the number of good starting indexes.
Example 1:
Input: [10,13,12,14,15]
Output: 2
Explanation:
From starting index i = 0, we can jump to i = 2 (since A[2] is the smallest among A[1], A[2], A[3], A[4] that is greater or equal to A[0]), then we can't jump any more.
From starting index i = 1 and i = 2, we can jump to i = 3, then we can't jump any more.
From starting index i = 3, we can jump to i = 4, so we've reached the end.
From starting index i = 4, we've reached the end already.
In total, there are 2 different starting indexes (i = 3, i = 4) where we can reach the end with some number of jumps.
Example 2:
Input: arr = [2,3,1,1,4]
Output: 3
Explanation:
From starting index i = 0, we make jumps to i = 1, i = 2, i = 3:
During our 1st jump (odd-numbered), we first jump to i = 1 because arr[1] is the smallest value in [arr[1], arr[2], arr[3], arr[4]] that is greater than or equal to arr[0].
During our 2nd jump (even-numbered), we jump from i = 1 to i = 2 because arr[2] is the largest value in [arr[2], arr[3], arr[4]] that is less than or equal to arr[1]. arr[3] is also the largest value, but 2 is a smaller index, so we can only jump to i = 2 and not i = 3
During our 3rd jump (odd-numbered), we jump from i = 2 to i = 3 because arr[3] is the smallest value in [arr[3], arr[4]] that is greater than or equal to arr[2].
We can't jump from i = 3 to i = 4, so the starting index i = 0 is not good.
In a similar manner, we can deduce that:
From starting index i = 1, we jump to i = 4, so we reach the end.
From starting index i = 2, we jump to i = 3, and then we can't jump anymore.
From starting index i = 3, we jump to i = 4, so we reach the end.
From starting index i = 4, we are already at the end.
In total, there are 3 different starting indices i = 1, i = 3, and i = 4, where we can reach the end with some
number of jumps.
Example 3:
Input: arr = [5,1,3,4,2]
Output: 3
Explanation: We can reach the end from starting indices 1, 2, and 4.
- [Hard] Max Path Value in Directed Graph – Given a directed graph, return the largest value path of the graph. Define a path’s value as the number of most frequent letters along path. (Try ME)
Question: In a directed graph, each node is assigned an uppercase letter. We define a path’s value as the number of most frequently-occurring letter along that path. For example, if a path in the graph goes through “ABACA”, the value of the path is 3, since there are 3 occurrences of ‘A’ on the path.
Given a graph with n nodes and m directed edges, return the largest value path of the graph. If the largest value is infinite, then return null.
The graph is represented with a string and an edge list. The i-th character represents the uppercase letter of the i-th node. Each tuple in the edge list (i, j) means there is a directed edge from the i-th node to the j-th node. Self-edges are possible, as well as multi-edges.
Example1:
Input: "ABACA", [(0, 1), (0, 2), (2, 3), (3, 4)]
Output: 3
Explanation: maximum value 3 using the path of vertices [0, 2, 3, 4], ie. A -> A -> C -> A.
Example2:
Input: "A", [(0, 0)]
Output: None
Explanation: we have an infinite loop.
- [Hard] Largest Rectangle – Given an N by M matrix consisting only of 1’s and 0’s, find the largest rectangle containing only 1’s and return its area. (Try ME)
Question: Given an N by M matrix consisting only of 1’s and 0’s, find the largest rectangle containing only 1’s and return its area.
For Example:
Given the following matrix:
[[1, 0, 0, 0],
[1, 0, 1, 1],
[1, 0, 1, 1],
[0, 1, 0, 0]]
Return 4. As the following 1s form the largest rectangle containing only 1s:
[1, 1],
[1, 1]
- [Hard] Find Arbitrage Opportunities – Given a table of currency exchange rates, represented as a 2D array. Determine whether there is a possible arbitrage. (Try ME)
Question: Suppose you are given a table of currency exchange rates, represented as a 2D array. Determine whether there is a possible arbitrage: that is, whether there is some sequence of trades you can make, starting with some amount A of any currency, so that you can end up with some amount greater than A of that currency.
There are no transaction costs and you can trade fractional quantities.
Example:
Given the following matrix:
# RMB, USD, CAD
# RMB 1, 0.14, 0.19
# USD 6.97, 1, 1.3
# CAD 5.37, 0.77, 1
# Since RMB -> CAD -> RMB: 1 Yuan * 0.19 * 5.37 = 1.02 Yuan
# If we keep exchange RMB to CAD and exchange back, we can make a profit eventually.
- [Medium] Maximum Circular Subarray Sum – Given a circular array, compute its maximum subarray sum in O(n) time. A subarray can be empty, and in this case the sum is 0. (Try ME)
Question: Given a circular array, compute its maximum subarray sum in O(n)
time. A subarray can be empty, and in this case the sum is 0.
Example 1:
Input: [8, -1, 3, 4]
Output: 15
Explanation: we choose the numbers 3, 4, and 8 where the 8 is obtained from wrapping around.
Example 2:
Input: [-4, 5, 1, 0]
Output: 6
Explanation: we choose the numbers 5 and 1.
Minimax
- [Medium] Egg Dropping Puzzle – Given N identical eggs and access to a building with k floors. Your task is to find the lowest floor that will cause an egg to break. (Try ME)
Questions: You are given N identical eggs and access to a building with k floors. Your task is to find the lowest floor that will cause an egg to break, if dropped from that floor. Once an egg breaks, it cannot be dropped again. If an egg breaks when dropped from the xth floor, you can assume it will also break when dropped from any floor greater than x.
Write an algorithm that finds the minimum number of trial drops it will take, in the worst case, to identify this floor.
Example1:
Input: N = 1, k = 5,
Output: 5
we will need to try dropping the egg at every floor, beginning with the first, until we reach the fifth floor, so our solution will be 5.
Example2:
Input: N = 2, k = 36
Minimum number of trials in worst case with 2 eggs and 36 floors is 8
- [Medium] LC 375. Guess Number Higher or Lower II – When you guess a particular number x, and you guess wrong, you pay $x. Given a particular n ≥ 1, find out how much money you need to have to guarantee a win. (Try ME)
Question: We are playing the Guess Game. The game is as follows:
I pick a number from 1 to n. You have to guess which number I picked.
Every time you guess wrong, I’ll tell you whether the number I picked is higher or lower.
However, when you guess a particular number x, and you guess wrong, you pay $x. You win the game when you guess the number I picked.
Given a particular n ≥ 1, find out how much money you need to have to guarantee a win.
Example:
n = 10, I pick 8.
First round: You guess 5, I tell you that it's higher. You pay $5.
Second round: You guess 7, I tell you that it's higher. You pay $7.
Third round: You guess 9, I tell you that it's lower. You pay $9.
Game over. 8 is the number I picked.
You end up paying $5 + $7 + $9 = $21.
- [Hard] Optimal Strategy For Coin Game – Write a program that returns the maximum amount of money you can win with certainty. (Try ME)
Question: In front of you is a row of N
coins, with values v_1, v_2, ..., v_n
.
You are asked to play the following game. You and an opponent take turns choosing either the first or last coin from the row, removing it from the row, and receiving the value of the coin.
Write a program that returns the maximum amount of money you can win with certainty, if you move first, assuming your opponent plays optimally.