Skip to content

Commit 5955ef8

Browse files
committed
Updated a number of solutions to ensure they're using strict mode, updated JSDocs or added them where missing. Reordered function declarations so solutions are at the tops of files and utility methods are later.
1 parent c188824 commit 5955ef8

File tree

11 files changed

+101
-50
lines changed

11 files changed

+101
-50
lines changed

src/chapter1/ch1-q9.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
'use strict';
22

3-
// Implementation of isSubstring function which is defined in question
4-
// can only be called once
5-
function isSubstring(str, substr) {
6-
return str.includes(substr);
7-
}
8-
93
/**
104
* Duplicate the rotated string, if the substring being searched is a different
115
* rotation of the string then it will be a substring of the new string. Both
@@ -28,3 +22,9 @@ export function isRotatedSubstring(str1, str2) {
2822
}
2923
return isSubstring(str1 + str1, str2);
3024
}
25+
26+
// Implementation of isSubstring function which is defined in question
27+
// can only be called once
28+
function isSubstring(str, substr) {
29+
return str.includes(substr);
30+
}

src/chapter2/ch2-q6.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,6 @@ export function isPalindromeStack(list) {
4343
return stack.length === 0;
4444
}
4545

46-
function reverse(node, end) {
47-
let prev = end,
48-
next;
49-
while (node) {
50-
next = node.next;
51-
node.next = prev;
52-
prev = node;
53-
node = next;
54-
}
55-
return prev;
56-
}
57-
5846
/**
5947
* First find out the length of the list, then walk to the middle of the list.
6048
* Once the middle is reached if the list had an odd length then skip the middle
@@ -105,3 +93,15 @@ export function isPalindromeReverse(list) {
10593

10694
return isPalindrome;
10795
}
96+
97+
function reverse(node, end) {
98+
let prev = end,
99+
next;
100+
while (node) {
101+
next = node.next;
102+
node.next = prev;
103+
prev = node;
104+
node = next;
105+
}
106+
return prev;
107+
}

src/chapter2/ch2-q7.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,6 @@
22

33
import { getLength } from './helpers';
44

5-
function skip(list, num) {
6-
while (num > 0) {
7-
list = list.next;
8-
--num;
9-
}
10-
return list;
11-
}
12-
135
/**
146
* Find out the length of the two lists first. If they intersect at some point
157
* then the length of their tails will be the same and any difference in length
@@ -41,3 +33,11 @@ export function doIntersect(list1, list2) {
4133

4234
return undefined;
4335
}
36+
37+
function skip(list, num) {
38+
while (num > 0) {
39+
list = list.next;
40+
--num;
41+
}
42+
return list;
43+
}

src/chapter2/helpers.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
'use strict';
12

23
export function linkedListToArray(list) {
34
let arr = [],

src/chapter3/ch3-q4.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
1+
'use strict';
12

3+
/**
4+
* Queues and Stacks have different orders for extracting items. To create a
5+
* queue with stacks we have two stacks, one for inserting items and one for
6+
* extracting them. When dequeuing an item if the extract stack is empty we
7+
* use queue operations to pop all the items off the insert stack onto the
8+
* extract stack which will now be in the right order for a queue.
9+
*
10+
* N = |MyQueue|
11+
* Time: enqueue O(1), dequeue O(N)
12+
* Additional space: O(N) - to hold the input items
13+
*/
214
export class MyQueue {
315
constructor() {
416
this.eStack = [];

src/chapter3/ch3-q5.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
'use strict';
22

3-
function peek(stack) {
4-
return stack[stack.length - 1];
5-
}
6-
7-
function isEmpty(stack) {
8-
return stack.length === 0;
9-
}
10-
113
/**
124
* Sort the stack by taking one item off the input stack at a time, find the
135
* right place within the processed items in the temp stack to insert it into.
@@ -42,3 +34,11 @@ export function sortStack(stack) {
4234

4335
return stack;
4436
}
37+
38+
function peek(stack) {
39+
return stack[stack.length - 1];
40+
}
41+
42+
function isEmpty(stack) {
43+
return stack.length === 0;
44+
}

src/chapter4/ch4-q01.js

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
'use strict';
22

3+
/**
4+
* One way to check if two nodes are connected is to do a BFS of the graph
5+
* from the source node. BFS would be useful where the nodes have many out
6+
* edges (degrees) and paths between pairs are not exceedingly deep as it will
7+
* visit neighbours from the source node radiating outwards.
8+
*
9+
* N = |vertices|
10+
* M = |edges|
11+
* Time: O(M)
12+
* Additional space: O(N)
13+
*/
314
export function isConnectedBFS(graph, source, target) {
415
let discovered = new Set(),
516
queue = [source];
@@ -20,6 +31,21 @@ export function isConnectedBFS(graph, source, target) {
2031
return false;
2132
}
2233

34+
/**
35+
* One way to check if two nodes are connected is to do a DFS of the graph
36+
* from the source node. DFS would be useful where the graph has really long
37+
* paths and we want to travel as far as we can through that graph as quickly as
38+
* possible. DFS can be recursive or use a stack and iteration.
39+
*
40+
* N = |vertices|
41+
* M = |edges|
42+
* Time: O(M)
43+
* Additional space: O(N)
44+
*/
45+
export function isConnectedDFS(graph, source, target) {
46+
return dfs(graph, new Set(), source, target);
47+
}
48+
2349
function dfs(graph, discovered, source, target) {
2450
if (source === target) {
2551
return true;
@@ -34,7 +60,3 @@ function dfs(graph, discovered, source, target) {
3460
}
3561
return false;
3662
}
37-
38-
export function isConnectedDFS(graph, source, target) {
39-
return dfs(graph, new Set(), source, target);
40-
}

src/chapter4/ch4-q02.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,6 @@
11
'use strict';
2-
import { Tree } from './helpers';
32

4-
function add(tree, values, start, end) {
5-
if (start === end) {
6-
tree.add(values[start]);
7-
}
8-
else if (start < end) {
9-
let mid = start + Math.floor((end - start) / 2);
10-
tree.add(values[mid]);
11-
add(tree, values, start, mid - 1);
12-
add(tree, values, mid + 1, end);
13-
}
14-
}
3+
import { Tree } from './helpers';
154

165
/**
176
* As the list is already sorted the best way to create a balanced tree is by
@@ -30,3 +19,15 @@ export function makeBalancedTree(values) {
3019
}
3120
return tree;
3221
}
22+
23+
function add(tree, values, start, end) {
24+
if (start === end) {
25+
tree.add(values[start]);
26+
}
27+
else if (start < end) {
28+
let mid = start + Math.floor((end - start) / 2);
29+
tree.add(values[mid]);
30+
add(tree, values, start, mid - 1);
31+
add(tree, values, mid + 1, end);
32+
}
33+
}

src/chapter4/ch4-q03.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
'use strict';
2+
23
import { LinkedList } from './helpers';
34

45
/**

src/chapter4/ch4-q05.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
'use strict';
22

3+
/**
4+
* To check if a tree is a valid BST we need to check that all the values under
5+
* a node are within the ranges defined by the path we took to get there. For
6+
* example, initially the root can have any value, every time we go down a left
7+
* child that sets an upper bound on the valid values of their children by that
8+
* nodes value. Every time you travel down the right child you lower bound the
9+
* valid values of their children by that nodes value.
10+
*
11+
* N = |tree|
12+
* Time: O(N)
13+
* Additional space: O(lg N) - due to recursion. Assumes a balanced tree, worst
14+
* case is O(N)
15+
*/
316
export function isValidBST(tree) {
417
if (!tree) {
518
throw new Error('invalid tree');

src/chapter4/helpers.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
'use strict';
12

23
class TreeNode {
34
constructor(value) {

0 commit comments

Comments
 (0)