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: book/content/part01/algorithms-analysis.asc
+3-4Lines changed: 3 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -28,15 +28,14 @@ Before going deeper into space and time complexity, let's cover the basics real
28
28
29
29
Algorithms (as you might know) are steps of how to do some tasks. When you cook, you follow a recipe (or an algorithm) to prepare a dish. Let's say you want to make a pizza.
30
30
31
-
.Example of an algorithm
31
+
.Example of an algorithm to make pizza
32
32
[source, javascript]
33
33
----
34
-
import { punchDown, rollOut, applyToppings, Oven } from '../pizza-utils';
34
+
import { rollOut, applyToppings, Oven } from '../pizza-utils';
35
35
36
36
function makePizza(dough, toppings = ['cheese']) {
Copy file name to clipboardExpand all lines: book/content/part01/how-to-big-o.asc
+1Lines changed: 1 addition & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -111,6 +111,7 @@ T(n) = n * [t(statement 1) + m * t(statement 2...3)]
111
111
Assuming the statements from 1 to 3 are `O(1)`, we would have a runtime of `O(n * m)`.
112
112
If instead of `m`, you had to iterate on `n` again, then it would be `O(n^2)`. Another typical case is having a function inside a loop. Let's see how to deal with that next.
113
113
114
+
[[big-o-function-statement]]
114
115
*Function call statements*
115
116
116
117
When you calculate your programs' time complexity and invoke a function, you need to be aware of its runtime. If you created the function, that might be a simple inspection of the implementation. However, if you are using a library function, you might infer it from the language/library documentation.
Arrays are one of the most used data structures. You probably have used it a lot, but are you aware of the runtimes of `splice`, `shift`, `indexOf`, and other operations? In this chapter, we are going deeper into the most common operations and their runtimes.
10
+
Arrays are one of the most used data structures. You probably have used it a lot already. But, are you aware of the runtimes of `push`, `splice`, `shift`, `indexOf`, and other operations? In this chapter, we are going deeper into the most common operations and their runtimes.
11
11
12
12
==== Array Basics
13
13
14
14
An array is a collection of things (strings, characters, numbers, objects, etc.). They can be many or zero.
15
15
16
-
TIP: Strings are a collection of Unicode characters, and most of the array concepts apply to them.
17
-
18
-
.Fixed vs. Dynamic Size Arrays
19
-
****
20
-
Some programming languages have fixed-size arrays like Java and {cpp}.
21
-
Fixed-size arrays might be a hassle when your collection gets full, and you have to create a new one with a bigger size. Those programming languages also have built-in dynamic arrays: we have `vector` in {cpp} and `ArrayList` in Java. Dynamic programming languages like JavaScript, Ruby, and Python use dynamic arrays by default.
22
-
****
16
+
TIP: Strings are a collection of characters. Most of the array methods apply to strings as well.
23
17
24
18
Arrays look like this:
25
19
26
-
.Array representation: each value is accessed through an index.
20
+
.Array representation: You can access each value in constant time through its index.
27
21
image::image16.png[image,width=388,height=110]
28
22
29
-
Arrays are a sequential collection of elements that can be accessed randomly using an index. Let’s take a look at the different operations that we can do with arrays.
23
+
===== Read and Update
30
24
31
-
==== Insertion
25
+
Arrays are a contiguous collection of elements that can be accessed randomly using an index. This access by index operation takes `O(1)` time. Let’s take a look at the different functions that we can do with arrays.
32
26
33
-
Arrays are built-in in most languages. Inserting an element is simple; you can either add them at creation time or after initialization. Below you can find an example for both cases:
34
-
35
-
.Inserting elements into an array
27
+
.Reading elements from an array and string
36
28
[source, javascript]
37
29
----
38
-
// (1) Add elements at the creation time:
39
30
const array = [2, 5, 1, 9, 6, 7];
40
-
41
-
// (2) initialize an empty array and add values later
42
-
const array2 = [];
43
-
array2[3] = 1;
44
-
array2[100] = 2;
45
-
array2 // [empty × 3, 1, empty × 96, 2]
31
+
const string = "hello";
32
+
console.log(array[2]); // 1
33
+
console.log(string[1]); // "e"
46
34
----
47
35
48
-
Using the index, you can replace whatever value you want. Also, you don't have to add items next to each other. The size of the array will dynamically expand to accommodate the data. You can reference values at whatever index you like: index 3 or even 100! In `array2`, we inserted 2 numbers, but the length is 101, and there are 99 empty spaces.
36
+
As you can see, you can access the string's characters using the same operator as arrays.
37
+
38
+
You can update arrays in the same way, using the `[]` operator. However, you can't modify strings. They are immutable!
WARNING: When you try to modify and string, you won't get an error or anything. It just gets ignored! Your only option is to create a new string with the adjusted value.
52
+
53
+
===== Insertion
56
54
57
-
The runtime for inserting elements using an index is always is constant: _O(1)_.
55
+
Insertions on an array have different times complexities.
56
+
O(1): constant time (on average) to append a value at the end of the array.
57
+
O(n): linear time to insert a value at the beginning or middle.
58
58
59
-
===== Inserting at the beginning of the array
59
+
====== Inserting at the beginning of the array
60
60
61
-
What if you want to insert a new element at the beginning of the array? You would have to push every item to the right.
61
+
What if you want to insert a new element at the beginning of the array? You would have to push every item to the right. We can use the following method:
As you can see, `2` was at index 0, now was pushed to index 1, and everything else is on a different index. `unshift` takes *O(n)* since it affects all the elements in the array.
81
+
As you can see, `2` was at index 0, now was pushed to index 1, and everything else is on a different index. `unshift` takes *O(n)* since it affects **all** the elements of the array.
72
82
73
83
.JavaScript built-in `array.unshift`
74
84
****
75
-
The `unshift()` method adds one or more elements to the beginning of an array and returns the array's new length.
85
+
The `unshift()` method adds one or more elements to the beginning of an array and returns its new length.
76
86
77
-
Runtime: O(n).
87
+
Runtime: `O(n)`.
78
88
****
79
89
80
-
===== Inserting at the middle of the array
90
+
====== Inserting at the middle of the array
91
+
92
+
Inserting a new element in the middle involves moving part of the array but not all of the items. We can use `splice` for that:
81
93
82
-
Inserting a new element in the middle involves moving part of the array but not all of the items.
Based on the parameters it takes, you can see that we can add and delete items. Here's an example of inserting in the middle.
83
101
84
102
.Inserting element in the middle
85
103
[source, javascript]
@@ -94,12 +112,20 @@ The Big O for this operation would be *O(n)* since, in the worst case, it would
94
112
95
113
.JavaScript built-in `array.splice`
96
114
****
97
-
The `splice()` method changes an array's contents by removing existing elements or adding new elements. Splice returns an array containing the deleted items.
115
+
The `splice()` method changes an array's contents by removing existing elements or adding new items. Splice returns an array containing the deleted items.
98
116
99
117
Runtime: O(n).
100
118
****
101
119
102
-
===== Inserting at the end of the array
120
+
====== Inserting at the end of the array
121
+
122
+
For inserting items at the end of the array, we can use: push.
*AR-2*) _You are given an array of integers. Each value represents the closing value of the stock on that day. You are only given one chance to buy and then sell. What's the maximum profit you can obtain? (Note: you have to buy first and then sell)_
331
+
*AR-2*) _You are given an array of integers. Each value represents the closing value of the stock on that day. You have only one chance to buy and then sell. What's the maximum profit you can obtain? (Note: you have to buy first and then sell)_
312
332
313
333
Examples:
314
334
@@ -317,7 +337,6 @@ Examples:
317
337
maxProfit([1, 2, 3]) // 2 (buying at 1 and selling at 3)
318
338
maxProfit([3, 2, 1]) // 2 (no buys)
319
339
maxProfit([5, 10, 5, 10]) // 5 (buying at 5 and selling at 10)
320
-
321
340
----
322
341
323
342
// _Seen in interviews at: Amazon, Facebook, Bloomberg_
Copy file name to clipboardExpand all lines: book/part02-linear-data-structures.asc
+5-4Lines changed: 5 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -1,9 +1,11 @@
1
1
[[part02-linear-data-structures]]
2
2
== Linear Data Structures
3
3
4
-
Data Structures comes in many flavors. There’s no one to rule them all. You have to know the tradeoffs so you can choose the right one for the job.
4
+
Data Structures come in many flavors. There's no one to rule them all. You have to know the tradeoffs so you can choose the right one for the job.
5
5
6
-
Even though in your day-to-day, you might not need to re-implementing them, knowing how they work internally would help you know when to use one over the other or even tweak them to create a new one. We are going to explore the most common data structures' time and space complexity.
6
+
In your day-to-day work, you might not need to re-implement basic data structures. However, knowing how they work internally can help you understand their time complexity better (Remember the chapter <<big-o-function-statement, How to determine Big-O from code?>>).
7
+
8
+
When you are aware of the data structures implementations, you spot when to use one over the other or even extend them to create a new one. We are going to explore the most common data structures' time and space complexity.
7
9
8
10
.In this part we are going to learn about the following linear data structures:
9
11
- <<array-chap>>
@@ -22,6 +24,7 @@ If you want to have a general overview of each one, take a look at the following
0 commit comments