Skip to content

Commit a93ac1b

Browse files
committed
chore(lab): add exercises
1 parent 6af937f commit a93ac1b

15 files changed

+467
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// brute force: O(nd) | O(1)
2+
function rotLeft(a, d) {
3+
for (let i = 0; i < d; i++) { // O(d)
4+
a.push(a.shift()); // O(n), shift O(n)
5+
}
6+
return a;
7+
}
8+
9+
module.exports = rotLeft;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// modulus for rotations: O(n^2) | O(1)
2+
function rotLeft(a, d) {
3+
const len = a.length;
4+
const rot = d % len;
5+
for (let i = 0; i < rot; i++) { // O(n^2)
6+
a.push(a.shift()); // O(n)
7+
}
8+
return a;
9+
}
10+
11+
module.exports = rotLeft;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// additional space: O(n) | O(n)
2+
function rotLeft(a, d) {
3+
const len = a.length;
4+
const rot = d % len;
5+
const b = [];
6+
for (let i = 0; i < len; i++) { // O(n)
7+
b[i] = a[(rot + i) % len]; // O(1)
8+
}
9+
return b;
10+
}
11+
12+
module.exports = rotLeft;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*eslint-disable */
2+
// npx jest benchmarks/two-sum-implementations/two-sum.spec.js --watch --collectCoverage
3+
const implementations = [
4+
{ name: 1, fn: require('./01-array-rotation') },
5+
{ name: '1a', fn: require('./01a-array-rotation') },
6+
{ name: 2, fn: require('./02-array-rotation') },
7+
];
8+
9+
implementations.forEach(({name, fn}) => {
10+
describe(`Two Sum: ${name}`, () => {
11+
it('should work on worst case', () => {
12+
const rots = 1000;
13+
const array = [1, 2, 3];
14+
expect(fn(array, rots)).toEqual([2,3,1]);
15+
});
16+
});
17+
});

Diff for: benchmarks/two-sum-implementations/01a-two-sum.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Brute force: O(n^2) | O(1)
2+
function twoSum(nums, target) {
3+
for (let i = 0; i < nums.length - 1; i++) { // O(n^2)
4+
const diff = target - nums[i];
5+
const offset = i + 1;
6+
const idx = nums.slice(offset).findIndex((n) => n === diff); // O(n)
7+
const j = offset + idx;
8+
if (idx > -1) return [i, j];
9+
}
10+
return [];
11+
}
12+
13+
module.exports = twoSum;

Diff for: benchmarks/two-sum-implementations/02-two-sum.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// With a HashMap: O(n) | O(n)
1+
// [map w/dups handling] → O(n^2) | O(n)
22
function twoSum(nums, target) {
33
const map = nums.reduce((m, v, i) => { // O(n)
44
const ids = m.get(v) || [];
@@ -8,8 +8,9 @@ function twoSum(nums, target) {
88

99
for (let i = 0; i < nums.length; i++) { // O(n)
1010
const diff = target - nums[i];
11-
if (map.has(diff) && i !== map.get(diff)) {
12-
return [i, map.get(diff)];
11+
if (map.has(diff)) {
12+
const id = map.get(diff).find((j) => j > i);
13+
if (id > -1) return [i, id];
1314
}
1415
}
1516

Diff for: benchmarks/two-sum-implementations/02a-two-sum.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// [map w/dups handling] → O(n^2) | O(n)
2+
function twoSum(nums, target) {
3+
const map = mapify(nums);
4+
5+
for (let i = 0; i < nums.length; i++) { // O(n)
6+
const diff = target - nums[i];
7+
if (map.has(diff)) {
8+
const id = map.get(diff).find((j) => j > i);
9+
if (id > -1) return [i, id];
10+
}
11+
}
12+
13+
return [];
14+
}
15+
16+
function mapify(nums) {
17+
return nums.reduce((m, v, i) => { // O(n)
18+
const ids = m.get(v) || [];
19+
ids.push(i);
20+
return m.set(v, ids);
21+
}, new Map());
22+
}
23+
24+
module.exports = twoSum;

Diff for: benchmarks/two-sum-implementations/04-two-sum.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// sort + two pointers: O(n log n) | O(1)
2+
function twoSum(nums, target) {
3+
nums.sort((a, b) => a - b);
4+
5+
let lo = 0;
6+
let hi = nums.length - 1;
7+
8+
while (lo < hi) {
9+
const sum = nums[lo] + nums[hi];
10+
if (sum === target) {
11+
return [lo, hi];
12+
}
13+
14+
if (sum > target) {
15+
hi--;
16+
} else {
17+
lo++;
18+
}
19+
}
20+
return [];
21+
}
22+
23+
module.exports = twoSum;

Diff for: benchmarks/two-sum-implementations/two-sum.spec.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*eslint-disable */
2+
// npx jest benchmarks/two-sum-implementations/two-sum.spec.js --watch --collectCoverage
3+
const implementations = [
4+
{ name: 1, fn: require('./01-two-sum') },
5+
{ name: '1a', fn: require('./01a-two-sum') },
6+
{ name: 2, fn: require('./02-two-sum') },
7+
{ name: '2a', fn: require('./02a-two-sum') },
8+
{ name: 3, fn: require('./03-two-sum') },
9+
{ name: 4, fn: require('./04-two-sum') },
10+
];
11+
12+
implementations.forEach(({name, fn}) => {
13+
describe(`Two Sum: ${name}`, () => {
14+
xit('should work', () => {
15+
expect(fn([1, 2, 3].concat(Array(1e2 - 3).fill(7)), 4)).toEqual([0, 2]);
16+
});
17+
18+
it('should work on worst case', () => {
19+
const size = 100;
20+
expect(fn([...Array(size).fill(2), 3, 3 * size * 10], 3 * size * 10 + 3)).toEqual([size, size + 1]);
21+
});
22+
});
23+
});

Diff for: lab/exercises/10-mixed/check-prerequisite.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const checkIfPrerequisite = function (n, prerequisites, queries) {
2+
const graph = new Map();
3+
4+
Array(n).fill(0).forEach((v, i) => graph.set(i, {
5+
children: [],
6+
// connected: new Set(),
7+
}));
8+
9+
prerequisites.forEach(([u, v]) => {
10+
graph.get(u).children.push(v);
11+
// graph.get(u).connected.add(v);
12+
});
13+
14+
15+
return queries.map(([u, v]) => isConnected(graph, u, v, new Set(), u));
16+
};
17+
18+
function isConnected(graph, u, v, path = new Set(), p) {
19+
// console.log({u, v, path}, graph)
20+
// if (graph.get(u).connected.has(v)) return true;
21+
// path.forEach(s => graph.get(p).connected.add(s));
22+
23+
for (const child of graph.get(u).children) {
24+
if (child === v) return true;
25+
if (path.has(child)) continue;
26+
if (isConnected(graph, child, v, path.add(u), p)) return true;
27+
}
28+
29+
return false;
30+
}
31+
32+
33+
module.exports = checkIfPrerequisite;

Diff for: lab/exercises/10-mixed/check-prerequisite.spec.js

+140
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
const checkIfPrerequisite = require('./check-prerequisite');
2+
3+
describe('checkIfPrerequisite', () => {
4+
it('should work with 2', () => {
5+
const n = 2;
6+
const prerequisites = [[1, 0]];
7+
const queries = [
8+
[0, 1],
9+
[1, 0],
10+
];
11+
expect(checkIfPrerequisite(n, prerequisites, queries)).toEqual([
12+
false,
13+
true,
14+
]);
15+
});
16+
17+
it('example 4', () => {
18+
const n = 3;
19+
const prerequisites = [
20+
[1, 0],
21+
[2, 0],
22+
];
23+
const queries = [
24+
[0, 1],
25+
[2, 0],
26+
];
27+
expect(checkIfPrerequisite(n, prerequisites, queries)).toEqual([
28+
false,
29+
true,
30+
]);
31+
});
32+
33+
it('example 5', () => {
34+
const n = 5;
35+
const prerequisites = [
36+
[0, 1],
37+
[1, 2],
38+
[2, 3],
39+
[3, 4],
40+
];
41+
const queries = [
42+
[0, 4],
43+
[4, 0],
44+
[1, 3],
45+
[3, 0],
46+
];
47+
expect(checkIfPrerequisite(n, prerequisites, queries)).toEqual([
48+
true,
49+
false,
50+
true,
51+
false,
52+
]);
53+
});
54+
55+
it('no prereq', () => {
56+
const n = 2;
57+
const prerequisites = [];
58+
const queries = [
59+
[1, 0],
60+
[0, 1],
61+
];
62+
expect(checkIfPrerequisite(n, prerequisites, queries)).toEqual([
63+
false,
64+
false,
65+
]);
66+
});
67+
68+
it('example 3', () => {
69+
const n = 3;
70+
const prerequisites = [
71+
[1, 2],
72+
[1, 0],
73+
[2, 0],
74+
];
75+
const queries = [
76+
[1, 0],
77+
[1, 2],
78+
];
79+
expect(checkIfPrerequisite(n, prerequisites, queries)).toEqual([
80+
true,
81+
true,
82+
]);
83+
});
84+
85+
it('should work with inferred', () => {
86+
const n = 3;
87+
const prerequisites = [
88+
[0, 1],
89+
[1, 2],
90+
];
91+
const queries = [
92+
[0, 1],
93+
[1, 2],
94+
[0, 2],
95+
[2, 1],
96+
[2, 0],
97+
];
98+
expect(checkIfPrerequisite(n, prerequisites, queries)).toEqual([
99+
true,
100+
true,
101+
true,
102+
false,
103+
false,
104+
]);
105+
});
106+
107+
it('should work with 4', () => {
108+
const n = 4;
109+
const prerequisites = [
110+
[2, 3],
111+
[2, 1],
112+
[0, 3],
113+
[0, 1],
114+
];
115+
// https://bit.ly/2ApKE3L
116+
const queries = [
117+
[0, 1],
118+
[0, 3],
119+
[2, 3],
120+
[3, 0],
121+
[2, 0],
122+
[0, 2],
123+
];
124+
expect(checkIfPrerequisite(n, prerequisites, queries)).toEqual([
125+
true,
126+
true,
127+
true,
128+
false,
129+
false,
130+
false,
131+
]);
132+
});
133+
134+
it('stress testing', () => {
135+
const n = 33;
136+
const prerequisites = [[16,22],[16,15],[16,2],[16,3],[16,25],[16,21],[16,11],[16,23],[16,5],[16,13],[16,20],[16,29],[16,14],[16,9],[16,17],[16,6],[16,27],[16,8],[16,24],[16,1],[16,31],[16,7],[16,18],[16,26],[16,30],[16,28],[16,10],[16,12],[32,22],[32,15],[32,2],[32,25],[32,21],[32,11],[32,23],[32,5],[32,4],[32,20],[32,29],[32,14],[32,17],[32,19],[32,6],[32,27],[32,8],[32,24],[32,1],[32,31],[32,7],[32,18],[32,26],[32,30],[32,28],[32,10],[32,12],[32,0],[22,15],[22,2],[22,3],[22,21],[22,11],[22,23],[22,5],[22,4],[22,13],[22,20],[22,29],[22,9],[22,17],[22,19],[22,6],[22,8],[22,24],[22,31],[22,7],[22,18],[22,26],[22,30],[22,28],[22,10],[22,12],[22,0],[15,2],[15,3],[15,25],[15,21],[15,11],[15,23],[15,5],[15,4],[15,13],[15,29],[15,14],[15,9],[15,17],[15,19],[15,6],[15,27],[15,8],[15,24],[15,1],[15,31],[15,7],[15,18],[15,26],[15,30],[15,28],[15,10],[15,12],[15,0],[2,25],[2,21],[2,11],[2,23],[2,5],[2,4],[2,13],[2,29],[2,14],[2,9],[2,17],[2,19],[2,6],[2,27],[2,8],[2,24],[2,1],[2,31],[2,7],[2,18],[2,10],[2,12],[2,0],[3,25],[3,21],[3,11],[3,23],[3,5],[3,4],[3,13],[3,20],[3,29],[3,14],[3,9],[3,17],[3,6],[3,8],[3,1],[3,31],[3,7],[3,18],[3,26],[3,30],[3,10],[3,12],[3,0],[25,21],[25,11],[25,23],[25,5],[25,4],[25,20],[25,29],[25,14],[25,9],[25,17],[25,19],[25,6],[25,27],[25,8],[25,24],[25,1],[25,31],[25,7],[25,18],[25,26],[25,30],[25,28],[25,10],[25,12],[21,11],[21,23],[21,5],[21,4],[21,13],[21,20],[21,29],[21,14],[21,9],[21,19],[21,6],[21,27],[21,8],[21,24],[21,1],[21,31],[21,7],[21,18],[21,26],[21,30],[21,28],[21,10],[21,12],[21,0],[11,23],[11,5],[11,4],[11,13],[11,20],[11,29],[11,14],[11,9],[11,17],[11,19],[11,6],[11,27],[11,8],[11,24],[11,1],[11,31],[11,7],[11,18],[11,26],[11,30],[11,28],[11,10],[11,12],[11,0],[23,5],[23,4],[23,13],[23,20],[23,29],[23,14],[23,9],[23,17],[23,6],[23,8],[23,24],[23,1],[23,7],[23,18],[23,28],[23,10],[23,12],[5,13],[5,20],[5,9],[5,17],[5,6],[5,27],[5,24],[5,1],[5,31],[5,7],[5,18],[5,26],[5,30],[5,10],[5,12],[5,0],[4,13],[4,20],[4,29],[4,14],[4,17],[4,19],[4,6],[4,8],[4,24],[4,1],[4,31],[4,7],[4,18],[4,26],[4,30],[4,28],[4,10],[4,12],[4,0],[13,20],[13,14],[13,9],[13,17],[13,19],[13,8],[13,24],[13,1],[13,31],[13,7],[13,18],[13,26],[13,30],[13,28],[13,10],[13,12],[13,0],[20,29],[20,14],[20,9],[20,17],[20,19],[20,6],[20,27],[20,1],[20,31],[20,26],[20,30],[20,28],[20,12],[20,0],[29,14],[29,9],[29,17],[29,19],[29,6],[29,27],[29,8],[29,24],[29,1],[29,31],[29,7],[29,18],[29,30],[29,28],[29,10],[29,12],[29,0],[14,9],[14,17],[14,19],[14,6],[14,27],[14,24],[14,1],[14,31],[14,7],[14,18],[14,26],[14,30],[14,28],[14,12],[14,0],[9,17],[9,19],[9,6],[9,27],[9,8],[9,24],[9,1],[9,31],[9,7],[9,18],[9,26],[9,30],[9,28],[9,12],[9,0],[17,19],[17,6],[17,27],[17,8],[17,24],[17,1],[17,7],[17,18],[17,26],[17,30],[17,28],[17,10],[17,12],[17,0],[19,6],[19,8],[19,24],[19,1],[19,31],[19,7],[19,18],[19,26],[19,30],[19,28],[19,10],[19,0],[6,27],[6,8],[6,24],[6,1],[6,7],[6,26],[6,30],[6,28],[6,10],[6,12],[6,0],[27,8],[27,24],[27,1],[27,7],[27,26],[27,30],[27,28],[27,10],[27,12],[27,0],[8,24],[8,1],[8,31],[8,18],[8,26],[8,28],[8,12],[8,0],[24,1],[24,31],[24,7],[24,18],[24,26],[24,30],[24,28],[24,12],[24,0],[1,31],[1,7],[1,18],[1,26],[1,30],[1,28],[1,10],[1,12],[1,0],[31,7],[31,18],[31,26],[31,30],[31,28],[31,10],[31,12],[31,0],[7,18],[7,26],[7,30],[7,28],[7,10],[18,26],[18,28],[18,10],[26,30],[26,28],[26,10],[26,0],[30,28],[30,10],[30,12],[30,0],[28,12],[28,0],[10,0],[12,0]];
137+
const queries = [[6,17],[31,23],[32,18],[31,18],[23,8],[30,20],[13,16],[25,31],[30,22],[17,0],[10,26],[32,20],[10,28],[31,32],[32,21],[24,27],[17,10],[27,3],[24,16],[31,29],[6,31],[23,22],[24,13],[7,21],[14,28],[28,30],[28,32],[16,7],[7,32],[10,24],[5,28],[20,32],[27,31],[31,32],[30,32],[4,10],[18,32],[18,22],[10,25],[32,22],[0,32],[32,27],[32,25],[18,12],[24,23],[30,31],[28,18],[18,26],[18,20],[30,16],[7,19],[29,31],[13,10],[18,7],[14,2],[32,30],[14,1],[27,29],[13,22],[25,1],[25,23],[8,29],[16,20],[10,24],[4,20],[0,13],[25,14],[29,27],[12,28],[24,31],[32,26],[29,3],[27,23],[22,31],[21,15],[29,11],[30,26],[18,25],[22,29],[10,26],[20,12],[6,1],[30,7],[4,31],[13,16],[30,18],[13,5],[16,25],[23,28],[28,25],[30,10],[16,21],[32,5],[19,16],[30,29],[2,16],[18,21],[20,24],[24,23],[13,7],[28,5],[32,31],[17,24],[6,29],[24,25],[21,24],[24,22],[26,29],[20,5],[22,29],[19,9],[16,21],[21,18],[2,11],[14,0],[14,7],[17,23],[31,1],[3,9],[22,4],[30,21],[32,11],[31,28],[26,29],[19,0],[22,13],[4,17],[1,2],[29,24],[14,15],[19,0],[6,25],[14,27],[20,31],[25,21],[32,31],[31,27],[6,14],[32,30],[22,20],[27,20],[23,22],[28,31],[15,26],[26,14],[28,7],[29,30],[32,28],[15,26],[14,27],[32,31],[6,23],[8,27],[5,7],[21,18],[14,11],[32,28],[29,8],[24,32],[14,28],[19,21],[15,13],[3,20],[30,27],[22,19],[32,29],[6,10],[26,30],[29,4],[16,3],[31,8],[20,24],[30,8],[17,28],[13,14],[4,10],[12,22],[26,3],[30,28],[3,8],[11,30],[20,4],[20,14],[15,27],[15,23],[27,25],[4,20],[7,31],[24,22],[28,29],[21,31],[32,11],[19,10],[30,25],[8,24],[26,17],[9,26],[17,14],[29,24],[30,31],[29,22],[10,21],[21,19],[25,28],[15,28],[32,29],[23,1],[26,29],[17,31],[25,21],[15,8],[29,28],[20,24],[19,24],[23,22],[2,15],[17,31],[31,32],[23,21],[27,31],[28,24],[2,21],[4,26],[27,20],[14,25],[4,3],[21,22],[28,22],[16,26],[12,22],[27,9],[8,30],[24,30],[23,28],[14,3],[1,7],[32,31],[23,29],[23,29],[11,20],[30,27],[20,7],[31,27],[2,30],[6,11],[18,10],[13,31],[19,31],[24,9],[7,10],[27,31],[3,4],[24,2],[8,21],[13,27],[23,20],[26,25],[18,30],[26,23],[20,29],[32,27],[29,14],[29,27],[6,27],[17,0],[29,30],[31,28],[24,29],[23,0],[31,23],[23,26],[21,14],[16,7],[28,12],[3,7],[3,30],[31,29],[15,0],[13,6],[32,12],[28,26],[27,30],[23,29],[5,13],[29,30],[14,22],[9,22],[16,13],[31,28],[30,13],[14,21],[32,31],[10,21],[27,24],[11,4],[13,1],[21,24],[31,32],[12,3],[29,32],[15,14],[9,26],[25,21],[32,31],[11,23],[18,11],[18,21],[24,1],[31,18],[26,20],[19,6],[25,10],[21,28],[23,32],[28,30],[31,30],[6,16],[19,29],[13,23],[31,30],[29,25],[32,19],[26,23],[2,3],[22,24],[32,29],[24,27],[30,8],[31,30],[24,13],[31,32],[26,30],[27,10],[26,5],[10,22],[27,32],[17,14],[26,18],[32,31],[10,5],[0,19],[28,30],[13,7],[9,28],[20,19],[4,23],[30,31],[26,21],[25,24],[23,22],[26,29],[22,17],[18,20],[18,31],[11,16],[0,23],[9,6],[17,19],[1,4],[18,27],[20,30],[16,19],[25,23],[32,29],[32,30],[18,3],[2,21],[0,5],[18,16],[11,1],[28,16],[31,10],[32,14],[13,32],[20,22],[25,23],[14,21],[14,26],[14,3],[18,5],[28,24],[4,19],[2,16],[24,16],[21,19],[32,25],[30,23],[12,8],[11,17],[13,5],[23,16],[28,22],[10,29],[10,15],[29,6],[18,10],[26,30],[31,21],[23,2],[27,20],[32,17],[1,16],[32,30],[22,12],[17,0],[4,6],[31,29],[30,32],[18,28],[32,28],[16,28],[19,25],[31,32],[25,31],[19,21],[12,9],[18,32],[17,10],[21,9],[0,16],[16,23],[10,30],[31,19],[30,19],[26,12],[24,25],[17,32],[23,22],[6,23],[14,28],[5,14],[25,29],[28,24],[8,3],[30,19],[26,7],[11,17],[32,29],[26,28],[14,23],[30,25],[25,31],[31,4],[11,9],[30,19],[19,2],[11,9],[21,28],[13,10],[19,6],[1,19],[24,15],[23,25],[16,30],[9,26],[28,0],[21,24],[19,3],[26,6],[11,21],[17,21],[16,21],[1,31],[15,16],[31,32],[30,8],[6,7],[23,14],[19,22],[31,32],[23,20],[17,30],[4,3],[17,1],[32,25],[10,31],[11,12],[26,28],[25,28],[31,23],[18,16],[16,14],[2,8],[1,14],[10,19],[26,8],[16,7],[25,18],[16,10],[19,27],[30,31],[29,21],[29,28],[17,30],[10,24],[16,0],[32,31],[32,31],[8,1],[23,11],[25,28],[20,14],[16,10],[10,0],[22,32],[15,9],[19,14],[27,25],[27,25],[31,32],[13,30],[9,31],[15,26],[15,28],[0,25],[30,22],[7,24],[10,28],[22,23],[16,28],[8,9],[31,17],[19,25],[23,6],[29,32],[4,5],[21,5],[27,23],[31,30],[29,30],[24,25],[20,30],[27,18],[21,9],[32,30],[23,5],[17,5],[20,18],[19,12],[32,28],[21,17],[32,20],[26,13],[28,25],[31,32],[19,29],[29,14],[24,15],[30,4],[14,20],[27,22],[4,16],[9,22],[18,5],[31,13],[26,29],[30,32],[27,30],[0,8],[17,1],[30,32],[18,19],[25,22],[23,17],[24,26],[8,10],[20,23],[32,31],[32,20],[22,9],[0,15],[19,18],[32,29],[22,1],[19,22],[13,23],[31,32],[20,3],[29,8],[27,29],[7,17],[16,20],[24,4],[30,32],[28,12],[32,31],[31,30],[6,18],[32,30],[11,6],[28,29],[21,31],[2,21],[9,30],[31,32],[13,21],[31,14],[22,30],[32,26],[11,30],[9,19],[10,20],[29,10],[1,20],[25,30],[13,20],[15,16],[27,28],[29,22],[13,3],[7,17],[12,14],[32,31],[18,15],[31,28],[25,31],[30,31],[29,2],[26,19],[31,24],[22,3],[32,31],[23,28],[2,24],[4,8],[19,5],[16,29],[7,13],[10,28],[12,9],[23,28],[14,27],[31,24],[14,3],[26,31],[23,32],[14,31],[14,18],[0,10],[23,13],[25,24],[17,7],[17,11],[19,2],[32,16],[9,15],[25,17],[12,3],[32,26],[12,20],[29,11],[29,32],[5,7],[23,25],[27,22],[31,32],[30,22],[30,31],[9,10],[28,29],[5,3],[8,24],[28,32],[14,22],[7,28],[30,20],[20,9],[32,31],[23,31],[31,29],[32,30],[24,32],[1,2],[20,4],[1,21],[31,15],[26,27],[10,24],[1,25],[23,20],[20,22],[9,29],[25,24],[17,9],[17,9],[13,32],[21,20],[29,20],[19,7],[28,30],[12,28],[21,28],[12,16],[14,11],[6,27],[31,23],[10,15],[28,7],[0,5],[19,17],[25,11],[7,16],[32,30],[32,30],[12,31],[11,12],[31,28],[27,32],[14,1],[17,5],[15,25],[8,19],[29,31],[14,27],[18,20],[7,3],[9,6],[20,24],[9,27],[1,11],[20,4],[12,30],[21,7],[28,3],[32,30],[11,0],[29,27],[31,32],[11,9],[24,0],[24,14],[21,3],[13,30],[25,32],[21,24],[7,8],[19,22],[21,20],[11,4],[5,4],[9,17],[31,26],[26,18],[25,21],[19,17],[7,4],[5,17],[2,4],[20,16],[13,20],[31,32],[29,24],[3,24],[10,23],[9,16],[18,17],[21,26],[27,29],[31,32],[24,2],[15,26],[1,8],[14,20],[31,32],[21,14],[32,23],[31,25],[31,17],[15,13],[22,32],[27,21],[32,10],[26,28],[27,29],[12,29],[6,16],[14,19],[17,21],[23,27],[1,12],[14,26],[22,21],[0,12],[10,9],[13,18],[20,23],[27,28],[11,15],[32,26],[23,4],[27,32],[19,10],[20,24],[9,12],[1,12],[16,12],[6,14],[11,21],[6,4],[11,22],[31,27],[21,20],[17,30],[31,32],[3,17],[27,22],[21,28],[25,5],[29,27],[31,18],[31,30],[29,27],[12,21],[29,10],[3,18],[21,31],[8,15],[16,22],[16,21],[32,31],[31,8],[5,13],[11,19],[23,4],[23,10],[5,26],[25,7],[8,27],[32,9],[30,28],[13,23],[32,28],[16,21],[31,32],[25,16],[32,28],[23,26],[11,22],[20,21],[28,12],[15,12],[14,13],[5,18],[15,14],[7,27],[11,0],[15,18],[32,17],[21,7],[27,26],[27,12],[27,32],[31,32],[19,30],[26,28],[32,31],[31,9],[30,26],[30,28],[27,24],[31,32],[14,20],[22,5],[5,26],[24,28],[29,30],[15,31],[11,30],[32,26],[31,13],[14,27],[32,18],[19,0],[28,24],[26,32],[17,15],[28,8],[26,10],[26,24],[21,10],[1,31],[14,29],[32,29],[9,16],[31,28],[25,31],[22,29],[32,22],[4,0],[26,5],[30,32],[8,24],[30,17],[32,30],[21,16],[17,24],[30,26],[18,32],[27,26],[16,31],[25,7],[30,18],[0,25],[32,31],[4,26],[31,27],[20,32],[9,24],[1,31],[22,30],[27,30],[23,32],[29,31],[31,30],[30,19],[6,5],[31,30],[22,1],[29,27],[24,31],[23,22],[15,20],[21,15],[8,14],[1,10],[32,23],[27,14],[15,30],[30,32],[28,30],[9,16],[20,14],[20,21],[32,25],[28,20],[29,16],[7,1],[7,28],[10,12],[20,27],[24,26],[6,26],[23,17],[20,6],[14,7],[30,15],[32,27],[30,27],[27,30],[24,1],[31,30],[30,28],[27,22],[22,27],[14,13],[28,17],[31,30],[23,29],[17,24],[29,32],[19,21],[29,21],[25,28],[16,12],[30,26],[10,19],[30,0],[20,12],[3,9],[7,32],[12,21],[24,8]];
138+
checkIfPrerequisite(n, prerequisites, queries);
139+
});
140+
});

Diff for: lab/exercises/10-mixed/linked-list-data-equality.js

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* Given two linked list, determine if they have the same data
3+
4+
**Input:**
5+
L1: he → ll → o
6+
7+
L2: hel → l → o
8+
9+
**Output:**
10+
11+
True
12+
13+
class Node {
14+
constructor(value) {
15+
this.value = value;
16+
this.next = null;
17+
this.previous = null;
18+
}
19+
}
20+
21+
* @param {Node} l1 - The list 1.
22+
* @param {Node} l2 - The list 2.
23+
*/
24+
function hasSameData(l1, l2) {
25+
if (l1 !== l2 && (!l1 || !l2)) return false;
26+
let p1 = l1;
27+
let p2 = l2;
28+
let i1 = 0;
29+
let i2 = 0;
30+
31+
while (p1 && p2) {
32+
// console.log({v1: p1.value[i1], v2: p2.value[i2], i1, i2})
33+
if (p1.value[i1] !== p2.value[i2]) return false;
34+
i1++;
35+
i2++;
36+
if (i1 >= p1.value.length) {
37+
p1 = p1.next;
38+
i1 = 0;
39+
}
40+
if (i2 >= p2.value.length) {
41+
p2 = p2.next;
42+
i2 = 0;
43+
}
44+
}
45+
46+
return true;
47+
}
48+
49+
module.exports = hasSameData;

0 commit comments

Comments
 (0)