Repository: vaskoz/dailycodingproblem-go (go)

Issue:
Day 4
Good morning! Here's your coding interview problem for today.

This problem was asked by Stripe.

Given an array of integers, find the first missing positive integer in linear time and constant space. In other words, find the lowest positive integer that does not exist in the array. The array can contain duplicates and negative numbers as well.

For example, the input [3, 4, -1, 1] should give 2. The input [1, 2, 0] should give 3.

You can modify the input array in-place.

Pull request:
day 4 solution
Resolves #5 

Patch:
diff --git a/day4/problem.go b/day4/problem.go
new file mode 100644
index 0000000..35efca4
--- /dev/null
+++ b/day4/problem.go
@@ -0,0 +1,56 @@
+package day4
+
+// PartitionBySign groups the negative values first, then zero values,
+// then positive values.
+// This operation happens in-place on the provided IntSlice.
+//
+// Returns the index to the first positive number.
+// Runs in O(N) time and O(1) space.
+func partitionBySign(nums []int) int {
+	left, right := 0, len(nums)-1
+	for left < right {
+		if nums[right] > 0 {
+			right--
+		} else if nums[left] <= 0 {
+			left++
+		} else {
+			nums[left], nums[right] = nums[right], nums[left]
+		}
+	}
+	for left < len(nums) && nums[left] <= 0 {
+		left++
+	}
+	return left
+}
+
+// findSmallestPositiveOnly works only on a slice of positive > 0 integers.
+// Runs in O(N) since it does two sequential linear scans.
+func findSmallestPositiveOnly(nums []int) int {
+	for i := range nums {
+		abs := nums[i]
+		if abs < 0 {
+			abs = -abs
+		}
+		if index := abs - 1; index < len(nums) {
+			if nums[index] > 0 {
+				nums[index] = -nums[index]
+			}
+		}
+	}
+
+	for i, v := range nums {
+		if v > 0 {
+			return i + 1
+		}
+	}
+
+	return len(nums) + 1
+}
+
+// FindSmallestMissingPositive returns the smallest positive number missing.
+// Running time is O(N) with O(1) space.
+// NOTE: this modifies the input array
+func FindSmallestMissingPositive(nums []int) int {
+	positiveStart := partitionBySign(nums)
+	return findSmallestPositiveOnly(nums[positiveStart:])
+}


Test patch:
diff --git a/day4/problem_test.go b/day4/problem_test.go
new file mode 100644
index 0000000..2cf5360
--- /dev/null
+++ b/day4/problem_test.go
@@ -0,0 +1,35 @@
+package day4
+
+import (
+	"testing"
+)
+
+var testcases = []struct {
+	input                   []int
+	smallestMissingPositive int
+}{
+	{[]int{-10, -20, -40, 1, 2, 3, 4, 5}, 6},
+	{[]int{3, 4, -1, 1}, 2},
+	{[]int{1, 2, 0}, 3},
+	{[]int{1, -1, 2, -2, 0, 100, -100}, 3},
+	{[]int{-1, -1, -2, -2, 0, -100, -100}, 1},
+	{[]int{1, 1, 2, 2, 0, 3, 4, 5, 6, 7, 8, 9, 9, 8, -100}, 10},
+	{[]int{100, 99, 100, -20, -30, -20}, 1},
+}
+
+func TestFindSmallestMissingPositive(t *testing.T) {
+	t.Parallel()
+	for _, tc := range testcases {
+		if result := FindSmallestMissingPositive(tc.input); result != tc.smallestMissingPositive {
+			t.Errorf("Input was %v. I expected %d, but received %d", tc.input, tc.smallestMissingPositive, result)
+		}
+	}
+}
+
+func BenchmarkFindSmallestMissingPositive(b *testing.B) {
+	for i := 0; i < b.N; i++ {
+		for _, tc := range testcases {
+			FindSmallestMissingPositive(tc.input)
+		}
+	}
+}