mirror of
https://github.com/yangshun/tech-interview-handbook.git
synced 2025-07-04 15:28:12 +08:00
Add interview questions
This commit is contained in:
@ -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]`
|
||||
|
||||
```
|
||||
|
37
questions/01-histogram-stars.md
Normal file
37
questions/01-histogram-stars.md
Normal file
@ -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
questions/02-matrix-islands.md
Normal file
56
questions/02-matrix-islands.md
Normal file
@ -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
questions/03-k-smallest-two-sorted.md
Normal file
40
questions/03-k-smallest-two-sorted.md
Normal file
@ -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
questions/04-synonyms.md
Normal file
36
questions/04-synonyms.md
Normal file
@ -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
questions/05-longest-consecutive-set-of-numbers.md
Normal file
33
questions/05-longest-consecutive-set-of-numbers.md
Normal file
@ -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
questions/06-string-compression.md
Normal file
25
questions/06-string-compression.md
Normal file
@ -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
questions/07-biggest-number-after-swap.md
Normal file
21
questions/07-biggest-number-after-swap.md
Normal file
@ -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
questions/08-pivot-index.md
Normal file
19
questions/08-pivot-index.md
Normal file
@ -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
questions/09-move-zeroes.md
Normal file
18
questions/09-move-zeroes.md
Normal file
@ -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].
|
||||
'''
|
||||
```
|
Reference in New Issue
Block a user