Skip to content

Commit b5a00dd

Browse files
committed
feat(book:array): add solutions for interview questions
1 parent 4d75825 commit b5a00dd

File tree

9 files changed

+99
-82
lines changed

9 files changed

+99
-82
lines changed

.vscode/settings.json

-14
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,3 @@
11
{
2-
"workbench.colorCustomizations": {
3-
"activityBar.background": "#fbed80",
4-
"activityBar.foreground": "#15202b",
5-
"activityBar.inactiveForeground": "#15202b99",
6-
"activityBarBadge.background": "#06b9a5",
7-
"activityBarBadge.foreground": "#15202b",
8-
"titleBar.activeBackground": "#f9e64f",
9-
"titleBar.inactiveBackground": "#f9e64f99",
10-
"titleBar.activeForeground": "#15202b",
11-
"titleBar.inactiveForeground": "#15202b99",
12-
"statusBar.background": "#f9e64f",
13-
"statusBarItem.hoverBackground": "#f7df1e",
14-
"statusBar.foreground": "#15202b"
15-
},
162
"peacock.color": "#f9e64f"
173
}
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
[appendix]
2+
[[d-interview-questions-solutions]]
3+
== Interview Questions Solutions
4+
(((Interview Questions Solutions)))
5+
6+
=== Solutions for Array Questions
7+
(((Interview Questions Solutions, Arrays)))
8+
9+
==== Rotate Left
10+
include::content/part02/array.asc[tag=array-q-rotate-left]
11+
12+
We are asked to rotate an array multiple times (`k`).
13+
14+
One brute force solution, would be removing the first element and appending it to the end `k` times:
15+
16+
include::interview-questions/rotate-array-left.js[tag=bruteForce]
17+
18+
However, what would happen if the array is huge (millions of elements)?
19+
How efficient will be if `k` number is large (thousands)?
20+
21+
When k is bigger than the array, it will loop back over and over again. We can avoid extra computation by calculating the final place using modulus.
22+
23+
Here's the final solution:
24+
25+
include::interview-questions/rotate-array-left.js[tag=description]
26+
include::interview-questions/rotate-array-left.js[tag=solution]
27+
28+
It runs on `O(n^2)` while the brute force solution was doing `O(n^2 * k)`.
29+
30+
==== Sum
31+
include::content/part02/array.asc[tag=array-sum]

book/ch02-git-basics-chapter.asc

-35
This file was deleted.

book/content/part02/array.asc

+10-17
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ TIP: Strings are a collection of Unicode characters and most of the array concep
1717

1818
.Fixed vs. Dynamic Size Arrays
1919
****
20-
Some programming languages have fixed size arrays like Java and C++. 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 C++ 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 C++.
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 C++ and `ArrayList` in Java. Dynamic programming languages like JavaScript, Ruby, and Python use dynamic arrays by default.
2122
****
2223

2324
Arrays look like this:
@@ -275,30 +276,21 @@ To sum up, the time complexity of an array is:
275276
|===
276277
//end::table
277278

278-
==== Array Exercises
279+
==== Interview Questions
280+
(((Interview Questions, Arrays)))
279281

282+
// tag::array-q-rotate-left[]
280283
1) Implement an efficient algorithm that rotate an array `a` an `k` number of times.
281284

282285
[source, javascript]
283286
----
284-
/**
285-
* Rotate an array left by k number of times.
286-
*
287-
* @example
288-
* rotateLeft([1,2,3], 1); // [2,3,1]
289-
* rotateLeft([1,2,3,4,5], 4); // [5,1,2,3,4]
290-
*
291-
* rotateLeft(Array(1e6).fill(1), 1e4); // <scale testing>
292-
*
293-
* @param a - The array
294-
* @param k - The number of times the array is rotated
295-
*/
296-
function rotateLeft(a, k) {
297-
// write you code and test with examples
287+
include::../../interview-questions/rotate-array-left.js[tag=description]
288+
// write you code here
298289
}
299290
----
291+
// end::array-q-rotate-left[]
300292

301-
293+
// tag::array-sum[]
302294
2) Implement an algorithm that takes two arrays of numbers and return a new array with the sum.
303295

304296
[source, javascript]
@@ -318,3 +310,4 @@ function sum(a, b) {
318310
// write you code and test with examples
319311
}
320312
----
313+
// end::array-sum[]

book/content/preface.asc

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,4 @@ Measurement is the first step that leads to control and eventually to improvemen
8282

8383
Your feedback is very welcome and valuable. Let us know what your thoughts about this book — what you like or ideas to make it better.
8484

85-
To send us feedback, e-mail us at [email protected], send a tweet to https://twitter.com/iAmAdrianMejia[@iAmAdrianMejia], or using the hash tags `#dsaJS`, `#javascript` and `#algorithms`.
85+
To send us feedback, e-mail us at [email protected], send a tweet to https://twitter.com/iAmAdrianMejia[@iAmAdrianMejia], or using the hash tag `#dsaJS`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// tag::description[]
2+
/**
3+
* Rotate an array left by k number of times.
4+
*
5+
* @example
6+
* rotateLeft([1,2,3], 1); // [2,3,1]
7+
* rotateLeft([1,2,3,4,5], 4); // [5,1,2,3,4]
8+
*
9+
* rotateLeft(Array(1e6).fill(1), 1e4); // <scale testing>
10+
*
11+
* @param a - The array
12+
* @param k - The number of times the array is rotated
13+
*/
14+
function rotateLeft(a, k) {
15+
// end::description[]
16+
// tag::solution[]
17+
const moves = k % a.length;
18+
for (let i = 0; i < moves; i++) {
19+
a.push(a.shift());
20+
}
21+
return a;
22+
}
23+
// end::solution[]
24+
25+
// tag::bruteForce[]
26+
function rotateLeftBruteForce(a, k) {
27+
for (let i = 0; i < k; i++) {
28+
a.push(a.shift());
29+
}
30+
return a;
31+
}
32+
// end::bruteForce[]
33+
34+
module.exports = { rotateLeft, rotateLeftBruteForce };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const { rotateLeft, rotateLeftBruteForce } = require('./rotate-array-left');
2+
3+
describe('Rotate Left', () => {
4+
describe('when data is small', () => {
5+
it('should work with 1', () => {
6+
expect(rotateLeft([1, 2, 3], 1)).toEqual([2, 3, 1]);
7+
});
8+
9+
it('should work with 4', () => {
10+
expect(rotateLeft([1, 2, 3, 4, 5], 4)).toEqual([5, 1, 2, 3, 4]);
11+
});
12+
});
13+
14+
describe('when data is large', () => {
15+
it('', () => {
16+
17+
});
18+
});
19+
});

lab/exercises/01-arrays/rotate-array-left.js

-15
This file was deleted.

notes.md

+4
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,7 @@ alert('foo');
117117
console.log('bar');
118118
/* eslint-enable no-alert */
119119
```
120+
121+
# Asciidoctor Manual
122+
123+
https://asciidoctor.org/docs/user-manual/

0 commit comments

Comments
 (0)