Repository: vaskoz/dailycodingproblem-go (go)

Issue:
Day 69
Good morning! Here's your coding interview problem for today.

This problem was asked by Facebook.

Given a list of integers, return the largest product that can be made by multiplying any three integers.

For example, if the list is `[-10, -10, 5, 2]`, we should return `500`, since that's `-10 * -10 * 5`.

You can assume the list has at least three integers.

Pull request:
Day69
Resolves #147 .

Patch:
diff --git a/README.md b/README.md
index a723b30..36da826 100644
--- a/README.md
+++ b/README.md
@@ -78,3 +78,4 @@ problems from
 * [Day 66](https://github.com/vaskoz/dailycodingproblem-go/issues/141)
 * [Day 67](https://github.com/vaskoz/dailycodingproblem-go/issues/143)
 * [Day 68](https://github.com/vaskoz/dailycodingproblem-go/issues/145)
+* [Day 69](https://github.com/vaskoz/dailycodingproblem-go/issues/147)
diff --git a/day69/problem.go b/day69/problem.go
new file mode 100644
index 0000000..a1f15ed
--- /dev/null
+++ b/day69/problem.go
@@ -0,0 +1,45 @@
+package day69
+
+import "sort"
+
+// LargestProductOf3 returns the largest possible number produced by
+// multiplying any three integers.
+func LargestProductOf3(nums []int) int {
+	smallest := make([]int, 2)
+	largest := make([]int, 3)
+	copy(smallest, nums[:2])
+	copy(largest, nums[:3])
+	sort.Ints(smallest)
+	sort.Ints(largest)
+	if nums[2] < smallest[1] {
+		smallest[1] = nums[2]
+		if smallest[1] < smallest[0] {
+			smallest[0], smallest[1] = smallest[1], smallest[0]
+		}
+	}
+
+	for i := 3; i < len(nums); i++ {
+		num := nums[i]
+		if num < smallest[1] {
+			smallest[1] = num
+			if smallest[1] < smallest[0] {
+				smallest[0], smallest[1] = smallest[1], smallest[0]
+			}
+		}
+		if num > largest[0] {
+			largest[0] = num
+			for i := 1; i < len(largest); i++ {
+				if largest[i-1] > largest[i] {
+					largest[i-1], largest[i] = largest[i], largest[i-1]
+				}
+			}
+		}
+	}
+
+	optionA := smallest[0] * smallest[1] * largest[2]
+	optionB := largest[0] * largest[1] * largest[2]
+	if optionA > optionB {
+		return optionA
+	}
+	return optionB
+}


Test patch:
diff --git a/day69/problem_test.go b/day69/problem_test.go
new file mode 100644
index 0000000..be8bfb8
--- /dev/null
+++ b/day69/problem_test.go
@@ -0,0 +1,30 @@
+package day69
+
+import "testing"
+
+var testcases = []struct {
+	input    []int
+	expected int
+}{
+	{[]int{-10, -10, 5, 2}, 500},
+	{[]int{2, 5, -10, -10, 5, 2}, 500},
+	{[]int{2, 5, 10, 10, 6, 2}, 600},
+	{[]int{2, 5, -10, -20, 6, 2}, 1200},
+}
+
+func TestLargestProductOf3(t *testing.T) {
+	t.Parallel()
+	for _, tc := range testcases {
+		if result := LargestProductOf3(tc.input); result != tc.expected {
+			t.Errorf("Expected %v got %v", tc.expected, result)
+		}
+	}
+}
+
+func BenchmarkLargestProductOf3(b *testing.B) {
+	for i := 0; i < b.N; i++ {
+		for _, tc := range testcases {
+			LargestProductOf3(tc.input)
+		}
+	}
+}