Repository: vaskoz/dailycodingproblem-go (go)

Issue:
Day 324
Good morning! Here's your coding interview problem for today.

This problem was asked by Amazon.

Consider the following scenario: there are `N` mice and `N` holes placed at integer points along a line. Given this, find a method that maps mice to holes such that the largest number of steps any mouse takes is minimized.

Each move consists of moving one mouse one unit to the left or right, and only one mouse can fit inside each hole.

For example, suppose the mice are positioned at `[1, 4, 9, 15]`, and the holes are located at `[10, -5, 0, 16]`. In this case, the best pairing would require us to send the mouse at `1` to the hole at `-5`, so our function should return `6`.

Pull request description:
Day324
Resolves #658.

Patch:
diff --git a/README.md b/README.md
index f6b7e26..2970460 100644
--- a/README.md
+++ b/README.md
@@ -327,3 +327,4 @@ problems from
 * [Day 321](https://github.com/vaskoz/dailycodingproblem-go/issues/651)
 * [Day 322](https://github.com/vaskoz/dailycodingproblem-go/issues/654)
 * [Day 323](https://github.com/vaskoz/dailycodingproblem-go/issues/656)
+* [Day 324](https://github.com/vaskoz/dailycodingproblem-go/issues/658)
diff --git a/day324/problem.go b/day324/problem.go
new file mode 100644
index 0000000..d066114
--- /dev/null
+++ b/day324/problem.go
@@ -0,0 +1,27 @@
+package day324
+
+import "sort"
+
+// MostMouseSteps returns the most steps taken by the
+// mouse that needs to move the farthest.
+// Expects len(mice) == len(holes), otherwise panic.
+// Runs in O(N log N) time due to sorting both inputs.
+func MostMouseSteps(mice, holes []int) int {
+	if len(mice) != len(holes) {
+		panic("mice and holes lengths do not match")
+	}
+	sort.Ints(mice)
+	sort.Ints(holes)
+	n := len(mice)
+	var max int
+	for i := 0; i < n; i++ {
+		move := mice[i] - holes[i]
+		if move < 0 {
+			move = -move
+		}
+		if move > max {
+			max = move
+		}
+	}
+	return max
+}


Test patch:
diff --git a/day324/problem_test.go b/day324/problem_test.go
new file mode 100644
index 0000000..6cc8848
--- /dev/null
+++ b/day324/problem_test.go
@@ -0,0 +1,38 @@
+package day324
+
+import "testing"
+
+// nolint
+var testcases = []struct {
+	mice, holes []int
+	expected    int
+}{
+	{[]int{1, 4, 9, 15}, []int{10, -5, 0, 16}, 6},
+}
+
+func TestMostMouseSteps(t *testing.T) {
+	t.Parallel()
+	for _, tc := range testcases {
+		if result := MostMouseSteps(tc.mice, tc.holes); result != tc.expected {
+			t.Errorf("Expected %v, got %v", tc.expected, result)
+		}
+	}
+}
+
+func TestMostMouseStepsUnmatched(t *testing.T) {
+	t.Parallel()
+	defer func() {
+		if err := recover(); err == nil {
+			t.Errorf("Expected a panic from unmatched inputs")
+		}
+	}()
+	MostMouseSteps([]int{10}, []int{10, 20})
+}
+
+func BenchmarkMostMouseSteps(b *testing.B) {
+	for i := 0; i < b.N; i++ {
+		for _, tc := range testcases {
+			MostMouseSteps(tc.mice, tc.holes)
+		}
+	}
+}