Skip to content

Commit 5167a18

Browse files
committed
JS solution and tests for IC #3
1 parent 8ac7bd9 commit 5167a18

File tree

4 files changed

+132
-0
lines changed

4 files changed

+132
-0
lines changed

3-Highest-Product-of-Three/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Highest Product of Three
2+
3+
## Problem Description
4+
5+
> Given an array of integers, find the highest product you can get from three of the integers.
6+
> The input `arrayOfInts` will always have at least three integers.
7+
8+
## Implementation
9+
10+
Given `n` integers in the array argument, my solution returns a result with time and space complexities of `O(n)` and `O(1)`, respectively.
11+
12+
- JavaScript
13+
- [Solution](./highestProductOf3.js)
14+
- [Jest Tests](./highestProductOf3.test.js)
15+
- [CodeSandbox](https://codesandbox.io/s/l4ny56913l?autoresize=1&fontsize=14&module=%2FhighestProductOf3.js&previewwindow=tests)
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/**
2+
* Calculates the highest product of three numbers.
3+
* @param {Number[]} arrayOfInts An array of integers with a minimum length of 3.
4+
* @return {Number} The highest product of three numbers.
5+
*/
6+
function highestProductOf3(arrayOfInts) {
7+
// Throw error if given array has fewer than 3 integers.
8+
if (arrayOfInts.length < 3)
9+
throw new Error("Minimum of three integers needed.");
10+
11+
// Track the greatest 3 positive values in descending order
12+
const highestPositives = [-Infinity, -Infinity, -Infinity];
13+
// Track the 2 lowest values in ascending order (for negative-to-positive product)
14+
const lowestNegatives = [Infinity, Infinity];
15+
16+
// Sweep through all given integers once.
17+
for (let i = 0; i < arrayOfInts.length; i++) {
18+
const currentInt = arrayOfInts[i];
19+
20+
// Check if current integer is one of the greatest 3 values seen thus far
21+
// Swap as needed (cascading swaps may occur).
22+
let temp = currentInt;
23+
let temp2 = temp;
24+
25+
// Largest value
26+
if (temp > highestPositives[0]) {
27+
temp2 = highestPositives[0];
28+
highestPositives[0] = temp;
29+
temp = temp2;
30+
}
31+
// 2nd Largest Value
32+
if (temp > highestPositives[1]) {
33+
temp2 = highestPositives[1];
34+
highestPositives[1] = temp;
35+
temp = temp2;
36+
}
37+
// 3rd Largest Value
38+
if (temp > highestPositives[2]) {
39+
temp2 = highestPositives[2];
40+
highestPositives[2] = temp;
41+
temp = temp2;
42+
}
43+
44+
// Check if current integer is one of the least 2 negative values seen thus far
45+
// Swap as needed (cascading swaps may occur).
46+
if (currentInt < 0) {
47+
temp = currentInt;
48+
temp2 = temp;
49+
// Least Negative Value
50+
if (temp < lowestNegatives[0]) {
51+
temp2 = lowestNegatives[0];
52+
lowestNegatives[0] = temp;
53+
temp = temp2;
54+
}
55+
// 2nd Least Negative Value
56+
if (temp < lowestNegatives[1]) {
57+
temp2 = lowestNegatives[1];
58+
lowestNegatives[1] = temp;
59+
temp = temp2;
60+
}
61+
}
62+
}
63+
// Calculate the product of the greatest 3 positive values,
64+
const highest3 =
65+
highestPositives[0] * highestPositives[1] * highestPositives[2];
66+
67+
// If at least two negative values were found
68+
if (lowestNegatives[0] !== Infinity && lowestNegatives[1] !== Infinity) {
69+
// Calculate the product of the greatest positive value with the 2 lowest negative values,
70+
// (Negative multiplied by a negative gives a positive)
71+
const highestWithLowest2 =
72+
highestPositives[0] * lowestNegatives[0] * lowestNegatives[1];
73+
// Return the greater result.
74+
return Math.max(highest3, highestWithLowest2);
75+
}
76+
77+
return highest3;
78+
}
79+
80+
export default highestProductOf3;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import highestProductOf3 from "./highestProductOf3";
2+
3+
describe(`highestProductOf3()`, () => {
4+
it(`finds highest product given a short array`, () => {
5+
expect(highestProductOf3([1, 2, 3, 4])).toBe(24);
6+
});
7+
8+
it(`finds highest product given a longer array`, () => {
9+
expect(highestProductOf3([6, 1, 3, 5, 7, 8, 2])).toBe(336);
10+
});
11+
12+
it(`finds highest product given a array has one negative`, () => {
13+
expect(highestProductOf3([-5, 4, 8, 2, 3])).toBe(96);
14+
});
15+
16+
it(`finds highest product given a array has two negatives`, () => {
17+
expect(highestProductOf3([-10, 1, 3, 2, -10])).toBe(300);
18+
});
19+
20+
it(`finds highest product given a array is all negatives`, () => {
21+
expect(highestProductOf3([-5, -1, -3, -2])).toBe(-6);
22+
});
23+
24+
it(`Throws an error given an empty array`, () => {
25+
expect(() => highestProductOf3([])).toThrow();
26+
});
27+
28+
it(`Throws an error given an array with one number`, () => {
29+
expect(() => highestProductOf3([1])).toThrow();
30+
});
31+
32+
it(`Throws an error given an array with two numbers`, () => {
33+
expect(() => highestProductOf3([1, 1])).toThrow();
34+
});
35+
});

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ The following problems are introduced in the [Interview Cake full course](https:
4242

4343
- [1. Apple Stocks](./1-Apple-Stocks/README.md)
4444

45+
- [3. Highest Product of Three](./3-Highest-Product-of-Three/README.md)
46+
4547
- [4. Merging Meeting Times](./4-Merging-Meeting-Times/README.md)
4648

4749
- [8. Balanced Binary Tree](./8-Balanced-Binary-Tree/README.md)

0 commit comments

Comments
 (0)