From 9d74d2b83690bec5ab76329bc7a4772e23e3f481 Mon Sep 17 00:00:00 2001 From: Tay Yang Shun Date: Sun, 19 Nov 2017 23:20:01 -0800 Subject: [PATCH] Add interview questions --- algorithms/array.md | 2 +- questions/01-histogram-stars.md | 37 ++++++++++++ questions/02-matrix-islands.md | 56 +++++++++++++++++++ questions/03-k-smallest-two-sorted.md | 40 +++++++++++++ questions/04-synonyms.md | 36 ++++++++++++ .../05-longest-consecutive-set-of-numbers.md | 33 +++++++++++ questions/06-string-compression.md | 25 +++++++++ questions/07-biggest-number-after-swap.md | 21 +++++++ questions/08-pivot-index.md | 19 +++++++ questions/09-move-zeroes.md | 18 ++++++ 10 files changed, 286 insertions(+), 1 deletion(-) create mode 100644 questions/01-histogram-stars.md create mode 100644 questions/02-matrix-islands.md create mode 100644 questions/03-k-smallest-two-sorted.md create mode 100644 questions/04-synonyms.md create mode 100644 questions/05-longest-consecutive-set-of-numbers.md create mode 100644 questions/06-string-compression.md create mode 100644 questions/07-biggest-number-after-swap.md create mode 100644 questions/08-pivot-index.md create mode 100644 questions/09-move-zeroes.md diff --git a/algorithms/array.md b/algorithms/array.md index 4027cfaa..0de9fedc 100644 --- a/algorithms/array.md +++ b/algorithms/array.md @@ -7,7 +7,7 @@ Arrays - Paginate an array with constraints, such as skipping certain items. - Implement a circular buffer using an array. - Given array of arrays, sort them in ascending order. -- Given an array of integers, print out a histogram of using said array; include a base layer (all stars) +- Given an array of integers, print out a histogram using the said array; include a base layer (all stars) - E.g. `[5, 4, 0, 3, 4, 1]` ``` diff --git a/questions/01-histogram-stars.md b/questions/01-histogram-stars.md new file mode 100644 index 00000000..766a2feb --- /dev/null +++ b/questions/01-histogram-stars.md @@ -0,0 +1,37 @@ +--- +id: 1 +title: Histogram Stars +topics: [array] +difficulty: easy +--- + +## Question + +``` +''' +Given an array of integers, print out a histogram using the array. Include a base layer (all stars) + +[5, 4, 0, 3, 4, 1] will result in the following: + +****** +***** +* +**** +***** +** +''' +``` + +## Follow Up + +``` +Print out a vertical version of the histogram. + +* +** * +** ** +** ** +** *** +****** +''' +``` diff --git a/questions/02-matrix-islands.md b/questions/02-matrix-islands.md new file mode 100644 index 00000000..26b3ed06 --- /dev/null +++ b/questions/02-matrix-islands.md @@ -0,0 +1,56 @@ +--- +id: 2 +title: Matrix Islands +topics: [array, depth-first-search, matrix] +difficulty: easy +source: + - https://leetcode.com/problems/number-of-islands/ + - https://leetcode.com/problems/max-area-of-island/ + - https://leetcode.com/problems/number-of-distinct-islands/ +--- + +## Question + +``` +''' +Given a matrix of 0s and 1s, count the number of islands present. + +[[0,0,1], + [0,0,0], + [0,1,1]] + +Answer: 2 +''' +``` + +## Follow Up + +``` +Given a matrix of 0s and 1s, find the size of the largest island present. + +''' +[[0,0,1], + [0,0,0], + [0,1,1]] + +Answer: 2 +``` + +## Follow Up II + +``` +Given a matrix of 0s and 1s, find the number of unique islands present present. + +''' +[[1,0,1,0], + [1,0,0,0], + [0,1,1,0]] + +Answer: 3 + +[[1,1,0,1], + [0,0,0,0], + [0,1,1,0]] + +Answer: 2 +``` diff --git a/questions/03-k-smallest-two-sorted.md b/questions/03-k-smallest-two-sorted.md new file mode 100644 index 00000000..1eaf3037 --- /dev/null +++ b/questions/03-k-smallest-two-sorted.md @@ -0,0 +1,40 @@ +--- +id: 3 +title: K Smallest Elements from Two Sorted Arrays +topics: [array] +difficulty: easy +--- + +## Question + +``` +''' +Find the k smallest elements from two sorted arrays of integers. + +Input: [1, 2, 3, 4, 5], [2, 3, 4], k = 3 +Output: [1, 2, 2] +''' +``` + +## Time Complexities + +- Bad: + - Time: O((n + m)log(n + m)) +- Good: + - Time: O(k) + +## Sample Answers + +```py +def k_smallest(A, B, k): + res = [] + a = b = 0 + while a < len(A) and b < len(B) and (a + b) < k: + if A[a] < B[b]: + res.append(A[a]) + a += 1 + else: + res.append(B[b]) + b += 1 + return res + A[a:k - b] + B[b:k - a] +``` diff --git a/questions/04-synonyms.md b/questions/04-synonyms.md new file mode 100644 index 00000000..b3615d76 --- /dev/null +++ b/questions/04-synonyms.md @@ -0,0 +1,36 @@ +--- +id: 4 +title: Synonyms +topics: [graph] +difficulty: medium +--- + +## Question + +``` +''' +Given a list of synonym pairs, determine if two words are synonymous. +Synonyms have a symmetric and transitive relation. i.e. if a <-> b and b <-> c, a <-> c. + +Input: +[["computer", "laptop"]] +"computer" +"laptop" + +Output: true + +Input: +[["computer", "laptop"], ["laptop", "pc"]] +"computer" +"pc" + +Output: true + +Input: +[["computer", "laptop"], ["laptop", "pc"], ["tablet", "iPad"]] +"computer" +"iPad" + +Output: false +''' +``` diff --git a/questions/05-longest-consecutive-set-of-numbers.md b/questions/05-longest-consecutive-set-of-numbers.md new file mode 100644 index 00000000..2231f8b3 --- /dev/null +++ b/questions/05-longest-consecutive-set-of-numbers.md @@ -0,0 +1,33 @@ +--- +id: 5 +title: Longest Consecutive Set of Numbers +topics: [graph] +difficulty: hard +source: https://leetcode.com/problems/longest-consecutive-sequence/ +--- + +## Question + +``` +''' +Given an array of integers, find the size of the largest set of consecutive numbers present in the array. + +Input: [100, 4, 200, 1, 3, 2] +Output: 4 because {1, 2, 3, 4} +''' +``` + +## Sample Answers + +```py +def longest_consecutive(nums): + nums = set(nums) + best = 0 + for x in nums: + if x - 1 not in nums: + y = x + 1 + while y in nums: + y += 1 + best = max(best, y - x) + return best +``` diff --git a/questions/06-string-compression.md b/questions/06-string-compression.md new file mode 100644 index 00000000..60b6a227 --- /dev/null +++ b/questions/06-string-compression.md @@ -0,0 +1,25 @@ +--- +id: 6 +title: String Compression and Decompression +topics: [string] +difficulty: easy +--- + +## Question + +``` +''' +Given a string, compress it by grouping repeated characters. The length after +compression must always be smaller than or equal to the original string. + +'aaabbccc' => 'a3b2c3' +''' +``` + +``` +''' +Given the above compressed string, decompress to obtain the original string. + +'a3b2c3' => 'aaabbccc' +''' +``` diff --git a/questions/07-biggest-number-after-swap.md b/questions/07-biggest-number-after-swap.md new file mode 100644 index 00000000..2f43f22f --- /dev/null +++ b/questions/07-biggest-number-after-swap.md @@ -0,0 +1,21 @@ +--- +id: 7 +title: Biggest Number After Swap +topics: [math] +difficulty: medium +source: https://leetcode.com/problems/maximum-swap/ +--- + +## Question + +``` +''' +Given a non-negative integer, find the maximum possible number if you can swap two digits at most once. + +2736 => 7236 +''' +``` + +## Follow Up + +- What if the given integer can be negative? diff --git a/questions/08-pivot-index.md b/questions/08-pivot-index.md new file mode 100644 index 00000000..81e978c9 --- /dev/null +++ b/questions/08-pivot-index.md @@ -0,0 +1,19 @@ +--- +id: 8 +title: Pivot Index +topics: [array] +difficulty: easy +source: https://leetcode.com/problems/find-pivot-index/ +--- + +## Question + +``` +''' +Given an array of integers, return the pivot index of this array. + +A pivot index is the index where the sum of the numbers to its left is equal to the sum of the numbers to its right: + +[7, 2, 3, 4] => 1 +''' +``` diff --git a/questions/09-move-zeroes.md b/questions/09-move-zeroes.md new file mode 100644 index 00000000..e8f9556f --- /dev/null +++ b/questions/09-move-zeroes.md @@ -0,0 +1,18 @@ +--- +id: 9 +title: Move Zeroes +topics: [array] +difficulty: easy +source: https://leetcode.com/problems/move-zeroes/ +--- + +## Question + +``` +''' +Given an array of integers, write a function to move all zeroes to the end of it while +maintaining the relative order of the non-zero elements. + +[0, 1, 0, 3, 12] => [1, 3, 12, 0, 0]. +''' +```