Repository: vaskoz/dailycodingproblem-go (go)

Issue:
Day 214
Good morning! Here's your coding interview problem for today.

This problem was asked by Stripe.

Given an integer n, return the length of the longest consecutive run of 1s in its binary representation.

For example, given `156`, you should return `3`.

Pull request description:
Day214
Resolves #441.

Patch:
diff --git a/README.md b/README.md
index baaae24..e2c6bd5 100644
--- a/README.md
+++ b/README.md
@@ -224,3 +224,4 @@ problems from
 * [Day 211](https://github.com/vaskoz/dailycodingproblem-go/issues/434)
 * [Day 212](https://github.com/vaskoz/dailycodingproblem-go/issues/436)
 * [Day 213](https://github.com/vaskoz/dailycodingproblem-go/issues/439)
+* [Day 214](https://github.com/vaskoz/dailycodingproblem-go/issues/441)
diff --git a/day214/problem.go b/day214/problem.go
new file mode 100644
index 0000000..e6712e6
--- /dev/null
+++ b/day214/problem.go
@@ -0,0 +1,44 @@
+package day214
+
+import "strconv"
+
+// LengthLongestConsecutiveRunOnesBitShift calculates the longest
+// run of 1's using bit shifting.
+func LengthLongestConsecutiveRunOnesBitShift(n int) int {
+	var max, count int
+	for n != 0 {
+		if n&1 == 1 {
+			count++
+		} else {
+			if count > max {
+				max = count
+			}
+			count = 0
+		}
+		n >>= 1
+	}
+	if count > max {
+		return count
+	}
+	return max
+}
+
+// LengthLongestConsecutiveRunOnesString converts the number
+// into a binary representation string.
+func LengthLongestConsecutiveRunOnesString(n int) int {
+	var max, count int
+	for _, digit := range strconv.FormatInt(int64(n), 2) {
+		if digit == '1' {
+			count++
+		} else {
+			if count > max {
+				max = count
+			}
+			count = 0
+		}
+	}
+	if count > max {
+		return count
+	}
+	return max
+}


Test patch:
diff --git a/day214/problem_test.go b/day214/problem_test.go
new file mode 100644
index 0000000..34bb6e4
--- /dev/null
+++ b/day214/problem_test.go
@@ -0,0 +1,46 @@
+package day214
+
+import "testing"
+
+var testcases = []struct {
+	n                      int
+	longestConsecutiveOnes int
+}{
+	{156, 3},
+	{20432348234, 3},
+	{65535, 16},
+}
+
+func TestLengthLongestConsecutiveRunOnesBitShift(t *testing.T) {
+	t.Parallel()
+	for _, tc := range testcases {
+		if result := LengthLongestConsecutiveRunOnesBitShift(tc.n); result != tc.longestConsecutiveOnes {
+			t.Errorf("Given n=%d, expected %d, got %d", tc.n, tc.longestConsecutiveOnes, result)
+		}
+	}
+}
+
+func BenchmarkLengthLongestConsecutiveRunOnesBitShift(b *testing.B) {
+	for i := 0; i < b.N; i++ {
+		for _, tc := range testcases {
+			LengthLongestConsecutiveRunOnesBitShift(tc.n)
+		}
+	}
+}
+
+func TestLengthLongestConsecutiveRunOnesString(t *testing.T) {
+	t.Parallel()
+	for _, tc := range testcases {
+		if result := LengthLongestConsecutiveRunOnesString(tc.n); result != tc.longestConsecutiveOnes {
+			t.Errorf("Given n=%d, expected %d, got %d", tc.n, tc.longestConsecutiveOnes, result)
+		}
+	}
+}
+
+func BenchmarkLengthLongestConsecutiveRunOnesString(b *testing.B) {
+	for i := 0; i < b.N; i++ {
+		for _, tc := range testcases {
+			LengthLongestConsecutiveRunOnesString(tc.n)
+		}
+	}
+}