You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+53-99Lines changed: 53 additions & 99 deletions
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,4 @@
1
-
# Data Structures and Algorithms
1
+
# Data Structures and Algorithms
2
2
3
3
These notes can be used to find top patterns for each category of questions and concepts. Note that reading these notes can be much faster than watching videos. Also, it can be used to refresh your knowledge or understand the important parts of each pattern. This repository provides the key information presented from all these sources:
4
4
@@ -20,6 +20,12 @@ These notes can be used to find top patterns for each category of questions and
20
20
Source: [Data Structures, Algorithms, and Machine Learning Optimization](https://www.oreilly.com/library/view/data-structures-algorithms/9780137644889/)
21
21
# 2. Big O Notion
22
22
23
+
The *rate of increase* of an algorithm is also referred to as the **order** of the algorithm
24
+
25
+
For example, instead of saying "this relationship has a linear rate of increase", we could instead say, "The *order* of this relationship is linear".
26
+
27
+
*O Notation*, and you'll see that the "O" in the name refers to the **o**rder of the rate of increase.
28
+
23
29

24
30
25
31

@@ -33,41 +39,25 @@ We keep the *m*. Given that it might be large. 
33
39
34
40
# 3. Data structures
35
41
36
-
37
-

38
42
- Common data structures:
39
43

40
44
- Big O notion
41
45

42
46
43
-
Linked lists are not indexed. Only nodes are linked toghether. 
47
+
Linked lists are not indexed. Only nodes are linked together. 
44
48
45
49

46
50
47
-
- Stacks are implemented as lists in python. s.append, s.pop
48
-
- Queue: beginning and end are available. You can also take a peek of the first one.
49
-
- Deques (دک) (Double ended- queue):
51
+
- Stacks are implemented as lists in Python. s.append, s.pop
52
+
- Queue: beginning and end are available. You can also take a peek at the first one.
53
+
- Deques (Double ended- queue):
50
54
51
55
# 4. Algorithms
52
56
53
-
The summary of **Data Structures and Algorithms** plus **Interview steps** from Udacity's nano degree "Data Structures and Algorithms".
54
-
55
-
I’ve included the code for each concept in Python in the code folder. I would suggest to implement short codes for your projects or coding interviews.
57
+
The summary of **Data Structures and Algorithms** plus **Interview steps**.
56
58
57
59
## Solution to any problems
58
-
59
-
Think in simple terms:
60
-
61
-
1. What are the inputs
62
-
2. What are the outputs
63
-
3. Step-by-step ways that can connect the two
64
-
65
-
**Next:**
66
-
67
-
Do some examples on paper or on your mind. The trick is to find the correct **relationships** between input and output. Finally, you can test some cases and fine-tune your approach for special cases.
68
-
69
-
Writing the algorithm can be done by writing the *pseudocode*.
70
-
60
+
Take a look at a simple question like below:
71
61
Question 1: Write an algorithm/program that returns the difference of two dates.
72
62
73
63
**Step 1: Ask yourself the following questions**
@@ -87,122 +77,86 @@ Tips: Break into simple parts so that we can see our progress. Write simple smal
87
77
88
78
Now we look into the main topics that are being covered in most interviews/coding challenges. We also look into the patterns that appear in solutions which can be used in new problems. The goal is to understand the fundamentals and try to use them to solve problems.
89
79
90
-
## Big O Notion
91
-
92
-
As the input to an algorithm increases, the time required to run the algorithm may also increase.
93
-
94
-
For example in **Nested Loops**:
95
-
96
-
\` For i in range(len(n)):
97
-
98
-
```
99
-
For j in range(len(n)):
100
-
```
101
-
102
-
Print(“hello World”) \`
103
-
104
-
n\^2
105
-
106
-
Will increases the lines dramatically
107
-
108
-
The *rate of increase* of an algorithm is also referred to as the **order** of the algorithm
109
-
110
-
For example, instead of saying "this relationship has a linear rate of increase", we could instead say, "The *order* of this relationship is linear".
111
-
112
-
*O Notation*, and you'll see that the "O" in the name refers to the **o**rder of the rate of increase.
113
-
114
-
Length of input to my function
115
-
116
-
O(2n+2) \> n=10 -\> 22
117
-
118
-
In n\^2 + 5*n*2+5, the 55 has very little impact on the total efficiency—especially as the input size gets larger and larger. Asking the computer to do 10,005 operations vs. 10,000 operations makes little difference. Thus, it is the n\^2*n*2 that we really care about the most, and the + makes little difference
119
-
120
-
Interviewer wants us to think about efficiency.
121
-
122
-
Run time analysis
123
-
124
-
O(3N) space efficiency (copying code)
125
-
126
-
Int , float 4 bytes
127
-
128
-
char 1 byte
129
80
130
81
## Binary Search
131
82
What are time complexity of the following problems?
132
83
Finding the smallest of n numbers?
133
-
• O(n-1)
134
-
Finding the biggest of n numbers?
135
-
• O(n-1)
136
-
Finding the smallest and the biggest of n numbers?
137
-
• O(2n-3)
138
-
139
-
**Binary search (Middle …. Middle, O (log(n)).:**
140
84
141
-

85
+
• Answer: O(n-1) ~ O(n)
86
+
87
+
Finding the biggest of n numbers?
142
88
143
-
**Algorithm:** Tricks to solve a problem
89
+
• Answer: O(n-1) ~ O(n)
90
+
91
+
Finding the smallest and the biggest of n numbers?
144
92
93
+
• Answer: O(2n-3) ~ O(n)
145
94
**Q:** How to find number 25 in this a sorted array?
146
-
147
95
**A:**
148
96
149
97
**1. Linear search: O(n)**
150
98
151
99
**2. Binary Search:** O(log(n)+1) ( Why it is called binary search? find a postion of binary)
Second, we compare the elements and join the elements
168
122
169
123

170
124
171
125
Efficiency:
172
126
173
-
O (n Log (n))
127
+
O (n log (n))
174
128
175
-
Why we are seeing log (n) in our efficiency? Hint: same as binary search problem
129
+
**Q:**Why we are seeing log (n) in our efficiency? Hint: same as the binary search problem
176
130
177
131
**Quick Sort:**
178
-
179
-
Pick one and move it around
132
+
Idea is to use pivots to sort the array.
180
133
181
134

182
135
183
-
Why it can be chosen as the most efficient algorithm?
184
-
185
-
Because on average it will outperform merge sort.
186
136
187
137
On each step we are performing two moving around.
188
138
189
139

190
140
191
-
(Pivot = Last element) First move we move
141
+
(Pivot = Last element)
192
142
193
-
1. Select right P
194
-
2. PL LS SP (Shift Pivot to left , Swap start and left)
195
-
3. Compare Start with pivot. If (Start \> Pivot repeat step 2)
196
-
4. Else move to second start
143
+
1. Select the rightmost element as the pivot
144
+
2. Shift Pivot to left by one element
145
+
3. Swap start and left
146
+
4. Compare Start with pivot. If (Start \> Pivot repeat step 2)
147
+
5. Else move to second start
197
148
198
149

199
150
200
151

201
152
202
-
I advise to practice this for cementing the algorithm.
203
-
204
153

205
154
155
+
Why it can be chosen as the most efficient algorithm?
156
+
157
+
Because on average it will outperform merge sort.
158
+
159
+
206
160
**Great no need to move the original pivot anymore (2 is at the right place)**
207
161
208
162

@@ -213,8 +167,8 @@ New pivot on right. Compare
213
167
214
168
Moving phase
215
169
216
-
Everything less than 8 and 10 sweap results in:
217
-
Everythin less than 8 are already below 8 so cemented
170
+
Everything less than 8 and 10 sweep results in:
171
+
Everything less than 8 is already below 8
218
172
219
173

220
174
@@ -224,7 +178,7 @@ O(n2) if it’s already sorted. Why? Wasting time
224
178
225
179
226
180
## Sliding Window
227
-
How to tell if its a sliding window problem?
181
+
How to tell if it's a sliding window problem?
228
182
• Deals with arrays
229
183
• Find/calculate among subarrays
230
184
@@ -233,11 +187,11 @@ How to tell if its a sliding window problem?
233
187
234
188
Input: ![[Pasted image 20220910145630.png]]
235
189
Output: ![[Pasted image 20220910145753.png]]
236
-
Alg1: Bruteforce return the average for every 5 element
190
+
Alg1: Bruteforce returns the average for every 5 element
237
191
Time: O(N * K)
238
-
• Its not optimal because we are calculating the sum of overlapping elements between subarray more than once.
192
+
• Its not optimal because we are calculating the sum of overlapping elements between subarrays more than once.
239
193
![[Pasted image 20220910150423.png]]
240
-
• Why not use a sliding window and substract and add to the sum?
194
+
• Why not use a sliding window and subtract and add to the sum?
241
195
![[Pasted image 20220910150647.png]]
242
196
Alg2: Sliding window
243
197
@@ -247,7 +201,7 @@ Alg2: Sliding window
247
201
248
202
(Programming == tables)
249
203
250
-
Factorial problem in python:
204
+
The factorial problem in Python:
251
205
252
206

253
207
@@ -257,9 +211,9 @@ Knapsack problem: Max value for weight limit
257
211
258
212

259
213
260
-
Knapsack. Imagine the skyrim world where you can carry a certain weight (50kg) in your bag, how can you gather the most valuable items (3w,500v) with you?
214
+
Knapsack. If you have to carry a weight (50kg) in your bag, how can you gather the most valuable items (e.g., 3 as the weight,500 as the value) with you?
261
215
262
-
How you optimize which items with their weights to carry with you?
216
+
How do you optimize which items with their weights to carry with you?
263
217
264
218
1. Brute force: Check all the solutions and pick the best one: O (2\^n) possible combinations. “exponential time”
0 commit comments