Skip to content

Commit 21f8ca2

Browse files
committed
gramar fixes
1 parent db9fabc commit 21f8ca2

File tree

4 files changed

+77
-77
lines changed

4 files changed

+77
-77
lines changed

book/D-interview-questions-solutions.asc

+27-27
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ include::interview-questions/max-subarray.js[tag=maxSubArrayBrute1]
2929

3030
This code is simple to understand; however, not very efficient. The runtime is `O(n^3)`.
3131

32-
If you noticed we adding up the numbers from `i` to `j` on each cycle. But, we can optimize this. We can keep a local variable and add the new number to it. That way, we don't have to revisit previous numbers.
32+
Notice we're adding up the numbers from `i` to `j` on each cycle. But, we can optimize this. We can keep a local variable and add the new number to it. That way, we don't have to revisit previous numbers.
3333

3434
[source, javascript]
3535
----
@@ -46,7 +46,7 @@ include::interview-questions/max-subarray.js[tag=description]
4646
include::interview-questions/max-subarray.js[tag=solution]
4747
----
4848

49-
The runtime is `O(n)` and a space complexity of `O(1)`.
49+
The runtime is `O(n)` and space complexity of `O(1)`.
5050

5151

5252

@@ -93,7 +93,7 @@ include::interview-questions/buy-sell-stock.js[tag=description]
9393
include::interview-questions/buy-sell-stock.js[tag=solution]
9494
----
9595

96-
The runtime is `O(n)` and a space complexity of `O(1)`.
96+
The runtime is `O(n)` and space complexity of `O(1)`.
9797

9898

9999

@@ -110,15 +110,15 @@ The runtime is `O(n)` and a space complexity of `O(1)`.
110110
[#linkedlist-q-merge-lists]
111111
include::content/part02/linked-list.asc[tag=linkedlist-q-merge-lists]
112112

113-
For this problem we need to visit each node in both list and recontruct them in ascending order. Note: We don't need to copy the values into a new node.
113+
We need to visit each node in both lists and merge them in ascending order. Note: We don't need to copy the values nor create new nodes.
114114

115-
Another case to take into consideration is that list might have different length. So, if one list runs out, we have to keep taking elements from the remaining list.
115+
Another case to take into consideration is that lists might have different lengths. So, if one list runs out, we have to keep taking elements from the remaining list.
116116

117117
*Algorithm*:
118118

119119
- Have a pointer for each list
120120
- While there's a pointer that is not null, visite them
121-
- Compare each list's node's value and take the one that is smaller.
121+
- Compare each list node's value and take the smaller one.
122122
- Advance the pointer of the taken node to the next one.
123123

124124
*Implementation*:
@@ -133,32 +133,32 @@ Notice that we used a "dummy" node or "sentinel node" to have some starting poin
133133

134134
*Complexity Analysis*:
135135

136-
- Time: `O(m+n)`. Visiting each node from the list 1 and list 2 has a time complexity `O(m + n)`, where m and n represents the length of each list repectively.
137-
- Space: `O(1)`. We resuse the same nodes and only change their `next` pointers. We only create one additional node "the sentinel node".
136+
- Time: `O(m+n)`. Visiting each node from the list 1 and list 2 has a time complexity `O(m + n)`. `m` and `n` represent each list's length.
137+
- Space: `O(1)`. We reuse the same nodes and only change their `next` pointers. We only create one additional node, "the sentinel node."
138138

139139

140140
[#linkedlist-q-linkedlist-same-data]
141141
include::content/part02/linked-list.asc[tag=linkedlist-q-linkedlist-same-data]
142142

143-
For this question, we are given two linked lists that contains string data. We want to know if the concatenated strings from each list are the same.
143+
We are given two linked lists that contain string data. We want to know if the concatenated strings from each list are the same.
144144

145-
The tricky part is that same data can be distributed differently on the linked lists:
145+
The tricky part is that the same data can be distributed differently on the linked lists:
146146

147147
----
148148
L1: he -> ll -> o
149149
L2: h -> e -> llo
150150
----
151151

152-
One naive approach could be to go through each list's node and concatenate the strings. Then, we just need to compare them.
152+
One naive approach could be to go through each list's node and concatenate the strings. Then, we can check if they are equal.
153153

154154
[source, javascript]
155155
----
156156
include::interview-questions/linkedlist-same-data.js[tag=hasSameDataBrute1]
157157
----
158158

159-
Notice that the problem mentions that lists could be VERY large (millions of nodes). If the first character on each list are different, we are unecessarily computing millions of nodes, when one simple check will do the job.
159+
Notice that the problem mentions that lists could be huge (millions of nodes). If the first character on each list is different, we are unnecessarily computing millions of nodes, when a straightforward check will do the job.
160160

161-
A better way to solve this problem is iterating over each character on both lists and when we found mistmatch we return `false`. If they are the same we will go over all of them.
161+
A better way to solve this problem is iterating over each character on both lists, and when we found mistmatch, we return `false` immediately. If they are the same, we still have to visit all of them.
162162

163163
*Algorithm*:
164164

@@ -183,7 +183,7 @@ If the index overflows, it moves to the next node and reset the index to zero.
183183

184184
*Complexity Analysis*:
185185

186-
- Time: `O(n)`. We go over all the characters on each lists
186+
- Time: `O(n)`. We go over all the characters on each list
187187
- Space: `O(1)`. Only using pointers and no auxiliary data structures.
188188

189189

@@ -200,16 +200,16 @@ include::content/part02/stack.asc[tag=stack-q-valid-parentheses]
200200

201201
.We need to validate that brackets are properly opened and closed, following these rules:
202202
- An opened bracket must be close by the same type.
203-
- Open brackets mush be closed in the corrent order.
203+
- Open brackets mush be closed in the correct order.
204204

205-
This is a parsing problem and usually stacks are a good candidates for them.
205+
This is a parsing problem, and usually, stacks are good candidates for them.
206206

207207
*Algorithm*:
208208

209-
- Create a mapping of opening bracket with its closing bracket
209+
- Create a mapping for each opening bracket, to its closing counterpart.
210210
- Iterate through the string
211211
- When we found an opening bracket, insert the corresponding closing bracket into the stack.
212-
- When we found a closing bracket, pop from the stack and make sure it correspond to the current character.
212+
- When we found a closing bracket, pop from the stack and make sure it corresponds to the current character.
213213
- Check the stack is empty. If there's a leftover, it means that something didn't close properly.
214214

215215
*Implementation*:
@@ -230,23 +230,23 @@ include::interview-questions/valid-parentheses.js[tag=solution]
230230
[#stack-q-daily-temperatures]
231231
include::content/part02/stack.asc[tag=stack-q-daily-temperatures]
232232

233-
The first solution that might come to mind it's using two for loops. For each element we count how many elements ahead there's a warmer temperature.
233+
The first solution that might come to mind it's using two for loops. For each element, we have visit each temperature ahead to find a bigger one.
234234

235235
[source, javascript]
236236
----
237237
include::interview-questions/daily-temperatures.js[tag=dailyTemperaturesBrute1]
238238
----
239239

240-
This solution is a `O(n^2)`. Can we do better? We can!
240+
This solution is an `O(n^2)`. Can we do better? We can!
241241

242-
Here's an idea: start backwards so we know when there's a warmer temperature before hand. The last element is always 0 (because there's no more temperatures ahead of it). We can place each element's index that we visit on a stack. If the current temperature is bigger than the on the top of the stack, we remove it until a bigger one remains or the stack is empty. If the stack has a value, we calculate the number of days ahead.
242+
Here's an idea: start backward, so we know when there's a warmer temperature beforehand. The last element is always 0 (because there are no more temperatures ahead of it). We can place each element's index that we visit on a stack. If the current weather is bigger than the stack top, we remove it until a bigger one remains or the stack is empty. If the stack has a value, we calculate the number of days ahead. Otherwise, it is 0.
243243

244244
*Algorithm*:
245245

246-
- Traverse the daily temperatures backwards
247-
- Push each temperature to an stack.
248-
- While the current temperature is larger than the one at the top of the stack, remove it.
249-
- If the stack is empty, then there's no warmer weather ahead.
246+
- Traverse the daily temperatures backward
247+
- Push each temperature to a stack.
248+
- While the current temperature is larger than the one at the top of the stack, pop it.
249+
- If the stack is empty, then there's no warmer weather ahead, so it's 0.
250250
- If the stack has an element, calculate the index delta.
251251

252252
*Implementation*:
@@ -257,12 +257,12 @@ include::interview-questions/daily-temperatures.js[tag=description]
257257
include::interview-questions/daily-temperatures.js[tag=solution]
258258
----
259259

260-
The stack contains the indexes rather than the temperatures.
260+
The stack contains the indexes rather than the temperatures themselves.
261261

262262
*Complexity Analysis*:
263263

264264
- Time: `O(n)`. We visit each element on the array once.
265-
- Space: `O(1)`. The worst case scenario is ascending order without duplicates. The stack will hold at most 70 items (100 - 30). If we didn't have the range restriction then space complexity would be `O(n)`.
265+
- Space: `O(1)`. The worst-case scenario is ascending order without duplicates. The stack will hold at most 70 items (100 - 30). If we didn't have the range restriction, then space complexity would be `O(n)`.
266266

267267

268268

book/content/part02/array.asc

+16-16
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,30 @@ 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, 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.
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.
16+
TIP: Strings are a collection of Unicode characters, and most of the array concepts apply to them.
1717

1818
.Fixed vs. Dynamic Size Arrays
1919
****
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. For that, 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.
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.
2222
****
2323

2424
Arrays look like this:
2525

2626
.Array representation: each value is accessed through an index.
2727
image::image16.png[image,width=388,height=110]
2828

29-
Arrays are a sequential collection of elements that can be accessed randomly using an index. Let’s take a look into the different operations that we can do with arrays.
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.
3030

3131
==== Insertion
3232

33-
Arrays are built-in into 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:
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:
3434

3535
.Inserting elements into an array
3636
[source, javascript]
@@ -45,7 +45,7 @@ array2[100] = 2;
4545
array2 // [empty × 3, 1, empty × 96, 2]
4646
----
4747

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

5050
[source, javascript]
5151
----
@@ -54,7 +54,7 @@ console.log(array2); // [empty × 3, 1, empty × 96, 2]
5454
----
5555

5656

57-
The runtime for inserting elements using index is always is constant: _O(1)_.
57+
The runtime for inserting elements using an index is always is constant: _O(1)_.
5858

5959
===== Inserting at the beginning of the array
6060

@@ -72,7 +72,7 @@ As you can see, `2` was at index 0, now was pushed to index 1, and everything el
7272

7373
.JavaScript built-in `array.unshift`
7474
****
75-
The `unshift()` method adds one or more elements to the beginning of an array and returns the new length of the array.
75+
The `unshift()` method adds one or more elements to the beginning of an array and returns the array's new length.
7676
7777
Runtime: O(n).
7878
****
@@ -90,11 +90,11 @@ array.splice(1, 0, 111); // ↪️ [] <1>
9090
----
9191
<1> at position `1`, delete `0` elements and insert `111`.
9292

93-
The Big O for this operation would be *O(n)* since in worst case it would move most of the elements to the right.
93+
The Big O for this operation would be *O(n)* since, in the worst case, it would move most of the elements to the right.
9494

9595
.JavaScript built-in `array.splice`
9696
****
97-
The `splice()` method changes the contents of an array by removing existing elements or adding new elements. Splice returns an array containing the deleted elements.
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.
9898
9999
Runtime: O(n).
100100
****
@@ -116,15 +116,15 @@ Adding to the tail of the array doesn’t change other indexes. E.g., element 2
116116

117117
.JavaScript built-in `array.push`
118118
****
119-
The `push()` method adds one or more elements to the end of an array and returns the new length of the array.
119+
The `push()` method adds one or more elements to the end of an array and returns the array's new length.
120120
121121
Runtime: O(1).
122122
****
123123

124124
[[array-search-by-value]]
125125
==== Searching by value and index
126126

127-
Searching by index is very easy using the `[]` operator:
127+
Searching by the index is very easy using the `[]` operator:
128128

129129
.Search by index
130130
[source, javascript]
@@ -185,7 +185,7 @@ We would have to loop through the whole array (worst case) or until we find it:
185185

186186
==== Deletion
187187

188-
There are three possible scenarios for deletion (similar to insertion): removing at the beginning, middle or end.
188+
There are three possible deletion scenarios (similar to insertion): removing at the beginning, middle, or end.
189189

190190
===== Deleting element from the beginning
191191

@@ -224,7 +224,7 @@ array.splice(2, 1); // ↪️[2] <1>
224224
----
225225
<1> delete 1 element at position 2
226226

227-
Deleting from the middle might cause most of the elements of the array to move up one position to fill in for the eliminated item. Thus, runtime: O(n).
227+
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).
228228

229229
===== Deleting element from the end
230230

@@ -297,7 +297,7 @@ _Solution: <<array-q-max-subarray>>_
297297
// tag::array-q-buy-sell-stock[]
298298
===== Best Time to Buy and Sell an Stock
299299

300-
*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 maximun profit you can obtain? (Note: you have to buy first and then sell)_
300+
*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)_
301301
// end::array-q-buy-sell-stock[]
302302

303303
[source, javascript]

0 commit comments

Comments
 (0)