Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit bcaf819

Browse files
feat(book/arrays): add exercises
1 parent 3f99c56 commit bcaf819

File tree

4 files changed

+77
-1
lines changed

4 files changed

+77
-1
lines changed

‎book/content/part02/array.asc

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ endif::[]
77
=== Array
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

@@ -274,3 +274,47 @@ To sum up, the time complexity of an array is:
274274
| splice ^| O(n) | Insert and remove from anywhere.
275275
|===
276276
//end::table
277+
278+
==== Array Exercises
279+
280+
1) Implement an efficient algorithm that rotate an array `a` an `k` number of times.
281+
282+
[source, javascript]
283+
----
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
298+
}
299+
----
300+
301+
302+
2) Implement an algorithm that takes two arrays of numbers and return a new array with the sum.
303+
304+
[source, javascript]
305+
----
306+
/**
307+
* Return the sum of two arrays as a new array.
308+
*
309+
* @example
310+
* sum([1,2,3], [1,1,1]); // [2,3,4]
311+
* sum([1], [9,9,9]); // [1,0,0,0]
312+
*
313+
* @param {number[]} a - Array of numbers.
314+
* @param {number[]} b - Array of numbers.
315+
* @returns {number[]} the sum array.
316+
*/
317+
function sum(a, b) {
318+
// write you code and test with examples
319+
}
320+
----

‎book/content/part02/linked-list.asc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,3 +285,7 @@ Use a doubly linked list when:
285285
* 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".
286286

287287
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.
288+
289+
==== Linked List Exercises
290+
291+
1) Merge two sorted lists into one (and keep them sorted)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Rotate an array left by k number of times.
3+
*
4+
* @example
5+
* rotateLeft([1,2,3], 1); // [2,3,1]
6+
* rotateLeft([1,2,3,4,5], 4); // [5,1,2,3,4]
7+
*
8+
* rotateLeft(Array(1e6).fill(1), 1e4); // <scale testing>
9+
*
10+
* @param a - The array
11+
* @param k - The number of times the array is rotated
12+
*/
13+
function rotateLeft(a, k) {
14+
// write you code and test with examples
15+
}

‎lab/exercises/01-arrays/sum-arrays.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* Return the sum of two arrays as a new array.
3+
*
4+
* @example
5+
* sum([1,2,3], [1,1,1]); // [2,3,4]
6+
* sum([1], [9,9,9]); // [1,0,0,0]
7+
*
8+
* @param {number[]} a - Array of numbers.
9+
* @param {number[]} b - Array of numbers.
10+
* @returns {number[]} the sum array.
11+
*/
12+
function sum(a, b) {
13+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /