Skip to content

Commit

Permalink
feat(book/arrays): add exercises
Browse files Browse the repository at this point in the history
  • Loading branch information
amejiarosario committed Jul 28, 2020
1 parent 3f99c56 commit bcaf819
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 1 deletion.
46 changes: 45 additions & 1 deletion book/content/part02/array.asc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ endif::[]
=== Array
(((Array)))
(((Data Structures, Linear, Array)))
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.
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.

==== Array Basics

Expand Down Expand Up @@ -274,3 +274,47 @@ To sum up, the time complexity of an array is:
| splice ^| O(n) | Insert and remove from anywhere.
|===
//end::table

==== Array Exercises

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); // <scale testing>
*
* @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
}
----


2) Implement an algorithm that takes two arrays of numbers and return a new array with the sum.

[source, javascript]
----
/**
* Return the sum of two arrays as a new array.
*
* @example
* sum([1,2,3], [1,1,1]); // [2,3,4]
* sum([1], [9,9,9]); // [1,0,0,0]
*
* @param {number[]} a - Array of numbers.
* @param {number[]} b - Array of numbers.
* @returns {number[]} the sum array.
*/
function sum(a, b) {
// write you code and test with examples
}
----
4 changes: 4 additions & 0 deletions book/content/part02/linked-list.asc
Original file line number Diff line number Diff line change
Expand Up @@ -285,3 +285,7 @@ Use a doubly linked list when:
* You want to save some memory when dealing with possibly large data sets. Arrays pre-allocate a large chunk of contiguous memory on initialization. Lists are more “grow as you go”.

For the next two linear data structures <<part02-linear-data-structures#stack>> and <<part02-linear-data-structures#queue>>, we are going to use a doubly linked list to implement them. We could use an array as well, but since inserting/deleting from the start performs better with linked-lists, we are going use that.

==== Linked List Exercises

1) Merge two sorted lists into one (and keep them sorted)
15 changes: 15 additions & 0 deletions lab/exercises/01-arrays/rotate-array-left.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* 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); // <scale testing>
*
* @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
}
13 changes: 13 additions & 0 deletions lab/exercises/01-arrays/sum-arrays.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Return the sum of two arrays as a new array.
*
* @example
* sum([1,2,3], [1,1,1]); // [2,3,4]
* sum([1], [9,9,9]); // [1,0,0,0]
*
* @param {number[]} a - Array of numbers.
* @param {number[]} b - Array of numbers.
* @returns {number[]} the sum array.
*/
function sum(a, b) {
}

0 comments on commit bcaf819

Please sign in to comment.