Skip to content

Commit 04836cd

Browse files
committedOct 17, 2020
fix(book/array): improve examples and grammar
1 parent 29f374a commit 04836cd

File tree

7 files changed

+127
-107
lines changed

7 files changed

+127
-107
lines changed
 

‎book/content/part01/algorithms-analysis.asc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,14 @@ Before going deeper into space and time complexity, let's cover the basics real
2828

2929
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.
3030

31-
.Example of an algorithm
31+
.Example of an algorithm to make pizza
3232
[source, javascript]
3333
----
34-
import { punchDown, rollOut, applyToppings, Oven } from '../pizza-utils';
34+
import { rollOut, applyToppings, Oven } from '../pizza-utils';
3535
3636
function makePizza(dough, toppings = ['cheese']) {
3737
const oven = new Oven(450);
38-
const punchedDough = punchDown(dough);
39-
const rolledDough = rollOut(punchedDough);
38+
const rolledDough = rollOut(dough);
4039
const rawPizza = applyToppings(rolledDough, toppings);
4140
const pizzaPromise = oven.bake(rawPizza, { minutes: 20 });
4241
return pizzaPromise;

‎book/content/part01/how-to-big-o.asc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ T(n) = n * [t(statement 1) + m * t(statement 2...3)]
111111
Assuming the statements from 1 to 3 are `O(1)`, we would have a runtime of `O(n * m)`.
112112
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.
113113

114+
[[big-o-function-statement]]
114115
*Function call statements*
115116

116117
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.

‎book/content/part02/array.asc

Lines changed: 118 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -7,79 +7,97 @@ endif::[]
77
=== Array [[array-chap]]
88
(((Array)))
99
(((Data Structures, Linear, Array)))
10-
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.
1111

1212
==== Array Basics
1313

1414
An array is a collection of things (strings, characters, numbers, objects, etc.). They can be many or zero.
1515

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.
2317

2418
Arrays look like this:
2519

26-
.Array representation: each value is accessed through an index.
20+
.Array representation: You can access each value in constant time through its index.
2721
image::image16.png[image,width=388,height=110]
2822

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
3024

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.
3226

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
3628
[source, javascript]
3729
----
38-
// (1) Add elements at the creation time:
3930
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"
4634
----
4735

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!
4939

40+
.Reading elements from an array and string
5041
[source, javascript]
5142
----
52-
console.log(array2.length); // 101
53-
console.log(array2); // [empty × 3, 1, empty × 96, 2]
43+
const array = [2, 5, 1, 9, 6, 7];
44+
const string = "hello";
45+
array[2] = 117;
46+
console.log(array[2]); // 117
47+
string[1] = "z"; // doesn't change the string.
48+
console.log(string[1]); // "e"
5449
----
5550

51+
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
5654

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.
5858

59-
===== Inserting at the beginning of the array
59+
====== Inserting at the beginning of the array
6060

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:
62+
63+
.Syntax
64+
[source, javascript]
65+
----
66+
const newArrLength = arr.unshift(element1[, ...[, elementN]]);
67+
----
68+
69+
Here's an example:
6270

6371
.Insert to head
6472
[source, javascript]
6573
----
66-
const array = [2, 5, 1, 9, 6, 7];
74+
const array = [2, 5, 1];
6775
array.unshift(0); // ↪️ 8
68-
// array: [0, 2, 5, 1, 9, 6, 7]
76+
console.log(array); // [ 0, 2, 5, 1 ]
77+
array.unshift(-2, -1); // ↪️ 6
78+
console.log(array); // [ -2, -1, 0, 2, 5, 1 ]
6979
----
7080

71-
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.
7282

7383
.JavaScript built-in `array.unshift`
7484
****
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.
7686
77-
Runtime: O(n).
87+
Runtime: `O(n)`.
7888
****
7989

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:
8193

82-
Inserting a new element in the middle involves moving part of the array but not all of the items.
94+
.Syntax
95+
[source, javascript]
96+
----
97+
const arrDeletedItems = arr.splice(start[, deleteCount[, item1[, item2[, ...]]]]);
98+
----
99+
100+
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.
83101

84102
.Inserting element in the middle
85103
[source, javascript]
@@ -94,12 +112,20 @@ The Big O for this operation would be *O(n)* since, in the worst case, it would
94112

95113
.JavaScript built-in `array.splice`
96114
****
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.
98116
99117
Runtime: O(n).
100118
****
101119

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.
123+
124+
.Syntax
125+
[source, javascript]
126+
----
127+
const newArrLength = arr.push([element1[, ...[, elementN]]]);
128+
----
103129

104130
We can push new values to the end of the array like this:
105131

@@ -116,15 +142,15 @@ Adding to the tail of the array doesn’t change other indexes. E.g., element 2
116142

117143
.JavaScript built-in `array.push`
118144
****
119-
The `push()` method adds one or more elements to the end of an array and returns the array's new length.
145+
The `push()` method adds one or more elements to the end of an array and returns its new length.
120146
121147
Runtime: O(1).
122148
****
123149

124150
[[array-search-by-value]]
125-
==== Searching by value and index
151+
===== Searching by value and index
126152

127-
Searching by the index is very easy using the `[]` operator:
153+
As we saw before, searching by the index is very easy using the `[]` operator:
128154

129155
.Search by index
130156
[source, javascript]
@@ -133,75 +159,56 @@ const array = [2, 5, 1, 9, 6, 7];
133159
array[4]; // ↪️ 6
134160
----
135161

136-
Searching by index takes constant time - *O(1)* - to retrieve values out of the array. If we want to get fancier, we can create a function:
162+
Searching by index takes constant time - *O(1)* - to retrieve values out of the array.
137163

138-
// image:image17.png[image,width=528,height=293]
164+
Searching by value can be done using `indexOf`.
139165

140-
.Search by index
166+
.Syntax
141167
[source, javascript]
142168
----
143-
/**
144-
* Search for array's element by index
145-
*
146-
* @example Given array = [2, 5, 1, 9, 6, 7, -1];
147-
* searchByIndex(array, 3); //↪️ 9
148-
* searchByIndex(array, 6); //↪️ -1
149-
* searchByIndex(array, 13); //↪️ undefined
150-
* @param {array} array
151-
* @param {number} index
152-
* @returns {any} value or undefined if not found
153-
*/
154-
function searchByIndex(array, index) {
155-
return array[index];
156-
}
169+
const index = arr.indexOf(searchElement[, fromIndex]);
157170
----
158171

159-
Finding out if a value is in the array or not is a different story.
160-
161-
// image:image18.png[image,width=528,height=338]
172+
If the value is there, we will get the index, otherwise `-1`.
162173

163174
.Search by value
164175
[source, javascript]
165176
----
166-
/**
167-
* Search for array's element by value
168-
*
169-
* @example Given array = [2, 5, 1, 9, 6, 7];
170-
* searchByValue(array, 9); //↪️ 3
171-
* searchByValue(array, 13); //↪️ -1
172-
* @param {array} array
173-
* @param {any} value
174-
*/
175-
function searchByValue(array, value) {
176-
for (let index = 0; index < array.length; index++) {
177-
const element = array[index];
178-
if (element === value) return index;
179-
}
180-
return -1;
181-
}
177+
const array = [2, 5, 1, 9, 6, 7];
178+
console.log(array.indexOf(9)); // ↪️ 3
179+
console.log(array.indexOf(90)); // ↪️ -1
182180
----
183181

184-
We would have to loop through the whole array (worst case) or until we find it: *O(n)*.
182+
Internally, `indexOf` has to loop through the whole array (worst case) or until we find the first occurrence. Time complexity is *O(n)*.
185183

186-
==== Deletion
184+
===== Deletion
187185

188186
There are three possible deletion scenarios (similar to insertion): removing at the beginning, middle, or end.
189187

190-
===== Deleting element from the beginning
188+
====== Deleting element from the beginning
189+
190+
Deleting from the beginning can be done using the `splice` function and the `shift`. For simplicity, we will use the latter.
191191

192-
Deleting from the beginning can be done using the `splice` function and also the `shift`. For simplicity, we will use the latter.
192+
.Syntax
193+
[source, javascript]
194+
----
195+
const removedElement = arr.shift();
196+
let arrDeletedItems = arr.splice(start[, deleteCount[, item1[, item2[, ...]]]]);
197+
----
193198

194199
.Deleting from the beginning of the array.
195200
[source, javascript]
196201
----
197202
const array = [2, 111, 5, 1, 9, 6, 7];
198203
// Deleting from the beginning of the array.
199-
array.shift(); // ↪️2
200-
array.shift(); // ↪️111
201-
// array: [5, 1, 9, 6, 7]
204+
array.shift(); // ↪️ 2
205+
array.shift(); // ↪️ 111
206+
console.log(array); // [5, 1, 9, 6, 7]
207+
array.splice(0, 1); // ↪️ [ 5 ]
208+
console.log(array); // [ 1, 9, 6, 7 ]
202209
----
203210

204-
As expected, this will change every index, so this takes *O(n)*.
211+
As expected, this will change every index on the array, so this takes linear time: *O(n)*.
205212

206213
.JavaScript built-in array.shift
207214
****
@@ -210,25 +217,35 @@ The `shift()` method shift all elements to the left. In turn, it removes the fir
210217
Runtime: O(n).
211218
****
212219

213-
===== Deleting element from the middle
220+
====== Deleting element from the middle
214221

215222
We can use the `splice` method for deleting an item from the middle of an array.
216223

224+
You can delete multiple items at once:
225+
217226
.Deleting from the middle
218227
[source, javascript]
219228
----
220229
const array = [0, 1, 2, 3, 4];
221230
// Deleting from the middle
222-
array.splice(2, 1); // ↪️[2] <1>
223-
// array: [0, 1, 3, 4]
231+
array.splice(2, 3); // ↪️ [ 2, 3, 4 ] <1>
232+
console.log(array); // [0, 1]
224233
----
225-
<1> delete 1 element at position 2
234+
<1> delete 3 elements starting on position 2
226235

227236
Deleting from the middle might cause most of the array elements to move up one position to fill in for the eliminated item. Thus, runtime: O(n).
228237

229-
===== Deleting element from the end
238+
====== Deleting element from the end
239+
240+
Removing the last element is very straightforward using pop:
230241

231-
Removing the last element is very straightforward:
242+
.Syntax
243+
[source, javascript]
244+
----
245+
const removedItem = arr.pop();
246+
----
247+
248+
Here's an example:
232249

233250
.Deleting last element from the array
234251
[source, javascript]
@@ -238,7 +255,7 @@ array.pop(); // ↪️111
238255
// array: [2, 5, 1, 9]
239256
----
240257

241-
No other element has been shifted, so it’s an _O(1)_ runtime.
258+
No other element was touched, so it’s an _O(1)_ runtime.
242259

243260
.JavaScript built-in `array.pop`
244261
****
@@ -247,7 +264,7 @@ The `pop()` method removes the last element from an array and returns that eleme
247264
Runtime: O(1).
248265
****
249266

250-
==== Array Complexity
267+
===== Array Complexity
251268

252269
To sum up, the time complexity of an array is:
253270
(((Tables, Linear DS, Array Complexities)))
@@ -268,11 +285,14 @@ To sum up, the time complexity of an array is:
268285
.Array Operations time complexity
269286
|===
270287
| Operation | Time Complexity | Usage
271-
| push ^| O(1) | Insert element to the right side.
272-
| pop ^| O(1) | Remove the rightmost element.
273-
| unshift ^| O(n) | Insert element to the left side.
274-
| shift ^| O(n) | Remove leftmost element.
275-
| splice ^| O(n) | Insert and remove from anywhere.
288+
| `push` ^| O(1) | Insert element on the right side.
289+
| `pop` ^| O(1) | Remove the rightmost element.
290+
| `[]` ^| O(1) | Search for element by index.
291+
| `indexOf` ^| O(n) | Search for element by value.
292+
| `unshift` ^| O(n) | Insert element on the left side.
293+
| `shift` ^| O(n) | Remove leftmost element.
294+
| `splice` ^| O(n) | Insert and remove from anywhere.
295+
| `slice` ^| O(n) | Returns shallow copy of the array.
276296
|===
277297
//end::table
278298

@@ -306,9 +326,9 @@ include::../../interview-questions/max-subarray.js[tag=description]
306326
_Solution: <<array-q-max-subarray>>_
307327

308328
// tag::array-q-buy-sell-stock[]
309-
===== Best Time to Buy and Sell an Stock
329+
===== Best Time to Buy and Sell a Stock
310330

311-
*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)_
312332

313333
Examples:
314334

@@ -317,7 +337,6 @@ Examples:
317337
maxProfit([1, 2, 3]) // 2 (buying at 1 and selling at 3)
318338
maxProfit([3, 2, 1]) // 2 (no buys)
319339
maxProfit([5, 10, 5, 10]) // 5 (buying at 5 and selling at 10)
320-
321340
----
322341

323342
// _Seen in interviews at: Amazon, Facebook, Bloomberg_
649 Bytes
Loading
2.65 KB
Loading
166 KB
Loading

‎book/part02-linear-data-structures.asc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
[[part02-linear-data-structures]]
22
== Linear Data Structures
33

4-
Data Structures comes in many flavors. Theres 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.
55

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.
79

810
.In this part we are going to learn about the following linear data structures:
911
- <<array-chap>>
@@ -22,6 +24,7 @@ If you want to have a general overview of each one, take a look at the following
2224
+++
2325
endif::[]
2426

27+
<<<
2528
include::content/part02/array.asc[]
2629

2730
<<<
@@ -35,5 +38,3 @@ include::content/part02/queue.asc[]
3538

3639
<<<
3740
include::content/part02/array-vs-list-vs-queue-vs-stack.asc[]
38-
39-

0 commit comments

Comments
 (0)
Please sign in to comment.