Skip to content

Commit

Permalink
Add interview questions
Browse files Browse the repository at this point in the history
  • Loading branch information
yangshun committed Nov 20, 2017
1 parent b6e8283 commit 9d74d2b
Show file tree
Hide file tree
Showing 10 changed files with 286 additions and 1 deletion.
2 changes: 1 addition & 1 deletion algorithms/array.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]`

```
Expand Down
37 changes: 37 additions & 0 deletions questions/01-histogram-stars.md
Original file line number Diff line number Diff line change
@@ -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.
*
** *
** **
** **
** ***
******
'''
```
56 changes: 56 additions & 0 deletions questions/02-matrix-islands.md
Original file line number Diff line number Diff line change
@@ -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
```
40 changes: 40 additions & 0 deletions questions/03-k-smallest-two-sorted.md
Original file line number Diff line number Diff line change
@@ -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]
```
36 changes: 36 additions & 0 deletions questions/04-synonyms.md
Original file line number Diff line number Diff line change
@@ -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
'''
```
33 changes: 33 additions & 0 deletions questions/05-longest-consecutive-set-of-numbers.md
Original file line number Diff line number Diff line change
@@ -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
```
25 changes: 25 additions & 0 deletions questions/06-string-compression.md
Original file line number Diff line number Diff line change
@@ -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'
'''
```
21 changes: 21 additions & 0 deletions questions/07-biggest-number-after-swap.md
Original file line number Diff line number Diff line change
@@ -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?
19 changes: 19 additions & 0 deletions questions/08-pivot-index.md
Original file line number Diff line number Diff line change
@@ -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
'''
```
18 changes: 18 additions & 0 deletions questions/09-move-zeroes.md
Original file line number Diff line number Diff line change
@@ -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].
'''
```

0 comments on commit 9d74d2b

Please sign in to comment.