Records of Review
type |
problems |
date |
understanding |
DFS |
Combinations Of Coins |
May 17, 2022 |
moderate |
DFS |
All Permutations I |
May 17, 2022 |
weak |
DFS |
All Subsets I |
May 17, 2022 |
moderate |
DFS |
All Valid Permutations Of Parentheses I |
May 17, 2022 |
moderate |
Recursion |
Maximum Path Sum Binary Tree II |
May 18, 2022 |
weak |
Recursion |
Spiral Order Traverse I |
May 18, 2022 |
weak |
Recursion |
N Queens |
May 18, 2022 |
weak |
Recursion |
Reverse Linked List In Pairs |
May 18, 2022 |
weak |
Recursion |
String Abbreviation Matching |
May 18, 2022 |
very weak |
Recursion |
Store Number Of Nodes In Left Subtree |
May 19, 2022 |
moderate |
Recursion |
Lowest Common Ancestor I |
May 19, 2022 |
moderate |
Recursion |
Spiral Order Traverse II |
May 19, 2022 |
weak |
Recursion |
Reverse Binary Tree Upside Down |
May 19, 2022 |
weak |
DFS |
All Permutations I |
May 21, 2022 |
moderate |
Recursion |
String Abbreviation Matching |
May 21, 2022 |
moderate |
Recursion |
N Queens |
May 21, 2022 |
moderate |
Recursion |
Spiral Order Traverse II |
May 21, 2022 |
weak |
Recursion |
N Queens |
May 30, 2022 |
moderate |
Recursion |
String Abbreviation Matching |
May 30, 2022 |
moderate |
DFS |
All Permutations I |
May 30, 2022 |
strong |
Heap |
K Smallest In Unsorted Array |
May 31, 2022 |
weak |
BFS |
Get Keys In Binary Tree Layer By Layer |
May 31, 2022 |
weak |
BFS |
Bipartite |
May 31, 2022 |
weak |
BFS |
Check If Binary Tree Is Completed |
May 31, 2022 |
moderate |
BFS |
Kth Smallest Number In Sorted Matrix |
May 31, 2022 |
weak |
DFS |
All Valid Permutations Of Parentheses I |
Jun 1, 2022 |
strong |
DFS |
Combinations Of Coins |
Jun 1, 2022 |
strong |
Recursion |
Spiral Order Traverse II |
Jun 1, 2022 |
moderate |
Recursion |
Maximum Path Sum Binary Tree I |
Jun 1, 2022 |
weak |
Recursion |
Maximum Path Sum Binary Tree II |
Jun 1, 2022 |
moderate |
Recursion |
Max Path Sum From Leaf To Root |
Jun 1, 2022 |
weak |
Recursion |
Binary Tree Path Sum To Target III |
Jun 1, 2022 |
weak |
Recursion |
Reverse Linked List In Pairs |
Jun 2, 2022 |
moderate |
Recursion |
Maximum Path Sum Binary Tree III |
Jun 2, 2022 |
moderate |
Recursion |
Flatten Binary Tree to Linked List |
Jun 3, 2022 |
weak |
DFS |
All Valid Permutations Of Parentheses I |
Jun 9, 2022 |
strong |
DFS |
Combinations Of Coins |
Jun 9, 2022 |
moderate |
Recursion |
Spiral Order Traverse II |
Jun 9, 2022 |
weak |
DFS |
All Subsets I |
Jun 10, 2022 |
strong |
Recursion |
Maximum Path Sum Binary Tree I |
Jun 10, 2022 |
moderate |
Recursion |
Maximum Path Sum Binary Tree II |
Jun 10, 2022 |
moderate |
Recursion |
Maximum Path Sum Binary Tree III |
Jun 10, 2022 |
moderate |
Recursion |
Binary Tree Path Sum To Target III |
Jun 10, 2022 |
weak |
Heap |
K Smallest In Unsorted Array |
Jun 11, 2022 |
moderate |
BFS |
Get Keys In Binary Tree Layer By Layer |
Jun 11, 2022 |
moderate |
BFS |
Bipartite |
Jun 11, 2022 |
weak |
BFS |
Seven Puzzle |
Jun 11, 2022 |
weak |
BFS |
Word Ladder II |
Jun 13, 2022 |
weak |
BFS |
Word Ladder |
Jun 13, 2022 |
weak |
BFS |
Largest Product Of Length |
Jun 13, 2022 |
weak |
BFS |
Kth Smallest With Only 3, 5, 7 As Factors |
Jun 13, 2022 |
weak |
BFS |
Kth Closest Point To <0,0,0> |
Jun 13, 2022 |
weak |
BFS |
Kth Closest Point To <0,0,0> |
Jun 14, 2022 |
moderate |
BFS |
Seven Puzzle |
Jun 14, 2022 |
moderate |
Recursion |
N Queens |
Jun 14, 2022 |
strong |
Recursion |
String Abbreviation Matching |
Jun, 2022 |
moderate |
BFS |
Course Schedule II |
Jun 16, 2022 |
weak |
BFS |
Place To Put The Chair I |
Jun 16, 2022 |
weak |
BFS |
Alien Dictionary |
Jun 16, 2022 |
weak |
DFS |
Two Subsets With Min Difference |
Jun 17, 2022 |
weak |
DFS |
All Subsets II of Size K |
Jun 18, 2022 |
weak |
DFS |
All Subsets of Size K |
Jun 18, 2022 |
moderate |
DFS |
All Subsets II |
Jun 18, 2022 |
moderate |
DFS |
All Valid Permutations Of Parentheses II |
Jun 18, 2022 |
weak |
DFS |
All Valid Permutations Of Parentheses III |
Jun 18, 2022 |
weak |
DFS |
Factor Combinations |
Jun 18, 2022 |
weak |
DFS |
All Permutations of Subsets |
Jun 19, 2022 |
moderate |
DFS |
Keep Distance For Identical Elements |
Jun 19, 2022 |
weak |
DFS |
Combinations For Telephone Pad I |
Jun 19, 2022 |
weak |
DFS |
Restore IP Addresses |
Jun 19, 2022 |
weak |
Recursion |
Reconstruct Binary Tree With Preorder And Inorder |
Jun 21, 2022 |
weak |
Recursion |
Reconstruct Binary Search Tree With Postorder Traversal |
Jun 21, 2022 |
weak |
Recursion |
Reconstruct Binary Tree With Levelorder And Inorder |
Jun 21, 2022 |
weak |
BFS |
Seven Puzzle |
Jun 22, 2022 |
moderate |
Recursion |
String Abbreviation Matching |
Jun 22, 2022 |
moderate |
DFS |
Two Subsets With Min Difference |
Jun 22, 2022 |
moderate |
DFS |
All Subsets II of Size K |
Jun 22, 2022 |
moderate |
DFS |
All Valid Permutations Of Parentheses III |
Jun 22, 2022 |
moderate |
Dynamic Programming |
Longest Ascending SubArray |
Jun 24, 2022 |
moderate |
Dynamic Programming |
Max Product Of Cutting Rope |
Jun 24, 2022 |
weak |
Dynamic Programming |
Dictionary Word I |
Jun 24, 2022 |
weak |
Two Pointers |
Find the Duplicate Number |
Jun 27, 2022 |
moderate |
Dynamic Programming |
Array Hopper I |
Jun 28, 2022 |
moderate |
Dynamic Programming |
Array Hopper II |
Jun 28, 2022 |
moderate |
Dynamic Programming |
Largest Subarray Sum |
Jun 28, 2022 |
moderate |
Dynamic Programming |
Dictionary Word I |
Jun 29, 2022 |
moderate |
Dynamic Programming |
Edit Distance |
Jun 29, 2022 |
weak |
Dynamic Programming |
Largest Square Of 1s |
Jul 1, 2022 |
weak |
Dynamic Programming |
Longest Consecutive 1s |
Jul 1, 2022 |
strong |
Dynamic Programming |
Longest Cross Of 1s |
Jul 1, 2022 |
moderate |
Dynamic Programming |
Largest X Of 1s |
Jul 1, 2022 |
weak |
Dynamic Programming |
Largest Square Surrounded By One |
Jul 1, 2022 |
weak |
Dynamic Programming |
Largest Square Of Matches |
Jul 2, 2022 |
weak |
Dynamic Programming |
Longest Ascending Subsequence |
Jul 2, 2022 |
weak |
Dynamic Programming |
Longest Ascending Subsequence II |
Jul 3, 2022 |
weak |
Dynamic Programming |
Count Ascending Subsequence |
Jul 3, 2022 |
moderate |
Dynamic Programming |
Largest Set Of Points With Positive Slope |
Jul 3, 2022 |
weak |
Dynamic Programming |
Longest Common Substring |
Jul 3, 2022 |
weak |
Dynamic Programming |
Longest Common Subsequence |
Jul 3, 2022 |
weak |
HashTable |
Top K Frequent Words |
Jul 4, 2022 |
moderate |
HashTable |
Missing Number I |
Jul 4, 2022 |
moderate |
HashTable |
Common Numbers Of Two Sorted Arrays(Array version) |
Jul 4, 2022 |
moderate |
HashTable |
Remove Certain Characters |
Jul 4, 2022 |
moderate |
String |
Remove Spaces |
Jul 4, 2022 |
weak |
String |
Remove Adjacent Repeated Characters I |
Jul 4, 2022 |
moderate |
String |
Remove Adjacent Repeated Characters IV |
Jul 4, 2022 |
moderate |
String |
Reverse String |
Jul 6, 2022 |
strong |
String |
Reverse Words In A Sentence I |
Jul 6, 2022 |
moderate |
String |
Right Shift By N Characters |
Jul 6, 2022 |
moderate |
String |
String Replace |
Jul 6, 2022 |
weak |
String |
All Permutations II |
Jul 8, 2022 |
moderate |
String |
Reoder Array |
Jul 8, 2022 |
weak |
String |
Compress String II |
Jul 8, 2022 |
weak |
String |
Decompress String II |
Jul 8, 2022 |
weak |
String |
Decompress String II |
Jul 9, 2022 |
weak |
String |
Longest Substring Without Repeating Characters |
Jul 9, 2022 |
weak |
String |
All Anagrams |
Jul 9, 2022 |
weak |
String |
Longest subarray contains only 1s |
Jul 9, 2022 |
weak |
Recursion |
Store Number Of Nodes In Left Subtree |
Jul 10, 2022 |
moderate |
Recursion |
Lowest Common Ancestor I |
Jul 10, 2022 |
moderate |
BFS |
Check If Binary Tree Is Completed |
Jul 10, 2022 |
moderate |
Recursion |
Reverse Linked List In Pairs |
Jul 10, 2022 |
moderate |
Recursion |
Max Path Sum From Leaf To Root |
Jun 10, 2022 |
moderate |
String |
Longest subarray contains only 1s |
Jul 11, 2022 |
moderate |
String |
Longest Repeating Character Replacement (leetcode 424) |
Jul 11, 2022 |
moderate |
Binary Tree |
Lowest Common Ancestor II |
Jul 12, 2022 |
moderate |
Binary Tree |
Lowest Common Ancestor Binary Search Tree I |
Jul 12, 2022 |
moderate |
Binary Tree |
Lowest Common Ancestor V |
Jul 12, 2022 |
moderate |
Binary Tree |
Lowest Common Ancestor VI |
Jul 12, 2022 |
moderate |
Binary Tree |
Lowest Common Ancestor IV |
Jul 12, 2022 |
moderate |
Array |
Array Deduplication I |
Jul 12, 2022 |
moderate |
Array |
Array Deduplication II |
Jul 12, 2022 |
moderate |
Array |
Array Deduplication III |
Jul 12, 2022 |
moderate |
Array |
Array Deduplication IV |
Jul 14, 2022 |
moderate |
Array |
Largest ans Smallest |
Jul 14, 2022 |
moderate |
Array |
Largest ans Second Largest |
Jul 14, 2022 |
moderate |
Array |
Closest Number In Binary Search Tree II |
Jul 14, 2022 |
weak |
Array |
Move 0s To The End II |
Jul 15, 2022 |
moderate |
Array |
Rotate Matrix |
Jul 15, 2022 |
moderate |
Array |
Get Keys In Binary Tree Layer By Layer Zig-Zag Order |
Jul 15, 2022 |
weak |
Binary Tree |
Search In Binary Search Tree |
Jul 15, 2022 |
moderate |
Binary Tree |
Delete In Binary Search Tree |
Jul 15, 2022 |
weak |
Sorting |
Merge Sort |
Jul 16, 2022 |
weak |
Sorting |
Quick Sort |
Jul 16, 2022 |
weak |
Sorting |
Rainbow Sort |
Jul 16, 2022 |
weak |
Array |
2 Sum |
Jul 17, 2022 |
weak |
Array |
2 Sum All Pair I |
Jul 17, 2022 |
weak |
Array |
2 Sum All Pair II |
Jul 18, 2022 |
weak |
Array |
3 Sum |
Jul 18, 2022 |
weak |
Array |
4 Sum |
Jul 18, 2022 |
weak |
Binary Tree |
Closest Number In Binary Search Tree |
Jul 18, 2022 |
weak |
Binary Tree |
Largest Number Smaller In Binary Search Tree |
Jul 18, 2022 |
weak |
Linked List |
Deep Copy Linked List With Random Pointer |
Jul 18, 2022 |
weak |
DFS |
Deep Copy Undirected Graph |
Jul 18, 2022 |
weak |
Array |
Common Numbers Of Two Arrays I(Array version) |
Jul 18, 2022 |
moderate |
Array |
Merge K Sorted Array |
Jul 23, 2022 |
weak |
Linked List |
Merge K Sorted List |
Jul 23, 2022 |
weak |
Array |
Common Elements In Three Sorted Array |
Jul 23, 2022 |
weak |
Array |
Common Elements In K Sorted Array |
Jul 23, 2022 |
weak |
Array |
Max Water Trapped I |
Jul 23, 2022 |
weak |
BFS |
Kth Smallest Sum In Two Sorted Array |
Jul 24, 2022 |
weak |
Array |
Maximum Values Of Size K Sliding Windows |
Jul 24, 2022 |
weak |
Array |
Majority Number I |
Jul 24, 2022 |
weak |
Array |
Majority Number II |
Jul 24, 2022 |
weak |
OOD |
First Non-Repeating Character In Stream |
Jul 26, 2022 |
weak |
OOD |
Implement LRU Cache |
Jul 26, 2022 |
weak |
Array |
Kth Smallest In Two Sorted Arrays |
Jul 26, 2022 |
weak |
Array |
Majority Number III |
Jul 26, 2022 |
weak |
Linked List |
Check If Linked List Is Palindrome |
Jul 29, 2022 |
weak |
Linked List |
Remove Linked List Element |
Jul 29, 2022 |
weak |
Binary Search |
Search In Unknown Sized Sorted Array |
Jul 29, 2022 |
weak |
Stack & Queue |
Deque By Three Stacks |
Jul 29, 2022 |
weak |
Binary Tree |
Pre-order Traversal Of Binary Tree (Iterative) |
Jul 29, 2022 |
weak |
Binary Tree |
In-order Traversal Of Binary Tree (Iterative) |
Jul 29, 2022 |
weak |
Dynamic Programming |
Minimum Cuts For Palindrome |
Jul 30, 2022 |
weak |
Dynamic Programming |
Can I Win II |
Jul 30, 2022 |
weak |
Linked List |
Reorder Linked List |
Jul 30, 2022 |
weak |
DFS |
All Subsets II |
Aug 26, 2022 |
moderate |
Recursion |
Reconstruct Binary Tree With Levelorder And Inorder |
Aug 26, 2022 |
moderate |
Dynamic Programming |
Coin Change |
Aug 31, 2022 |
weak |
Dynamic Programming |
Coin Change 2 |
Aug 31, 2022 |
weak |
Dynamic Programming |
Target Sum |
Aug 31, 2022 |
weak |
OOD
First Non-Repeating Character In Stream
Given a stream of characters, find the first non-repeating character from stream. You need to tell the first non-repeating character in O(1) time at any moment.
1
2
3
4
5
6
7
8
9
10
11
12
13
|
Implement two methods of the class:
read() - read one character from the stream
firstNonRepeating() - return the first non-repoeating character from the stream at any time when calling the method, return null if there does not exist such characters
Examples:
read:
a b c a c c b
firstNonRepeating:
a a a b b b null
|
Google Doc Link
Implement LRU Cache
Implement a least recently used cache. It should provide set(), get() operations. If not exists, return null (Java), false (C++), -1(Python).
Google Doc Link
Linked List
Reorder Linked List
Reorder the given singly-linked list N1 -> N2 -> N3 -> N4 -> … -> Nn -> null to be N1- > Nn -> N2 -> Nn-1 -> N3 -> Nn-2 -> … -> null
Examples
L = null, is reordered to null
L = 1 -> null, is reordered to 1 -> null
L = 1 -> 2 -> 3 -> 4 -> null, is reordered to 1 -> 4 -> 2 -> 3 -> null
L = 1 -> 2 -> 3 -> null, is reordred to 1 -> 3 -> 2 -> null
Google Doc Link
Deep Copy Linked List With Random Pointer
Each of the nodes in the linked list has another pointer pointing to a random node in the list or null. Make a deep copy of the original list.
Google Doc Link
Array
4 Sum
Determine if there exists a set of four elements in a given array that sum to the given target number.
1
2
3
4
5
6
7
8
|
Assumptions
The given array is not null and has length of at least 4
Examples
A = {1, 2, 2, 3, 4}, target = 9, return true(1 + 2 + 2 + 4 = 9)
A = {1, 2, 2, 3, 4}, target = 12, return false
|
Google Doc Link
2 Sum All Pair I
Find all pairs of elements in a given array that sum to the pair the given target number. Return all the distinct pairs of values.
1
2
3
4
5
6
7
|
Assumptions
The given array is not null and has length of at least 2
The order of the values in the pair does not matter
Examples
A = {2, 1, 3, 2, 4, 3, 4, 2}, target = 6, return [[2, 4], [3, 3]]
|
Google Doc Link
2 Sum All Pair I
Find all pairs of elements in a given array that sum to the given target number. Return all the pairs of indices.
1
2
3
4
5
6
7
8
9
|
Assumptions
The given array is not null and has length of at least 2.
Examples
A = {1, 3, 2, 4}, target = 5, return [[0, 3], [1, 2]]
A = {1, 2, 2, 4}, target = 6, return [[1, 3], [2, 3]]
|
Google Doc Link
Sorting
Merge Sort
Given an array of integers, sort the elements in the array in ascending order. The merge sort algorithm should be used to solve this problem.
Examples
{1} is sorted to {1}
{1, 2, 3} is sorted to {1, 2, 3}
{3, 2, 1} is sorted to {1, 2, 3}
{4, 2, -3, 6, 1} is sorted to {-3, 1, 2, 4, 6}
Corner Cases
What if the given array is null? In this case, we do not need to do anything.
What if the given array is of length zero? In this case, we do not need to do anything.
Google Doc Link
Quick Sort
Given an array of integers, sort the elements in the array in ascending order. The quick sort algorithm should be used to solve this problem.
Examples
{1} is sorted to {1}
{1, 2, 3} is sorted to {1, 2, 3}
{3, 2, 1} is sorted to {1, 2, 3}
{4, 2, -3, 6, 1} is sorted to {-3, 1, 2, 4, 6}
Corner Cases
What if the given array is null? In this case, we do not need to do anything.
What if the given array is of length zero? In this case, we do not need to do anything.
Google Doc Link
Rainbow Sort
Given an array of balls, where the color of the balls can only be Red, Green or Blue, sort the balls such that all the Red balls are grouped on the left side, all the Green balls are grouped in the middle and all the Blue balls are grouped on the right side. (Red is denoted by -1, Green is denoted by 0, and Blue is denoted by 1).
Examples
{0} is sorted to {0}
{1, 0} is sorted to {0, 1}
{1, 0, 1, -1, 0} is sorted to {-1, 0, 0, 1, 1}
Assumptions
The input array is not null.
Corner Cases
What if the input array is of length zero? In this case, we should not do anything as well.
Google Doc Link
String
Longest Repeating Character Replacement (leetcode 424)
You are given a string s and an integer k. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most k times.
Return the length of the longest substring containing the same letter you can get after performing the above operations.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
Example 1:
Input: s = "ABAB", k = 2
Output: 4
Explanation: Replace the two 'A's with two 'B's or vice versa.
Example 2:
Input: s = "AABABBA", k = 1
Output: 4
Explanation: Replace the one 'A' in the middle with 'B' and form "AABBBBA".
The substring "BBBB" has the longest repeating letters, which is 4.
Constraints:
1 <= s.length <= 105
s consists of only uppercase English letters.
0 <= k <= s.length
|
Google Doc Link
Longest subarray contains only 1s
Given an array of integers that contains only 0s and 1s and a positive integer k, you can flip at most k 0s to 1s, return the longest subarray that contains only integer 1 after flipping.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
Assumptions:
1. Length of given array is between [1, 20000].
2. The given array only contains 1s and 0s.
3. 0 <= k <= length of given array.
Example 1:
Input: array = [1,1,0,0,1,1,1,0,0,0], k = 2
Output: 7
Explanation: flip 0s at index 2 and 3, then the array becomes [1,1,1,1,1,1,1,0,0,0], so that the length of longest subarray that contains only integer 1 is 7.
Example 2:
Input: array = [1,1,0,0,1,1,1,0,0,0], k = 0
Output: 3
Explanation: k is 0 so you can not flip any 0 to 1, then the length of longest subarray that contains only integer 1 is 3.
|
Google Doc Link
All Anagrams
Find all occurrence of anagrams of a given string s in a given string l. Return the list of starting indices.
1
2
3
4
5
6
7
|
Assumptions
sh is not null or empty.
lo is not null.
Examples
l = "abcbac", s = "ab", return [0, 3] since the substring with length 2 starting from index 0/3 are all anagrams of "ab" ("ab", "ba").
|
Google Doc Link
Longest Substring Without Repeating Characters
Given a string, find the longest substring without any repeating characters and return the length of it. The input string is guaranteed to be not null.
For example, the longest substring without repeating letters for “bcdfbd” is “bcdf”, we should return 4 in this case.
Google Doc Link
Decompress String II
Given a string in compressed form, decompress it to the original string. The adjacent repeated characters in the original string are compressed to have the character followed by the number of repeated occurrences.
1
2
3
4
5
6
7
8
9
10
11
|
Assumptions
The string is not null
The characters used in the original string are guaranteed to be ‘a’ - ‘z’
There are no adjacent repeated characters with length > 9
Examples
“a1c0b2c4” → “abbcccc”
|
Google Doc Link
Compress String II
Given a string, replace adjacent, repeated characters with the character followed by the number of repeated occurrences.
1
2
3
4
5
6
7
8
9
|
Assumptions
The string is not null
The characters used in the original string are guaranteed to be ‘a’ - ‘z’
Examples
“abbcccdeee” → “a1b2c3d1e3”
|
Google Doc Link
Reorder Array
Given an array of elements, reorder it as follow:
{ N1, N2, N3, …, N2k } → { N1, Nk+1, N2, Nk+2, N3, Nk+3, … , Nk, N2k }
{ N1, N2, N3, …, N2k+1 } → { N1, Nk+1, N2, Nk+2, N3, Nk+3, … , Nk, N2k, N2k+1 }
Try to do it in place.
1
2
3
4
5
6
7
8
9
10
|
Assumptions
The given array is not null
Examples
{ 1, 2, 3, 4, 5, 6} → { 1, 4, 2, 5, 3, 6 }
{ 1, 2, 3, 4, 5, 6, 7, 8 } → { 1, 5, 2, 6, 3, 7, 4, 8 }
{ 1, 2, 3, 4, 5, 6, 7 } → { 1, 4, 2, 5, 3, 6, 7 }
|
Google Doc Link
All Permutations II
Given a string with possible duplicate characters, return a list with all permutations of the characters.
1
2
3
4
5
6
|
Examples
Set = “abc”, all permutations are [“abc”, “acb”, “bac”, “bca”, “cab”, “cba”]
Set = "aba", all permutations are ["aab", "aba", "baa"]
Set = "", all permutations are [""]
Set = null, all permutations are []
|
Google Doc Link
String Replace
Given an original string input, and two strings S and T, from left to right replace all occurrences of S in input with T.
1
2
3
4
5
6
7
|
Assumptions
input, S and T are not null, S is not empty string
Examples
input = "appledogapple", S = "apple", T = "cat", input becomes "catdogcat"
input = "laicode", S = "code", T = "offer", input becomes "laioffer"
|
Google Doc Link
Remove Adjacent Repeated Characters I
Remove adjacent, repeated characters in a given string, leaving only one character for each group of such characters.
1
2
3
4
5
6
7
8
9
|
Assumptions
Try to do it in place.
Examples
“aaaabbbc” is transferred to “abc”
Corner Cases
If the given string is null, returning null or an empty string are both valid.
|
Google Doc Link
Remove Spaces
Given a string, remove all leading/trailing/duplicated empty spaces.
1
2
3
4
5
6
7
|
Assumptions:
The given string is not null.
Examples:
“ a” --> “a”
“ I love MTV ” --> “I love MTV”
|
Google Doc Link
HashTable
Remove Certain Characters
Remove given characters in input string, the relative order of other characters should be remained. Return the new string after deletion.
1
2
3
4
5
6
7
|
Assumptions
The given input string is not null.
The characters to be removed is given by another string, it is guaranteed to be not null.
Examples
input = "abcd", t = "ab", delete all instances of 'a' and 'b', the result is "cd".
|
Google Doc Link
Missing Number I
Given an integer array of size N - 1, containing all the numbers from 1 to N except one, find the missing number.
1
2
3
4
5
6
7
8
|
Assumptions
The given array is not null, and N >= 1
Examples
A = {2, 1, 4}, the missing number is 3
A = {1, 2, 3}, the missing number is 4
A = {}, the missing number is 1
|
Google Doc Link
Top K Frequent Words
Given a composition with different kinds of words, return a list of the top K most frequent words in the composition.
1
2
3
4
5
6
7
8
9
10
11
12
|
Assumptions
the composition is not null and is not guaranteed to be sorted
K >= 1 and K could be larger than the number of distinct words in the composition, in this case, just return all the distinct words
Return
a list of words ordered from most frequent one to least frequent one (the list could be of size K or smaller than K)
Examples
Composition = ["a", "a", "b", "b", "b", "b", "c", "c", "c", "d"], top 2 frequent words are [“b”, “c”]
Composition = ["a", "a", "b", "b", "b", "b", "c", "c", "c", "d"], top 4 frequent words are [“b”, “c”, "a", "d"]
Composition = ["a", "a", "b", "b", "b", "b", "c", "c", "c", "d"], top 5 frequent words are [“b”, “c”, "a", "d"]
|
Google Doc Link
Two Pointers
Find the Duplicate Number
Given an array of integers nums containing n + 1 integers where each integer is in the range [1, n] inclusive.
There is only one repeated number in nums, return this repeated number.
You must solve the problem without modifying the array nums and uses only constant extra space.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
Example 1:
Input: nums = [1,3,4,2,2]
Output: 2
Example 2:
Input: nums = [3,1,3,4,2]
Output: 3
Constraints:
1 <= n <= 105
nums.length == n + 1
1 <= nums[i] <= n
All the integers in nums appear only once except for precisely one integer which appears two or more times.
Follow up:
How can we prove that at least one duplicate number must exist in nums?
Can you solve the problem in linear runtime complexity?
|
Google Doc Link
Binary Search
Classical Binary Search
Given a target integer T and an integer array A sorted in ascending order, find the index i such that A[i] == T or return -1 if there is no such index.
1
2
3
4
5
6
7
8
9
10
11
12
13
|
Assumptions
There can be duplicate elements in the array, and you can return any of the indices i such that A[i] == T.
Examples
A = {1, 2, 3, 4, 5}, T = 3, return 2
A = {1, 2, 3, 4, 5}, T = 6, return -1
A = {1, 2, 2, 2, 3, 4}, T = 2, return 1 or 2 or 3
Corner Cases
What if A is null or A is of zero length? We should return -1 in this case.
|
Google Doc Link
K Closest In Sorted Array
Given a target integer T, a non-negative integer K and an integer array A sorted in ascending order, find the K closest numbers to T in A. If there is a tie, the smaller elements are always preferred.
1
2
3
4
5
6
7
8
9
10
11
|
Assumptions
A is not null
K is guranteed to be >= 0 and K is guranteed to be <= A.length
Return
A size K integer array containing the K closest numbers(not indices) in A, sorted in ascending order by the difference between the number and T.
Examples
A = {1, 2, 3}, T = 2, K = 3, return {2, 1, 3} or {2, 3, 1}
A = {1, 4, 6, 8}, T = 3, K = 3, return {4, 1, 6}
|
Google Doc Link
Smallest Element Larger than Target
Given a target integer T and an integer array A sorted in ascending order, find the index of the smallest element in A that is larger than T or return -1 if there is no such index.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
Assumptions
There can be duplicate elements in the array.
Examples
A = {1, 2, 3}, T = 1, return 1
A = {1, 2, 3}, T = 3, return -1
A = {1, 2, 2, 2, 3}, T = 1, return 1
Corner Cases
What if A is null or A of zero length? We should return -1 in this case.
|
Google Doc Link
Linked List
Reverse Linked List Iteratively
Reverse a singly-linked list iteratively.
1
2
3
4
5
|
Examples
L = null, return null
L = 1 -> null, return 1 -> null
L = 1 -> 2 -> 3 -> null, return 3 -> 2 -> 1 -> null
|
Google Doc Link
Reverse Linked List Recursively
Reverse a singly-linked list recursively.
1
2
3
4
5
|
Examples
L = null, return null
L = 1 -> null, return 1 -> null
L = 1 -> 2 -> 3 -> null, return 3 -> 2 -> 1 -> null
|
Google Doc Link
Partition Linked List
Given a linked list and a target value T, partition it such that all nodes less than T are listed before the nodes larger than or equal to target value T. The original relative order of the nodes in each of the two partitions should be preserved.
1
2
3
4
|
Examples
L = 2 -> 4 -> 3 -> 5 -> 1 -> null, T = 3,
is partitioned to 2 -> 1 -> 4 -> 3 -> 5 -> null
|
Google Doc Link
Merge Sort Lnked List
Given a singly-linked list, where each node contains an integer value, sort it in ascending order. The merge sort algorithm should be used to solve this problem.
1
2
3
4
5
6
|
Examples
null, is sorted to null
1 -> null, is sorted to 1 -> null
1 -> 2 -> 3 -> null, is sorted to 1 -> 2 -> 3 -> null
4 -> 2 -> 6 -> -3 -> 5 -> null, is sorted to -3 -> 2 -> 4 -> 5 -> 6
|
Google Doc Link
Add Two Numbers
You are given two linked lists representing two non-negative numbers. 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.
1
2
3
4
5
|
Example
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
|
Google Doc Link
Move 0s To The End I
Given an array of integers, move all the 0s to the right end of the array.
The relative order of the elements in the original array does not need to be maintained.
1
2
3
4
5
6
7
8
|
Assumptions:
The given array is not null.
Examples:
{1} --> {1}
{1, 0, 3, 0, 1} --> {1, 3, 1, 0, 0} or {1, 1, 3, 0, 0} or {3, 1, 1, 0, 0}
|
Google Doc Link
Queue & Stack
Sort With 2 Stacks
Given an array that is initially stored in one stack, sort it with one additional stacks (total 2 stacks).
After sorting the original stack should contain the sorted integers and from top to bottom the integers are sorted in ascending order.
1
2
3
4
5
6
7
8
|
Assumptions:
The given stack is not null.
There can be duplicated numbers in the give stack.
Requirements:
No additional memory, time complexity = O(n ^ 2).
|
Google Doc Link
Stack by Queue(s)
Implement a stack containing integers using queue(s). The stack should provide push(x), pop(), top() and isEmpty() operations.
In java: if the stack is empty, then top() and pop() will return null.
Google Doc Link
Binary Tree & Binary Search Tree
Largest Number Smaller In Binary Search Tree
In a binary search tree, find the node containing the largest number smaller than the given target number.
If there is no such number, return -2^31.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
Assumptions:
The given root is not null.
There are no duplicate keys in the binary search tree.
Examples
5
/ \
2 11
/ \
6 14
largest number smaller than 1 is Integer.MIN_VALUE(Java) or INT_MIN(c++)
largest number smaller than 10 is 6
largest number smaller than 6 is 5
How is the binary tree represented?
We use the level order traversal sequence with a special symbol "#" denoting the null node.
For Example:
The sequence [1, 2, 3, #, #, 4] represents the following binary tree:
1
/ \
2 3
/
4
|
Google Doc Link
Pre-order Traversal Of Binary Tree (recursive)
Implement a recursive, pre-order traversal of a given binary tree, return the list of keys of each node in the tree as it is pre-order traversed.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
Examples
5
/ \
3 8
/ \ \
1 4 11
Pre-order traversal is [5, 3, 1, 4, 8, 11]
Corner Cases
What if the given binary tree is null? Return an empty list in this case.
How is the binary tree represented?
We use the level order traversal sequence with a special symbol "#" denoting the null node.
For Example:
The sequence [1, 2, 3, #, #, 4] represents the following binary tree:
1
/ \
2 3
/
4
|
Google Doc Link
Check If Binary Tree Is Balanced
Check if a given binary tree is balanced. A balanced binary tree is one in which the depths of every node’s left and right subtree differ by at most 1.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
Examples
5
/ \
3 8
/ \ \
1 4 11
is balanced binary tree,
5
/
3
/ \
1 4
is not balanced binary tree.
Corner Cases
What if the binary tree is null? Return true in this case.
How is the binary tree represented?
We use the level order traversal sequence with a special symbol "#" denoting the null node.
For Example:
The sequence [1, 2, 3, #, #, 4] represents the following binary tree:
1
/ \
2 3
/
4
|
Google Doc Link
Symmetric Binary Tree
Check if a given binary tree is symmetric.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
Examples
5
/ \
3 3
/ \ / \
1 4 4 1
is symmetric.
5
/ \
3 3
/ \ / \
1 4 1 4
is not symmetric.
Corner Cases
What if the binary tree is null? Return true in this case.
How is the binary tree represented?
We use the level order traversal sequence with a special symbol "#" denoting the null node.
For Example:
The sequence [1, 2, 3, #, #, 4] represents the following binary tree:
1
/ \
2 3
/
4
|
Google Doc Link
Search in BST
Find the target key K in the given binary search tree, return the node that contains the key if K is found, otherwise return null.
1
2
3
|
Assumptions
There are no duplicate keys in the binary search tree
|
Google Doc Link
Lowest Common Ancestor II
Given two nodes in a binary tree (with parent pointer available), find their lowest common ancestor.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
Assumptions
There is parent pointer for the nodes in the binary tree
The given two nodes are not guaranteed to be in the binary tree
Examples
5
/ \
9 12
/ \ \
2 3 14
The lowest common ancestor of 2 and 14 is 5
The lowest common ancestor of 2 and 9 is 9
The lowest common ancestor of 2 and 8 is null (8 is not in the tree)
|
Google Doc Link
Insert In Binary Search Tree
Insert a key in a binary search tree if the binary search tree does not already contain the key. Return the root of the binary search tree.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
Assumptions
There are no duplicate keys in the binary search tree
If the key is already existed in the binary search tree, you do not need to do anything
Examples
5
/ \
3 8
/ \
1 4
insert 11, the tree becomes
5
/ \
3 8
/ \ \
1 4 11
insert 6, the tree becomes
5
/ \
3 8
/ \ / \
1 4 6 11
|
Google Doc Link
HashMap
Design HashMap From Scratch
Dynamic Programming
House Robber 2
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, adjacent houses have a security system connected, and it will automatically contact the police if two adjacent houses were broken into on the same night.
Given an integer array nums representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
Example 1:
Input: nums = [2,3,2]
Output: 3
Explanation: You cannot rob house 1 (money = 2) and then rob house 3 (money = 2), because they are adjacent houses.
Example 2:
Input: nums = [1,2,3,1]
Output: 4
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
Total amount you can rob = 1 + 3 = 4.
Example 3:
Input: nums = [1,2,3]
Output: 3
Constraints:
1 <= nums.length <= 100
0 <= nums[i] <= 1000
|
Google Doc Link
House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
Given an integer array nums representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
Example 1:
Input: nums = [1,2,3,1]
Output: 4
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
Total amount you can rob = 1 + 3 = 4.
Example 2:
Input: nums = [2,7,9,3,1]
Output: 12
Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1).
Total amount you can rob = 2 + 9 + 1 = 12.
Constraints:
1 <= nums.length <= 100
0 <= nums[i] <= 400
|
Google Doc Link
Target Sum
You are given an integer array nums and an integer target.
You want to build an expression out of nums by adding one of the symbols ‘+’ and ‘-’ before each integer in nums and then concatenate all the integers.
For example, if nums = [2, 1], you can add a ‘+’ before 2 and a ‘-’ before 1 and concatenate them to build the expression “+2-1”.
Return the number of different expressions that you can build, which evaluates to target.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
Example 1:
Input: nums = [1,1,1,1,1], target = 3
Output: 5
Explanation: There are 5 ways to assign symbols to make the sum of nums be target 3.
-1 + 1 + 1 + 1 + 1 = 3
+1 - 1 + 1 + 1 + 1 = 3
+1 + 1 - 1 + 1 + 1 = 3
+1 + 1 + 1 - 1 + 1 = 3
+1 + 1 + 1 + 1 - 1 = 3
Example 2:
Input: nums = [1], target = 1
Output: 1
Constraints:
1 <= nums.length <= 20
0 <= nums[i] <= 1000
0 <= sum(nums[i]) <= 1000
-1000 <= target <= 1000
|
Google Doc Link
Coin Change 2
You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money.
Return the number of combinations that make up that amount. If that amount of money cannot be made up by any combination of the coins, return 0.
You may assume that you have an infinite number of each kind of coin.
The answer is guaranteed to fit into a signed 32-bit integer.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
Example 1:
Input: amount = 5, coins = [1,2,5]
Output: 4
Explanation: there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
Example 2:
Input: amount = 3, coins = [2]
Output: 0
Explanation: the amount of 3 cannot be made up just with coins of 2.
Example 3:
Input: amount = 10, coins = [10]
Output: 1
Constraints:
1 <= coins.length <= 300
1 <= coins[i] <= 5000
All the values of coins are unique.
0 <= amount <= 5000
|
Google Doc Link
Coin Change
You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money.
Return the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104
|
Google Doc Link
Longgest Common Subsequence
Find the length of longest common subsequence of two given strings.
1
2
3
4
5
6
|
Assumptions
The two given strings are not null
Examples
S = “abcde”, T = “cbabdfe”, the longest common subsequence of s and t is {‘a’, ‘b’, ‘d’, ‘e’}, the length is 4.
|
Google Doc Link
Longgest Common Substring
Find the longest common substring of two given strings.
1
2
3
4
5
6
|
Assumptions
The two given strings are not null
Examples
S = “abcde”, T = “cdf”, the longest common substring of S and T is “cd”
|
Google Doc Link
Largest Set Of Points With Positive Slope
Given an array of 2D coordinates of points (all the coordinates are integers), find the largest number of points that can form a set such that any pair of points in the set can form a line with positive slope. Return the size of such a maximal set.
1
2
3
4
5
6
7
|
Assumptions
The given array is not null
Note: if there does not even exist 2 points can form a line with positive slope, should return 0.
Examples
<0, 0>, <1, 1>, <2, 3>, <3, 3>, the maximum set of points are {<0, 0>, <1, 1>, <2, 3>}, the size is 3.
|
Google Doc Link
Count Ascending Subsequence
Given an array A[0]…A[n-1] of integers, count the number of ascending subsequences.
In case that the result is larger than 32-bit integer, return the result in 10^9+7 modulo.
1
2
3
4
5
6
7
|
Assumptions
A is not null
Examples
Input: A = {1,2,3}
Output: 7
Explanation: [1],[2],[3],[1,2],[1,3],[2,3],[1,2,3]
|
Google Doc Link
Longest Ascending Subsequence II
Given an array A[0]…A[n-1] of integers, find out the longest ascending subsequence. If there are multiple results, then return any valid result.
1
2
3
4
5
6
7
|
Assumptions
A is not null
Examples
Input: A = {5, 2, 6, 3, 4, 7, 5}
Output: [2,3,4,5]
Because [2, 3, 4, 5] is one of the longest ascending subsequences.
|
Google Doc Link
Longest Ascending Subsequence
Given an array A[0]…A[n-1] of integers, find out the length of the longest ascending subsequence.
1
2
3
4
5
6
7
|
Assumptions
A is not null
Examples
Input: A = {5, 2, 6, 3, 4, 7, 5}
Output: 4
Because [2, 3, 4, 5] is the longest ascending subsequence.
|
Google Doc Link
Largest Square Of Matches
Determine the largest square surrounded by a bunch of matches (each match is either horizontal or vertical), return the length of the largest square.
1
2
3
4
5
6
7
8
9
10
11
|
The input is a matrix of points. Each point has one of the following values:
0 - there is no match to its right or bottom.
1 - there is a match to its right.
2 - there is a match to its bottom.
3 - there is a match to its right, and a match to its bottom.
Assumptions
The given matrix is guaranteed to be of size M * N, where M, N >= 0
|
This matrix represents the following bunch of matches:
The largest square has length of 2.
Google Doc Link
Largest Square Surrounded By One
Determine the largest square surrounded by 1s in a binary matrix (a binary matrix only contains 0 and 1), return the length of the largest square.
1
2
3
4
5
|
Assumptions
The given matrix is guaranteed to be of size M * N, where M, N >= 0
The largest square surrounded by 1s has length of 3.
|
Google Doc Link
Largest X Of 1s
Given a matrix that contains only 1s and 0s, find the largest X shape which contains only 1s, with the same arm lengths and the four arms joining at the central point.
Return the arm length of the largest X shape.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
Assumptions
The given matrix is not null, has size of N * M, N >= 0 and M >= 0.
Examples
{ {0, 0, 0, 0},
{1, 1, 1, 1},
{0, 1, 1, 1},
{1, 0, 1, 1} }
the largest X of 1s has arm length 2.
|
Google Doc Link
Longest Cross Of 1s
Given a matrix that contains only 1s and 0s, find the largest cross which contains only 1s, with the same arm lengths and the four arms joining at the central point.
Return the arm length of the largest cross.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
Assumptions
The given matrix is not null, has size of N * M, N >= 0 and M >= 0.
Examples
{ {0, 0, 0, 0},
{1, 1, 1, 1},
{0, 1, 1, 1},
{1, 0, 1, 1} }
the largest cross of 1s has arm length 2.
|
Google Doc Link
Largest Square Of 1s
Determine the largest square of 1s in a binary matrix (a binary matrix only contains 0 and 1), return the length of the largest square.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
Assumptions
The given matrix is not null and guaranteed to be of size N * N, N >= 0
Examples
{ {0, 0, 0, 0},
{1, 1, 1, 1},
{0, 1, 1, 1},
{1, 0, 1, 1}}
the largest square of 1s has length of 2
|
Google Doc Link
Edit Distance
Given two strings of alphanumeric characters, determine the minimum number of Replace, Delete, and Insert operations needed to transform one string into the other.
1
2
3
4
5
6
7
8
|
Assumptions
Both strings are not null
Examples
string one: “sigh”, string two : “asith”
the edit distance between one and two is 2 (one insert “a” at front then replace “g” with “t”).
|
Google Doc Link
Longest Ascending SubArray
Given an unsorted array, find the length of the longest subarray in which the numbers are in ascending order.
1
2
3
4
5
6
7
8
|
Assumptions
The given array is not null
Examples
{7, 2, 3, 1, 5, 8, 9, 6}, longest ascending subarray is {1, 5, 8, 9}, length is 4.
{1, 2, 3, 3, 4, 4, 5}, longest ascending subarray is {1, 2, 3}, length is 3.
|
Google Doc Link
Dictionary Word I
Given a word and a dictionary, determine if it can be composed by concatenating words from the given dictionary.
1
2
3
4
5
6
7
8
9
10
11
|
Assumptions
The given word is not null and is not empty
The given dictionary is not null and is not empty and all the words in the dictionary are not null or empty
Examples
Dictionary: {“bob”, “cat”, “rob”}
Word: “robob” return false
Word: “robcatbob” return true since it can be composed by "rob", "cat", "bob"
|
Google Doc Link
Fibonacci Number
Get the Kth number in the Fibonacci Sequence. (K is 0-indexed, the 0th Fibonacci number is 0 and the 1st Fibonacci number is 1).
1
2
3
4
5
6
7
8
9
10
11
|
Examples
0th fibonacci number is 0
1st fibonacci number is 1
2nd fibonacci number is 1
3rd fibonacci number is 2
6th fibonacci number is 8
Corner Cases
What if K < 0? in this case, we should always return 0.
Is it possible the result fibonacci number is overflowed? We can assume it will not be overflowed when we solve this problem on this online judge, but we should also know that it is possible to get an overflowed number, and sometimes we will need to use something like BigInteger.
|
Google Doc Link
Max Product Of Cutting Rope
Given a rope with positive integer-length n, how to cut the rope into m integer-length parts with length p[0]
, p[1], ...,p[m-1]
, in order to get the maximal product of p[0]*p[1]* ... *p[m-1]
? m is determined by you and must be greater than 0 (at least one cut must be made). Return the max product you can have.
1
2
3
4
5
6
|
Assumptions
n >= 2
Examples
n = 12, the max product is 3 * 3 * 3 * 3 = 81(cut the rope into 4 pieces with length of each is 3).
|
Google Doc Link
Array Hopper I
Given an array A of non-negative integers, you are initially positioned at index 0 of the array. A[i] means the maximum jump distance from that position (you can only jump towards the end of the array). Determine if you are able to reach the last index.
1
2
3
4
5
6
7
8
|
Assumptions
The given array is not null and has length of at least 1.
Examples
{1, 3, 2, 0, 3}, we are able to reach the end of array(jump to index 1 then reach the end of the array)
{2, 1, 1, 0, 2}, we are not able to reach the end of array
|
Google Doc Link
Array Hopper II
Given an array A of non-negative integers, you are initially positioned at index 0 of the array. A[i] means the maximum jump distance from index i (you can only jump towards the end of the array). Determine the minimum number of jumps you need to reach the end of array. If you can not reach the end of the array, return -1.
1
2
3
4
5
6
7
8
|
Assumptions
The given array is not null and has length of at least 1.
Examples
{3, 3, 1, 0, 4}, the minimum jumps needed is 2 (jump to index 1 then to the end of array)
{2, 1, 1, 0, 2}, you are not able to reach the end of array, return -1 in this case.
|
Google Doc Link
Largest Subarray Sum
Given an unsorted integer array, find the subarray that has the greatest sum. Return the sum.
1
2
3
4
5
6
7
8
|
Assumptions
The given array is not null and has length of at least 1.
Examples
{2, -1, 4, -2, 1}, the largest subarray sum is 2 + (-1) + 4 = 5
{-2, -1, -3}, the largest subarray sum is -1
|
Google Doc Link
DFS
Deep Copy Undirected Graph
Make a deep copy of an undirected graph, there could be cycles in the original graph.
Assumptions
The given graph is not null
Google Doc Link
Two Subsets With Min Difference
Given a set of n integers, divide the set in two subsets of n/2 sizes each such that the difference of the sum of two subsets is as minimum as possible.
Return the minimum difference(absolute value).
1
2
3
4
5
6
|
Assumptions:
The given integer array is not null and it has length of >= 2.
Examples:
{1, 3, 2} can be divided into {1, 2} and {3}, the minimum difference is 0
|
Google Doc Link
Restore IP Addresses
A valid IP address consists of exactly four integers separated by single dots. Each integer is between 0 and 255 (inclusive) and cannot have leading zeros.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
For example, "0.1.2.201" and "192.168.1.1" are valid IP addresses, but "0.011.255.245", "192.168.1.312" and "192.168@1.1" are invalid IP addresses.
Given a string s containing only digits, return all possible valid IP addresses that can be formed by inserting dots into s. You are not allowed to reorder or remove any digits in s. You may return the valid IP addresses in any order.
Example 1:
Input: s = "25525511135"
Output: ["255.255.11.135","255.255.111.35"]
Example 2:
Input: s = "0000"
Output: ["0.0.0.0"]
Example 3:
Input: s = "101023"
Output: ["1.0.10.23","1.0.102.3","10.1.0.23","10.10.2.3","101.0.2.3"]
|
Google Doc Link
Combinations For Telephone Pad I
Given a telephone keypad, and an int number, print all words which are possible by pressing these numbers, the output strings should be sorted.
1
2
3
4
5
6
7
8
9
10
|
{0 : "", 1 : "", 2 : "abc", 3 : "def", 4 : "ghi", 5 : "jkl", 6 : "mno", 7 : "pqrs", 8 : "tuv", 9 : "wxyz"}
Assumptions:
The given number >= 0
Examples:
if input number is 231, possible words which can be formed are:
[ad, ae, af, bd, be, bf, cd, ce, cf]
|
Google Doc Link
Keep Distance For Identical Elements
Given an integer k, arrange the sequence of integers [1, 1, 2, 2, 3, 3, ...., k - 1, k - 1, k, k]
, such that the output integer array satisfy this condition:
Between each two i’s, they are exactly i integers (for example: between the two 1s, there is one number, between the two 2’s there are two numbers).
If there does not exist such sequence, return null.
1
2
3
4
5
6
|
Assumptions:
k is guaranteed to be > 0
Examples:
k = 3, The output = { 2, 3, 1, 2, 1, 3 }.
|
Google Doc Link
All Permutations of Subsets
Given a string with no duplicate characters, return a list with all permutations of the string and all its subsets.
1
2
3
4
5
6
7
|
Examples
Set = “abc”, all permutations are [“”, “a”, “ab”, “abc”, “ac”, “acb”, “b”, “ba”, “bac”, “bc”, “bca”, “c”, “cb”, “cba”, “ca”, “cab”].
Set = “”, all permutations are [“”].
Set = null, all permutations are [].
|
Google Doc Link
Factor Combinations
Given an integer number, return all possible combinations of the factors that can multiply to the target number.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
Example
Give A = 24
since 24 = 2 x 2 x 2 x 3
= 2 x 2 x 6
= 2 x 3 x 4
= 2 x 12
= 3 x 8
= 4 x 6
your solution should return
{ { 2, 2, 2, 3 }, { 2, 2, 6 }, { 2, 3, 4 }, { 2, 12 }, { 3, 8 }, { 4, 6 } }
note: duplicate combination is not allowed.
|
Google Doc Link
All Valid Permutations Of Parentheses III
Get all valid permutations of l pairs of (), m pairs of <> and n pairs of {}, subject to the priority restriction: {} higher than <> higher than ().
1
2
3
4
5
6
7
8
9
10
11
12
13
|
Assumptions
l, m, n >= 0
l + m + n >= 0
Examples
l = 1, m = 1, n = 0, all the valid permutations are ["()<>", "<()>", "<>()"].
l = 2, m = 0, n = 1, all the valid permutations are [“()(){}”, “(){()}”, “(){}()”, “{()()}”, “{()}()”, “{}()()”].
|
Google Doc Link
All Valid Permutations Of Parentheses II
Get all valid permutations of l pairs of (), m pairs of <> and n pairs of {}.
1
2
3
4
5
6
7
|
Assumptions
l, m, n >= 0
l + m + n > 0
Examples
l = 1, m = 1, n = 0, all the valid permutations are ["()<>", "(<>)", "<()>", "<>()"]
|
Google Doc Link
All Subsets II
Given a set of characters represented by a String, return a list containing all subsets of the characters. Notice that each subset returned will be sorted to remove the sequence.
1
2
3
4
5
6
7
8
9
10
|
Assumptions
There could be duplicate characters in the original set.
Examples
Set = "abc", all the subsets are ["", "a", "ab", "abc", "ac", "b", "bc", "c"]
Set = "abb", all the subsets are ["", "a", "ab", "abb", "b", "bb"]
Set = "abab", all the subsets are ["", "a", "aa","aab", "aabb", "ab","abb","b", "bb"]
Set = "", all the subsets are [""]
Set = null, all the subsets are []
|
Google Doc Link
All Subsets of Size K
Given a set of characters represented by a String, return a list containing all subsets of the characters whose size is K.
1
2
3
4
5
6
7
8
9
10
11
|
Assumptions
There are no duplicate characters in the original set.
Examples
Set = "abc", K = 2, all the subsets are [“ab”, “ac”, “bc”].
Set = "", K = 0, all the subsets are [""].
Set = "", K = 1, all the subsets are [].
|
Google Doc Link
All Subsets II of Size K
Given a set of characters represented by a String, return a list containing all subsets of the characters whose size is K. Notice that each subset returned will be sorted for deduplication.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
Assumptions
There could be duplicate characters in the original set.
Examples
Set = "abc", K = 2, all the subsets are [“ab”, “ac”, “bc”].
Set = "abb", K = 2, all the subsets are [“ab”, “bb”].
Set = "abab", K = 2, all the subsets are [“aa”, “ab”, “bb”].
Set = "", K = 0, all the subsets are [""].
Set = "", K = 1, all the subsets are [].
Set = null, K = 0, all the subsets are [].
|
Google Doc Link
All Subsets I
Given a set of characters represented by a String, return a list containing all subsets of the characters.
1
2
3
4
5
6
7
8
9
|
Assumptions
There are no duplicate characters in the original set.
Examples
Set = "abc", all the subsets are [“”, “a”, “ab”, “abc”, “ac”, “b”, “bc”, “c”]
Set = "", all the subsets are [""]
Set = null, all the subsets are []
|
Google Doc Link
All Valid Permutations Of Parentheses I
Given N pairs of parentheses “()”, return a list with all the valid permutations.
1
2
3
4
5
6
7
|
Assumptions
N > 0
Examples
N = 1, all valid permutations are ["()"]
N = 3, all valid permutations are ["((()))", "(()())", "(())()", "()(())", "()()()"]
|
Google Doc Link
Combinations Of Coins
Given a number of different denominations of coins (e.g., 1 cent, 5 cents, 10 cents, 25 cents), get all the possible ways to pay a target number of cents.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
Arguments
coins - an array of positive integers representing the different denominations of coins, there are no duplicate numbers and the numbers are sorted by descending order, eg. {25, 10, 5, 2, 1}
target - a non-negative integer representing the target number of cents, eg. 99
Assumptions
coins is not null and is not empty, all the numbers in coins are positive
target >= 0
You have infinite number of coins for each of the denominations, you can pick any number of the coins.
Return
a list of ways of combinations of coins to sum up to be target.
each way of combinations is represented by list of integer, the number at each index means the number of coins used for the denomination at corresponding index.
Examples
coins = {2, 1}, target = 4, the return should be
[
[0, 4], (4 cents can be conducted by 0 * 2 cents + 4 * 1 cents)
[1, 2], (4 cents can be conducted by 1 * 2 cents + 2 * 1 cents)
[2, 0] (4 cents can be conducted by 2 * 2 cents + 0 * 1 cents)
]
|
Google Doc Link
All Permutations I
Given a string with no duplicate characters, return a list with all permutations of the characters.
Assume that input string is not null.
1
2
3
4
5
|
Examples
Set = “abc”, all permutations are [“abc”, “acb”, “bac”, “bca”, “cab”, “cba”]
Set = "", all permutations are [""]
|
Google Doc Link
Recursion
Reconstruct Binary Search Tree With Levelorder and Inorder
Given the levelorder and inorder traversal sequence of a binary tree, reconstruct the original tree.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
Assumptions
The given sequences are not null and they have the same length
There are no duplicate keys in the binary tree
Examples
levelorder traversal = {5, 3, 8, 1, 4, 11}
inorder traversal = {1, 3, 4, 5, 8, 11}
the corresponding binary tree is
5
/ \
3 8
/ \ \
1 4 11
How is the binary tree represented?
We use level order traversal sequence with a special symbol "#" denoting the null node.
For Example:
The sequence [1, 2, 3, #, #, 4] represents the following binary tree:
1
/ \
2 3
/
4
|
Google Doc Link
Reconstruct Binary Search Tree With Postorder Traversal
Given the postorder traversal sequence of a binary search tree, reconstruct the original tree.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
Assumptions
The given sequence is not null
There are no duplicate keys in the binary search tree
Examples
postorder traversal = {1, 4, 3, 11, 8, 5}
the corresponding binary search tree is
5
/ \
3 8
/ \ \
1 4 11
How is the binary tree represented?
We use the pre order traversal sequence with a special symbol "#" denoting the null node.
For Example:
The sequence [1, 2, #, #, 3, 4, #, #, #] represents the following binary tree:
1
/ \
2 3
/
4
|
Google Doc Link
Reconstruct Binary Tree With Preorder And Inorder
Given the preorder and inorder traversal sequence of a binary tree, reconstruct the original tree.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
Assumptions
The given sequences are not null and they have the same length
There are no duplicate keys in the binary tree
Examples
preorder traversal = {5, 3, 1, 4, 8, 11}
inorder traversal = {1, 3, 4, 5, 8, 11}
the corresponding binary tree is
5
/ \
3 8
/ \ \
1 4 11
How is the binary tree represented?
We use the pre order traversal sequence with a special symbol "#" denoting the null node.
For Example:
The sequence [1, 2, #, 3, 4, #, #, #] represents the following binary tree:
1
/ \
2 3
/
4
|
Google Doc Link
Flatten Binary Tree to Linked List
Given a binary tree, flatten it to a linked list in-place.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
For example,
Given
1
/ \
2 5
/ \ \
3 4 6
The flattened tree should look like:
1
\
2
\
3
\
4
\
5
\
6
|
Google Doc Link
Maximum Path Sum Binary Tree III
Given a binary tree in which each node contains an integer number. Find the maximum possible subpath sum(both the starting and ending node of the subpath should be on the same path from root to one of the leaf nodes, and the subpath is allowed to contain only one node).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
Assumptions
The root of given binary tree is not null
Examples
-5
/ \
2 11
/ \
6 14
/
-3
The maximum path sum is 11 + 14 = 25
How is the binary tree represented?
We use the level order traversal sequence with a special symbol "#" denoting the null node.
For Example:
The sequence [1, 2, 3, #, #, 4] represents the following binary tree:
1
/ \
2 3
/
4
|
Google Doc Link
Max Path Sum From Leaf To Root
Given a binary tree in which each node contains an integer number. Find the maximum possible path sum from a leaf to root.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
Assumptions
The root of given binary tree is not null.
Examples
10
/ \
-2 7
/ \
8 -4
The maximum path sum is 10 + 7 = 17.
|
Google Doc Link
Maximum Path Sum Binary Tree I
Given a binary tree in which each node contains an integer number. Find the maximum possible sum from one leaf node to another leaf node. If there is no such path available, return Integer.MIN_VALUE(Java)/INT_MIN (C++).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
Examples
-15
/ \
2 11
/ \
6 14
The maximum path sum is 6 + 11 + 14 = 31.
How is the binary tree represented?
We use the level order traversal sequence with a special symbol "#" denoting the null node.
For Example:
The sequence [1, 2, 3, #, #, 4] represents the following binary tree:
1
/ \
2 3
/
4
|
Google Doc Link
Maximum Path Sum Binary Tree II
Given a binary tree in which each node contains an integer number. Find the maximum possible sum from any node to any node (the start node and the end node can be the same).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
Assumptions
The root of the given binary tree is not null
Examples
-1
/ \
2 11
/ \
6 -14
one example of paths could be -14 -> 11 -> -1 -> 2
another example could be the node 11 itself
The maximum path sum in the above binary tree is 6 + 11 + (-1) + 2 = 18
How is the binary tree represented?
We use the level order traversal sequence with a special symbol "#" denoting the null node.
For Example:
The sequence [1, 2, 3, #, #, 4] represents the following binary tree:
1
/ \
2 3
/
4
|
Google Doc Link
Spiral Order Traverse I
Traverse an N * N 2D array in spiral order clock-wise starting from the top left corner. Return the list of traversal sequence.
1
2
3
4
5
6
7
8
9
10
11
12
|
Assumptions
The 2D array is not null and has size of N * N where N >= 0
Examples
{ {1, 2, 3},
{4, 5, 6},
{7, 8, 9} }
the traversal sequence is [1, 2, 3, 6, 9, 8, 7, 4, 5]
|
Google Doc Link
N Queens
Get all valid ways of putting N Queens on an N * N chessboard so that no two Queens threaten each other.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
Assumptions
N > 0
Return
A list of ways of putting the N Queens
Each way is represented by a list of the Queen's y index for x indices of 0 to (N - 1)
Example
N = 4, there are two ways of putting 4 queens:
[1, 3, 0, 2] --> the Queen on the first row is at y index 1, the Queen on the second row is at y index 3, the Queen on the third row is at y index 0 and the Queen on the fourth row is at y index 2.
[2, 0, 3, 1] --> the Queen on the first row is at y index 2, the Queen on the second row is at y index 0, the Queen on the third row is at y index 3 and the Queen on the fourth row is at y index 1.
|
Google Doc Link
Reverse Linked List In Pairs
Reverse pairs of elements in a singly-linked list.
1
2
3
4
5
6
|
Examples
L = null, after reverse is null
L = 1 -> null, after reverse is 1 -> null
L = 1 -> 2 -> null, after reverse is 2 -> 1 -> null
L = 1 -> 2 -> 3 -> null, after reverse is 2 -> 1 -> 3 -> null
|
Google Doc Link
String Abbreviation Matching
Word “book” can be abbreviated to 4, b3, b2k, etc. Given a string and an abbreviation, return if the string matches the abbreviation.
1
2
3
4
5
6
7
8
|
Assumptions:
The original string only contains alphabetic characters.
Both input and pattern are not null.
Pattern would not contain invalid information like "a0a","0".
Examples:
pattern “s11d” matches input “sophisticated” since “11” matches eleven chars “ophisticate”.
|
Google Doc Link
Store Number Of Nodes In Left Subtree
Given a binary tree, count the number of nodes in each node’s left subtree, and store it in the numNodesLeft field.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
Examples
1(6)
/ \
2(3) 3(0)
/ \
4(1) 5(0)
/ \ \
6(0) 7(0) 8(0)
The numNodesLeft is shown in parentheses.
|
Google Doc Link
Lowest Common Ancestor I
Given two nodes in a binary tree, find their lowest common ancestor.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
Assumptions
There is no parent pointer for the nodes in the binary tree
The given two nodes are guaranteed to be in the binary tree
Examples
5
/ \
9 12
/ \ \
2 3 14
The lowest common ancestor of 2 and 14 is 5
The lowest common ancestor of 2 and 9 is 9
|
Google Doc Link
Reverse Binary Tree Upside Down
Given a binary tree where all the right nodes are leaf nodes, flip it upside down and turn it into a tree with left leaf nodes as the root.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
Examples
1
/ \
2 5
/ \
3 4
is reversed to
3
/ \
2 4
/ \
1 5
|
Google Doc Link
Spiral Order Traverse II
Traverse an M * N 2D array in spiral order clock-wise starting from the top left corner. Return the list of traversal sequence.
1
2
3
4
5
6
7
8
9
10
11
12
|
Assumptions
The 2D array is not null and has size of M * N where M, N >= 0
Examples
{ {1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12} }
the traversal sequence is [1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7]
|
Google Doc Link
Heap & BFS
K Smallest In Unsorted Array
Find the K smallest numbers in an unsorted integer array A. The returned numbers should be in ascending order.
1
2
3
4
5
6
7
8
9
10
|
Assumptions
A is not null
K is >= 0 and smaller than or equal to size of A
Return
an array with size K containing the K smallest numbers in ascending order
Examples
A = {3, 4, 1, 2, 5}, K = 3, the 3 smallest numbers are {1, 2, 3}
|
Google Doc Link
Get Keys In Binary Tree Layer By Layer
Get the list of list of keys in a given binary tree layer by layer. Each layer is represented by a list of keys and the keys are traversed from left to right.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
Examples
5
/ \
3 8
/ \ \
1 4 11
the result is [ [5], [3, 8], [1, 4, 11] ]
Corner Cases
What if the binary tree is null? Return an empty list of list in this case.
How is the binary tree represented?
We use the level order traversal sequence with a special symbol "#" denoting the null node.
For Example:
The sequence [1, 2, 3, #, #, 4] represents the following binary tree:
1
/ \
2 3
/
4
|
Google Doc Link
Bipartite
Determine if an undirected graph is bipartite. A bipartite graph is one in which the nodes can be divided into two groups such that no nodes have direct edges to other nodes in the same group.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
Examples
1 -- 2
/
3 -- 4
is bipartite (1, 3 in group 1 and 2, 4 in group 2).
1 -- 2
/ |
3 -- 4
is not bipartite.
Assumptions
The graph is represented by a list of nodes, and the list of nodes is not null.
|
Google Doc Link
Check If Binary Tree Is Completed
Check if a given binary tree is completed. A complete binary tree is one in which every level of the binary tree is completely filled except possibly the last level. Furthermore, all nodes are as far left as possible.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
Examples
5
/ \
3 8
/ \
1 4
is completed.
5
/ \
3 8
/ \ \
1 4 11
is not completed.
Corner Cases
What if the binary tree is null? Return true in this case.
How is the binary tree represented?
We use the level order traversal sequence with a special symbol "#" denoting the null node.
For Example:
The sequence [1, 2, 3, #, #, 4] represents the following binary tree:
1
/ \
2 3
/
4
|
Google Doc Link
Kth Smallest Number In Sorted Matrix
Given a matrix of size N x M. For each row the elements are sorted in ascending order, and for each column the elements are also sorted in ascending order. Find the Kth smallest number in it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
Assumptions
the matrix is not null, N > 0 and M > 0
K > 0 and K <= N * M
Examples
{ {1, 3, 5, 7},
{2, 4, 8, 9},
{3, 5, 11, 15},
{6, 8, 13, 18} }
the 5th smallest number is 4
the 8th smallest number is 6
|
Google Doc Link
Seven Puzzle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
Given eight cards with number 0, 1, 2, ..7 on them, the cards are placed in two rows with 4 cards in each row. In each step only card 0 could swap with one of its adjacent(top, right, bottom, left) card. Your goal is to make all cards placed in order like this:
0 1 2 3
4 5 6 7
Find the minimum number of steps from the given state to the final state. If there is no way to the final state, then return -1.
The state of cards is represented by an array of integer, for example [0,1,2,3,4,5,6,7] where the first four numbers are in the first row from left to right while the others are placed in the second row from left to right.
Example:
Input: [4,1,2,3,5,0,6,7] Output: 2
Explanation:
Initial state is:
4 1 2 3
5 0 6 7
First swap 0 with 5, then the state is:
4 1 2 3
0 5 6 7
Then swap 0 with 4, then we get the final state:
0 1 2 3
4 5 6 7
|
Google Doc Link
Word Ladder II
Given a begin word, an end word and a dictionary, find the least number transformations from begin word to end word, and return the transformation sequences. Return empty list if there is no such transformations.
In each transformation, you can only change one letter, and the word should still in the dictionary after each transformation.
1
2
3
4
5
6
7
8
9
10
11
12
13
|
Assumptions
1. All words have the same length.
2. All words contain only lowercase alphabetic characters.
3. There is no duplicates in the word list.
4.The beginWord and endWord are non-empty and are not the same.
Example: start = "git", end = "hot", dictionary = {"git","hit","hog","hot","got"}
Output: [["git","hit","hot"], ["git","got","hot"]]
|
Google Doc Link
Word Ladder
Given a begin word, an end word and a dictionary, find the least number transformations from begin word to end word, and return the length of the transformation sequence. Return 0 if there is no such transformations.
In each transformation, you can only change one letter, and the word should still in the dictionary after each transformation.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
Assumptions
1. All words have the same length.
2. All words contain only lowercase alphabetic characters.
3. There is no duplicates in the word list.
4.The beginWord and endWord are non-empty and are not the same.
Example: start = "git", end = "hot", dictionary = {"git","hit","hog","hot"}
Output: 3
Explanation: git -> hit -> hot
|
Google Doc Link
Largest Product Of Length
Given a dictionary containing many words, find the largest product of two words’ lengths, such that the two words do not share any common characters.
1
2
3
4
5
6
7
8
|
Assumptions
The words only contains characters of 'a' to 'z'
The dictionary is not null and does not contains null string, and has at least two strings
If there is no such pair of words, just return 0
Examples
dictionary = [“abcde”, “abcd”, “ade”, “xy”], the largest product is 5 * 2 = 10 (by choosing “abcde” and “xy”)
|
Google Doc Link
Kth Smallest With Only 3, 5, 7 As Factors
Find the Kth smallest number s such that s = 3 ^ x * 5 ^ y * 7 ^ z, x > 0 and y > 0 and z > 0, x, y, z are all integers.
1
2
3
4
5
6
7
8
9
|
Assumptions
K >= 1
Examples
the smallest is 3 * 5 * 7 = 105
the 2nd smallest is 3 ^ 2 * 5 * 7 = 315
the 3rd smallest is 3 * 5 ^ 2 * 7 = 525
the 5th smallest is 3 ^ 3 * 5 * 7 = 945
|
Google Doc Link
Kth Closest Point To <0,0,0>
Given three arrays sorted in ascending order. Pull one number from each array to form a coordinate <x,y,z> in a 3D space. Find the coordinates of the points that is k-th closest to <0,0,0>.
We are using euclidean distance here.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
Assumptions
The three given arrays are not null or empty, containing only non-negative numbers
K >= 1 and K <= a.length * b.length * c.length
Return
a size 3 integer list, the first element should be from the first array, the second element should be from the second array and the third should be from the third array
Examples
A = {1, 3, 5}, B = {2, 4}, C = {3, 6}
The closest is <1, 2, 3>, distance is sqrt(1 + 4 + 9)
The 2nd closest is <3, 2, 3>, distance is sqrt(9 + 4 + 9)
|
Google Doc Link
Course Schedule II
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
There are a total of n courses you have to take, labeled from 0 to n - 1.
Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1]
Given the total number of courses and a list of prerequisite pairs, return the ordering of courses you should take to finish all courses.
There may be multiple correct orders, you just need to return one of them. If it is impossible to finish all courses, return an empty array.
For example:
2, [[1,0]]
There are a total of 2 courses to take. To take course 1 you should have finished course 0. So the correct course order is [0,1]
4, [[1,0],[2,0],[3,1],[3,2]]
There are a total of 4 courses to take. To take course 3 you should have finished both courses 1 and 2. Both courses 1 and 2 should be taken after you finished course 0. So one correct course order is [0,1,2,3]. Another correct ordering is[0,2,1,3].
Note:
The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.
You may assume that there are no duplicate edges in the input prerequisites.
|
Google Doc Link