-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
293 lines (218 loc) · 6.75 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
//////////////////////
//// TIME & SPACE ////
//////////////////////
// 1) -------------------------------------------------------
let numbers = [0,1,2,3,4,5,6,7,8,9]
function printNumbers(numbers) {
for (let i = 0; i < numbers.length; i++) {
console.log(numbers[i])
}
}
// Q: Given an array of numbers, what is the time complexity of this function?
// ANSWER: O(n)
// Q: What is the space complexity?
// ANSWER: O(1)
// 2) -------------------------------------------------------
// You are challenged to find the 4th element of an array, and you came up with this solution:
function findFourth(numbers) {
var count = 1
var answer = 0
for (let i = 0; i < numbers.length; i++) {
if (count === 4) {
answer = numbers[i]
return
} else {
count += 1
}
}
}
// Q: What is the time complexity of of this solution?
// ANSWER: O(1)
// Q: Given an array of numbers, what is the space complexity of this function?
// ANSWER: O(1)
// Q: Could you come up with a solution that has a better time complexity? If so, write out that solution and note the time complexity. If not, why not?
// ANSWER: I could not here because for-loops tend to be O(n).
// 3) -------------------------------------------------------
function printA() {
for (let i = 0; i < 5; i++) {
console.log(i)
}
}
// Q: What is the time complexity of this function?
// ANSWER: O(1)
// Q: What is the space complexity of this function?
// ANSWER: O(1)
// 4) -------------------------------------------------------
function printB(number) {
for (let i = 0; i < number; i++) {
console.log(i)
}
}
// Q: What is the time complexity of this function?
// ANSWER: O(n)
// Q: What is the space complexity of this function?
// ANSWER: O(1)
// 5) -------------------------------------------------------
function printC(num, arr) {
for (let i = 0; i < num; i++) {
for (let j = 0; j < arr.length; j++) {
console.log(j)
}
}
}
// Q: What is the time complexity of this function?
// ANSWER: O(n^2)
// Q: What is the space complexity of this function?
// ANSWER: O(1)
// 6) -------------------------------------------------------
function nested(numbers) {
for (let i = 0; i < numbers.length; i++) {
for (let j = 0; j < numbers.length; j++) {
console.log(j)
}
}
}
// Q: What is the time complexity of this function?
// ANSWER: O(n^2)
// Q: What is the space complexity of this function?
// ANSWER: O(1)
// 7) -------------------------------------------------------
let numbers = [76,35,73,13,4,88,23,12,86,1,54,9,10,2]
// remember numbers could be reassigned, added to, etc.
let newNumbersA = numbers.filter((num) => {
return num < 5
})
// Q: What is the time complexity of the filter higher-order function?
// ANSWER: O(n)
// Q: What is the space complexity of the filter higher-order function?
// ANSWER: O(n)
// 8) -------------------------------------------------------
// remember numbers (in #7) could be reassigned, added to, etc.
let newNumbersB = numbers.map((num) => {
return num * 3
})
// Q: What is the time complexity of the map higher-order function?
// ANSWER: O(n)
// Q: What is the space complexity of the map higher-order function?
// ANSWER: O(n)
//////////////
//// TIME ////
//////////////
// 9) -------------------------------------------------------
function removeNum() {
numbers.splice(3,1)
}
// Q: What is the time complexity of the function?
// ANSWER: O(n)
// 10) -------------------------------------------------------
function insertNum() {
numbers.splice(3, 0, 19)
}
// Q: What is the time complexity of the function?
// ANSWER: O(n)
// 11) -------------------------------------------------------
function pushNum() {
numbers.push(25)
}
// Q: What is the time complexity of the function?
// ANSWER: O(1)
// 12) -------------------------------------------------------
// Given an array of names....
function printFirstTwoNames(array) {
console.log(array[0])
console.log(array[1])
}
// Q: What is the time complexity of the function?
// ANSWER: O(1)
///////////////
//// SPACE ////
///////////////
// 13) -------------------------------------------------------
// This function takes in parentArr which is an array of arrays
// It will only ever have 2 levels of arrays (the child arrays can't have array children)
// ex: [[1,2,3], ['a','b','c'], [5,null,'x',0]]
// NOT: [[1,2,3], ['a', ['an', 'invalid', '(grand)child', 'array], 'b', 'c'], [6,7,8,9,10]]
function arrInception(parentArr) {
for (let i = 0; i < parentArr.length; i++) {
let childArr = parentArr[i]
for (let j = 0; j < childArr.length; j++) {
console.log(`value at index ${j} in child arr (at ${i} in parent arr): ${childArr[j]}`)
}
}
}
// Q: What is the space complexity of this function?
// ANSWER: O(n^2)
// 14) You are challenged to find the squared value of each number in an array. This is not the best solution, but it is what you come up with anyway...
function getNumbersSquared(numbers) {
let numbers2 = [...numbers]
let answers = []
for (let i = 0; i < numbers.length; i++) {
let squaredValue = numbers[i] * numbers2[i]
answers.push(squaredValue)
}
return answers
}
// Q: What is the space complexity of this function?
// ANSWER: O(n^2)
// Q: Is it possible to improve this function's space complexity? If it is, write a new function below.
// ANSWER: No.
// 15) Here are two functions that you came up with to count how many of each letter is in a given string and return an object with the individual counts:
function countLetters(str) {
let letters = {
a: 0,
b: 0,
c: 0,
d: 0,
e: 0,
f: 0,
g: 0,
h: 0,
i: 0,
j: 0,
k: 0,
l: 0,
m: 0,
n: 0,
o: 0,
p: 0,
q: 0,
r: 0,
s: 0,
t: 0,
u: 0,
v: 0,
w: 0,
x: 0,
y: 0,
z: 0,
}
str = str.toLowerCase()
const splitStr = str.split('')
splitStr.forEach(char => {
if (letters.hasOwnProperty(char)) {
letters[char]++
}
})
return letters
}
/////////////////////////////
function countLettersRoundTwo(str) {
// this contains a RegEx or Regular Expression, feel free to google
str = str.toLowerCase().replace(/([^a-z])+/g,'')
let letters = {}
for (let i = 0; i < str.length; i++) {
if (letters.hasOwnProperty(str[i])) {
letters[str[i]]++
} else {
letters[str[i]] = 0
}
}
return letters
}
// Q: Do these functions have the same space complexity?
// ANSWER: Yes
// Q: What is the space complexity of each?
// ANSWER - countLetters: O(n)
// ANSWER - countLettersRoundTwo: O(n)
// Q: Explain how you came to your conclusions about the space complexity of each function.
// ANSWER: This depends on the length of the string being passed in the function.