Skip to content

Commit 36ce433

Browse files
More algorithm
1 parent 3c91128 commit 36ce433

File tree

3 files changed

+266
-19
lines changed

3 files changed

+266
-19
lines changed

Interview/Company/facebook/algorithm_prepare.md

Lines changed: 197 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,25 @@
77
4. [Best time to buy and sell stocks II] Medium
88
5. [Best Time to Buy and Sell Stock III] Medium
99
6. [Best time to buy and sell stock with transcation fee](#Best-time-to-buy-and-sell-stock-with-transcation-fee) Medium
10-
7. [Diagnose Tranverse](#Diagnose-Tranverse) Medium
11-
8. [Insert Delete GetRandom O(1)](#Insert-Delete-GetRandom-O(1)) Medium
12-
9. [Product of Array Except Self](#product-of-array-except-self) Medium
13-
10. [Kth Smallest Element in a Sorted Matrix](#Kth-Smallest-Element-in-a-Sorted-Matrix) Medium
14-
11. [Rotate Image](#Rotate-Image) Medium
15-
12. [Maximum Swap](#Maximum-Swap) Medium
16-
13. [Intersection of Two Arrays](#Intersection-of-Two-Arrays) Easy
17-
14. [Intersection of Two Arrays II](#Intersection-of-Two-Arrays-II) Easy
18-
15. [Sparse Matrix Multiplication](#Sparse-Matrix-Multiplication) Medium
19-
16. [Rectangle Overlap](#Rectangle-Overlap) Easy
20-
17. [Minimum Window Substring](#Minimum-Window-Substring) Hard
21-
18. [Valid Number](#Valid-Number) Hard
22-
19. [Candy](#Candy) Hard
23-
20. [Subarray Sum Equals K](#Subarray-Sum-Equals-K) Medium
10+
7. [Insert Delete GetRandom O(1)](#Insert-Delete-GetRandom-O(1)) Medium
11+
8. [Product of Array Except Self](#product-of-array-except-self) Medium
12+
9. [Maximum Swap](#Maximum-Swap) Medium
13+
10. [Intersection of Two Arrays](#Intersection-of-Two-Arrays) Easy
14+
11. [Intersection of Two Arrays II](#Intersection-of-Two-Arrays-II) Easy
15+
12. [Sparse Matrix Multiplication](#Sparse-Matrix-Multiplication) Medium
16+
13. [Rectangle Overlap](#Rectangle-Overlap) Easy
17+
14. [Minimum Window Substring](#Minimum-Window-Substring) Hard
18+
15. [Valid Number](#Valid-Number) Hard
19+
16. [Candy](#Candy) Hard
20+
17. [Subarray Sum Equals K](#Subarray-Sum-Equals-K) Medium
21+
18. [Find the Kth largest item](#Find-the-Kth-largest-item) Medium
22+
19. [Count Duplicates](#Count-Duplicates) Medium
23+
20. [3 Sum](#3-Ssum)
24+
25+
## Matrix
26+
1. [Diagnose Tranverse](#Diagnose-Tranverse) Medium
27+
2. [Kth Smallest Element in a Sorted Matrix](#Kth-Smallest-Element-in-a-Sorted-Matrix) Medium
28+
3. [Rotate Image](#Rotate-Image) Medium
2429

2530
## Binary Search
2631
1. [Search a 2D Matrix](#Search-a-2D-Matrix) Easy
@@ -43,6 +48,8 @@
4348
10. [Rotate List](#Rotate-List) Medium
4449
11. [Plus One Linked List](#Plus-One-Linked-List) Medium
4550
12. [Intersection of Two Linked Lists](#Intersection-of-Two-Linked-Lists) Medium
51+
13. [Remove Duplicates from Sorted List II](#Remove-Duplicates-from-Sorted-List-II) Medium
52+
14. [Remove Nth Node From End of List](#Remove-Nth node-from-the-end-of-list) Medium
4653

4754
## String
4855
1. [Valid Anagram](#Valid-Anagram) Easy
@@ -2855,4 +2862,180 @@ string minWindow(string s, string t) {
28552862
}
28562863
return result;
28572864
}
2865+
```
2866+
2867+
### Find the Kth largest item
2868+
***Big O:*** O(n) speed, O(1) space
2869+
```
2870+
Tips:
2871+
2872+
Quick Select
2873+
```
2874+
```c++
2875+
class Solution {
2876+
public:
2877+
int findKthLargest(vector<int>& nums, int k) {
2878+
//partition rule: >=pivot pivot <=pivot
2879+
int left=0,right=nums.size()-1,idx=0;
2880+
while(1){
2881+
idx = partition(nums,left,right);
2882+
if(idx==k-1) break;
2883+
else if(idx < k-1) left=idx+1;
2884+
else right= idx-1;
2885+
}
2886+
return nums[idx];
2887+
}
2888+
int partition(vector<int>& nums,int left,int right){//hoare partition
2889+
int pivot = nums[left], l=left+1, r = right;
2890+
while(l<=r){
2891+
if(nums[l]<pivot && nums[r]>pivot) swap(nums[l++],nums[r--]);
2892+
if(nums[l]>=pivot) ++l;
2893+
if(nums[r]<=pivot) --r;
2894+
}
2895+
swap(nums[left], nums[r]);
2896+
return r;
2897+
}
2898+
};
2899+
```
2900+
2901+
### Count Duplicates
2902+
***Big O:*** O(n) speed, O(n) space
2903+
```
2904+
Tips:
2905+
2906+
Hashmap
2907+
```
2908+
```c++
2909+
class Solution {
2910+
public:
2911+
vector<int> countduplicates(vector<int> &nums) {
2912+
// write your code here
2913+
unordered_map<int, bool> dup;
2914+
vector<int> ans;
2915+
2916+
for (auto n : nums) {
2917+
if ((dup.find(n) != dup.end()) && !dup[n]) {
2918+
ans.push_back(n);
2919+
dup[n] = true;
2920+
}
2921+
else if (dup.find(n) == dup.end())
2922+
dup[n] = false;
2923+
}
2924+
2925+
return ans;
2926+
}
2927+
};
2928+
```
2929+
2930+
### 3 Sum
2931+
***Big O:*** O(n^2) speed, O(1) space
2932+
```
2933+
Tips:
2934+
2935+
Sort + 2 Sum II (two pointer).
2936+
2937+
Be careful with duplicate items.
2938+
```
2939+
```c++
2940+
class Solution {
2941+
public:
2942+
vector<vector<int>> threeSum(vector<int>& nums) {
2943+
sort(begin(nums), end(nums));
2944+
vector<vector<int>> res;
2945+
for (int i = 0; i < nums.size() && nums[i] <= 0; ++i)
2946+
if (i == 0 || nums[i - 1] != nums[i]) {
2947+
twoSumII(nums, i, res);
2948+
}
2949+
return res;
2950+
}
2951+
void twoSumII(vector<int>& nums, int i, vector<vector<int>> &res) {
2952+
int lo = i + 1, hi = nums.size() - 1;
2953+
while (lo < hi) {
2954+
int sum = nums[i] + nums[lo] + nums[hi];
2955+
if (sum < 0) {
2956+
++lo;
2957+
} else if (sum > 0) {
2958+
--hi;
2959+
} else {
2960+
res.push_back({ nums[i], nums[lo++], nums[hi--] });
2961+
while (lo < hi && nums[lo] == nums[lo - 1])
2962+
++lo;
2963+
}
2964+
}
2965+
}
2966+
};
2967+
```
2968+
2969+
### Remove Duplicates from Sorted List II
2970+
***Big O:*** O(n) speed, O(1) space
2971+
```
2972+
Tips:
2973+
2974+
Two pointers pre and cur.
2975+
```
2976+
```c++
2977+
class Solution {
2978+
public:
2979+
ListNode* deleteDuplicates(ListNode* head) {
2980+
ListNode sentinal, *pre, *cur, *nxt;
2981+
2982+
sentinal.next = head;
2983+
sentinal.val = INT_MIN;
2984+
pre = &sentinal;
2985+
cur = pre;
2986+
2987+
while (cur && cur->next) {
2988+
while (cur->next && cur->val == cur->next->val) {
2989+
cur = cur->next;
2990+
}
2991+
cur = cur->next;
2992+
pre->next = cur;
2993+
2994+
if (cur && cur->next && cur->val != cur->next->val)
2995+
pre = pre->next;
2996+
}
2997+
2998+
return sentinal.next;
2999+
}
3000+
};
3001+
```
3002+
3003+
### Remove Nth node from the end of list
3004+
***Big O:*** O(L) speed, O(1) space
3005+
```
3006+
Tips:
3007+
3008+
One pass algorithm:
3009+
3010+
The first pointer advances the list by n+1n+1 steps from the beginning, while the second pointer starts from the beginning of the list. Now, both pointers are exactly separated by nn nodes apart. We maintain this constant gap by advancing both pointers together until the first pointer arrives past the last node. The second pointer will be pointing at the nnth node counting from the last. We relink the next pointer of the node referenced by the second pointer to point to the node's next next node.
3011+
```
3012+
```c++
3013+
class Solution {
3014+
public:
3015+
ListNode* removeNthFromEnd(ListNode* head, int n) {
3016+
if (head == nullptr || (head->next == nullptr && n > 0))
3017+
return nullptr;
3018+
3019+
ListNode sentinal, *first, *second;
3020+
3021+
sentinal.next = head;
3022+
first = &sentinal;
3023+
second = &sentinal;
3024+
3025+
while(n--) {
3026+
if (second == nullptr)
3027+
return nullptr;
3028+
second = second->next;
3029+
}
3030+
3031+
while(second->next) {
3032+
first = first->next;
3033+
second = second->next;
3034+
}
3035+
3036+
first->next = first->next->next;
3037+
3038+
return sentinal.next;
3039+
}
3040+
};
28583041
```
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
I am not sure about above but I'd probably work from top to down and try to organize as much info as I can however time is limit in basically refining a design.
2+
3+
## Players
4+
5+
1. airplane in the sky
6+
2. air traffic control unit on the ground
7+
8+
### Airplane -
9+
It has many components, but important one here are two
10+
11+
- pilot - one who is displacing the plane or changing its position
12+
- air traffic control unit - the one that communicates with air traffic control system on the ground
13+
14+
***Pilot -***
15+
It has couple of actions
16+
17+
- register - sends initial message to new air traffic control on the ground
18+
19+
- deregister - send final message to old air traffic control on the ground
20+
21+
- steer - move plane (but not relevant to this i suppose)
22+
23+
***Air Traffic Control Communication -***
24+
- updates its location - this happens either in loop continuously
25+
- send message to air traffic control unit on ground this happens again periodically in loop
26+
- receive messages from air traffic control unit and send those back to pilot - happens periodically in loop (messages are either simple pings or some message)
27+
28+
### On the ground -
29+
30+
***Controller***
31+
32+
- subscribes for registration / deregistration / urgent messages on global queue
33+
- receive registration, send registration ack
34+
- receive de-registration, send re-registration ack
35+
- In response to registration spawn plane specific thread handling object
36+
( one can discuss thread pool, database/data structure to maintain location and other info of plane)
37+
38+
***Worker:***
39+
40+
- subscribes to messages on plane's queue
41+
- subscribes to messages on a global queue
42+
- update location database in response to periodic location update
43+
- notifies registration message and exit
44+
45+
***Communication:***
46+
- A backend of radio. Implements message passing
47+
- Receives and collects all messages and distributes them
48+
- Messages are either registration, deregistration, urgent and location update
49+
- Messages are put in different queues , each worker thread create its own queue when it registers for messages and subscribes for it.
50+
51+
***Control Unit on Plane***
52+
- subscribes for urgent messages from controller
53+
- periodically sends location update
54+
- Urgent messages directed to Pilot class
55+
- unsubscribe from controller
56+
57+
***Pilot***
58+
59+
- Sends registration, in response to ack, trigger Control Unit on Plane subscribe to new Controller
60+
- Sends deregistration, in response to ack, trigger Control Unit on Plane unsubscribe from old Controller
61+
- One can discuss message queue, subscription mechanism of IPC etc, DB, interfaces to GUI (if that counts)

README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -443,13 +443,16 @@
443443
6. System Design Examples with Embedded Flavor
444444
1. Design a Cache
445445
2. Design a Vending Machine
446-
3. Design a Traffic Control System
446+
3. [Design a Traffic Control System](Interview/SystemDesign/examples/airControlSystem.md)
447447
4. Design a ATM Machine
448448
5. Design a Elevator System
449-
6. [Design a Parking Lot System](https://github.com/donnemartin/system-design-primer/blob/master/solutions/object_oriented_design/parking_lot/parking_lot.ipynb)
450-
7. Design a File System
451-
8. [Design a Garbage Collector](http://journal.stuffwithstuff.com/2013/12/08/babys-first-garbage-collector/)
452-
9. [Design a call center](https://github.com/donnemartin/system-design-primer/blob/master/solutions/object_oriented_design/call_center/call_center.ipynb)
449+
6. Design a Air Traffic Controller
450+
7. Design a Amazon Locker System
451+
8. Design a service for Alexa devices to report battery status
452+
9. [Design a Parking Lot System](https://github.com/donnemartin/system-design-primer/blob/master/solutions/object_oriented_design/parking_lot/parking_lot.ipynb)
453+
10. Design a File System
454+
11. [Design a Garbage Collector](http://journal.stuffwithstuff.com/2013/12/08/babys-first-garbage-collector/)
455+
12. [Design a call center](https://github.com/donnemartin/system-design-primer/blob/master/solutions/object_oriented_design/call_center/call_center.ipynb)
453456
7. [Embedded System Design Pattern Catalogue](https://embeddedartistry.com/fieldatlas/design-pattern-catalogue/)
454457
8. Embedded System Design Topics
455458
1. Small memory management

0 commit comments

Comments
 (0)