Repository: vaskoz/dailycodingproblem-go (go)

Issue:
Day 202
Good morning! Here's your coding interview problem for today.

This problem was asked by Palantir.

Write a program that checks whether an integer is a palindrome. For example, 121 is a palindrome, as well as 888. 678 is not a palindrome. Do not convert the integer into a string.

Pull request description:
Day202
Resolves #417.

Patch:
diff --git a/README.md b/README.md
index 2974ac2..15ae13e 100644
--- a/README.md
+++ b/README.md
@@ -212,3 +212,4 @@ problems from
 * [Day 199](https://github.com/vaskoz/dailycodingproblem-go/issues/409)
 * [Day 200](https://github.com/vaskoz/dailycodingproblem-go/issues/411)
 * [Day 201](https://github.com/vaskoz/dailycodingproblem-go/issues/415)
+* [Day 202](https://github.com/vaskoz/dailycodingproblem-go/issues/417)
diff --git a/day202/problem.go b/day202/problem.go
new file mode 100644
index 0000000..ba7c5e5
--- /dev/null
+++ b/day202/problem.go
@@ -0,0 +1,38 @@
+package day202
+
+// IsIntegerPalindrome returns true if the integer is a
+// palindrome.
+// Runs in O(digits) time and O(digits) space.
+func IsIntegerPalindrome(i int) bool {
+	var digits []int
+	for i != 0 {
+		digits = append(digits, i%10)
+		i /= 10
+	}
+	for i := 0; i < len(digits)/2; i++ {
+		if digits[i] != digits[len(digits)-1-i] {
+			return false
+		}
+	}
+	return true
+}
+
+// IsIntegerPalindromeFaster returns true if the integer is a
+// palindrome.
+// Runs in O(digits) time and O(1) space.
+func IsIntegerPalindromeFaster(i int) bool {
+	divisor := 1
+	for temp := i; temp/divisor > 10; divisor = divisor * 10 {
+	}
+	for i >= 10 {
+		highest := i / divisor
+		lowest := i % 10
+		if highest != lowest {
+			return false
+		}
+		i %= divisor
+		i /= 10
+		divisor /= 100
+	}
+	return true
+}


Test patch:
diff --git a/day202/problem_test.go b/day202/problem_test.go
new file mode 100644
index 0000000..b2ed366
--- /dev/null
+++ b/day202/problem_test.go
@@ -0,0 +1,49 @@
+package day202
+
+import "testing"
+
+var testcases = []struct {
+	num      int
+	expected bool
+}{
+	{888, true},
+	{121, true},
+	{678, false},
+	{123124234323, false},
+	{123454321, true},
+	{23455432, true},
+}
+
+func TestIsIntegerPalindrome(t *testing.T) {
+	t.Parallel()
+	for _, tc := range testcases {
+		if result := IsIntegerPalindrome(tc.num); result != tc.expected {
+			t.Errorf("For input %v, expected %v, got %v", tc.num, tc.expected, result)
+		}
+	}
+}
+
+func BenchmarkIsIntegerPalindrome(b *testing.B) {
+	for i := 0; i < b.N; i++ {
+		for _, tc := range testcases {
+			IsIntegerPalindrome(tc.num)
+		}
+	}
+}
+
+func TestIsIntegerPalindromeFaster(t *testing.T) {
+	t.Parallel()
+	for _, tc := range testcases {
+		if result := IsIntegerPalindromeFaster(tc.num); result != tc.expected {
+			t.Errorf("For input %v, expected %v, got %v", tc.num, tc.expected, result)
+		}
+	}
+}
+
+func BenchmarkIsIntegerPalindromeFaster(b *testing.B) {
+	for i := 0; i < b.N; i++ {
+		for _, tc := range testcases {
+			IsIntegerPalindromeFaster(tc.num)
+		}
+	}
+}