Skip to content

[pull] master from amejiarosario:master #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Aug 22, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat(book:array): add solutions for interview questions
  • Loading branch information
amejiarosario committed Aug 20, 2020
commit b5a00dded45a1f4eeab3bbbdd68ccbcbf02419d3
14 changes: 0 additions & 14 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
{
"workbench.colorCustomizations": {
"activityBar.background": "#fbed80",
"activityBar.foreground": "#15202b",
"activityBar.inactiveForeground": "#15202b99",
"activityBarBadge.background": "#06b9a5",
"activityBarBadge.foreground": "#15202b",
"titleBar.activeBackground": "#f9e64f",
"titleBar.inactiveBackground": "#f9e64f99",
"titleBar.activeForeground": "#15202b",
"titleBar.inactiveForeground": "#15202b99",
"statusBar.background": "#f9e64f",
"statusBarItem.hoverBackground": "#f7df1e",
"statusBar.foreground": "#15202b"
},
"peacock.color": "#f9e64f"
}
31 changes: 31 additions & 0 deletions book/D-interview-questions-solutions.asc
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
[appendix]
[[d-interview-questions-solutions]]
== Interview Questions Solutions
(((Interview Questions Solutions)))

=== Solutions for Array Questions
(((Interview Questions Solutions, Arrays)))

==== Rotate Left
include::content/part02/array.asc[tag=array-q-rotate-left]

We are asked to rotate an array multiple times (`k`).

One brute force solution, would be removing the first element and appending it to the end `k` times:

include::interview-questions/rotate-array-left.js[tag=bruteForce]

However, what would happen if the array is huge (millions of elements)?
How efficient will be if `k` number is large (thousands)?

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.

Here's the final solution:

include::interview-questions/rotate-array-left.js[tag=description]
include::interview-questions/rotate-array-left.js[tag=solution]

It runs on `O(n^2)` while the brute force solution was doing `O(n^2 * k)`.

==== Sum
include::content/part02/array.asc[tag=array-sum]
35 changes: 0 additions & 35 deletions book/ch02-git-basics-chapter.asc

This file was deleted.

27 changes: 10 additions & 17 deletions book/content/part02/array.asc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ TIP: Strings are a collection of Unicode characters and most of the array concep

.Fixed vs. Dynamic Size Arrays
****
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.
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.
****

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

==== Array Exercises
==== Interview Questions
(((Interview Questions, Arrays)))

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

[source, javascript]
----
/**
* Rotate an array left by k number of times.
*
* @example
* rotateLeft([1,2,3], 1); // [2,3,1]
* rotateLeft([1,2,3,4,5], 4); // [5,1,2,3,4]
*
* rotateLeft(Array(1e6).fill(1), 1e4); //
*
* @param a - The array
* @param k - The number of times the array is rotated
*/
function rotateLeft(a, k) {
// write you code and test with examples
include::../../interview-questions/rotate-array-left.js[tag=description]
// write you code here
}
----
// end::array-q-rotate-left[]


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

[source, javascript]
Expand All @@ -318,3 +310,4 @@ function sum(a, b) {
// write you code and test with examples
}
----
// end::array-sum[]
2 changes: 1 addition & 1 deletion book/content/preface.asc
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,4 @@ Measurement is the first step that leads to control and eventually to improvemen

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.

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`.
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`.
34 changes: 34 additions & 0 deletions book/interview-questions/rotate-array-left.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// tag::description[]
/**
* Rotate an array left by k number of times.
*
* @example
* rotateLeft([1,2,3], 1); // [2,3,1]
* rotateLeft([1,2,3,4,5], 4); // [5,1,2,3,4]
*
* rotateLeft(Array(1e6).fill(1), 1e4); //
*
* @param a - The array
* @param k - The number of times the array is rotated
*/
function rotateLeft(a, k) {
// end::description[]
// tag::solution[]
const moves = k % a.length;
for (let i = 0; i < moves; i++) {
a.push(a.shift());
}
return a;
}
// end::solution[]

// tag::bruteForce[]
function rotateLeftBruteForce(a, k) {
for (let i = 0; i < k; i++) {
a.push(a.shift());
}
return a;
}
// end::bruteForce[]

module.exports = { rotateLeft, rotateLeftBruteForce };
19 changes: 19 additions & 0 deletions book/interview-questions/rotate-array-left.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const { rotateLeft, rotateLeftBruteForce } = require('./rotate-array-left');

describe('Rotate Left', () => {
describe('when data is small', () => {
it('should work with 1', () => {
expect(rotateLeft([1, 2, 3], 1)).toEqual([2, 3, 1]);
});

it('should work with 4', () => {
expect(rotateLeft([1, 2, 3, 4, 5], 4)).toEqual([5, 1, 2, 3, 4]);
});
});

describe('when data is large', () => {
it('', () => {

});
});
});
15 changes: 0 additions & 15 deletions lab/exercises/01-arrays/rotate-array-left.js

This file was deleted.

4 changes: 4 additions & 0 deletions notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,7 @@ alert('foo');
console.log('bar');
/* eslint-enable no-alert */
```

# Asciidoctor Manual

https://asciidoctor.org/docs/user-manual/