The Palindrome challenge
The palindrome challenge is one of the most popular coding challenges in different coding exams at most software companies, especially at the big tech companies like Google, Microsoft, Facebook, Amazon, and more.
What is a palindrome?
The word/number is palindrome when it can be read from the right as from the left. For example, 121 is a palindrome, 1221 is also, but 10 is not, because when you read it from left it will be 1 then 0 but when u read it from right it will be 0 then 1 so it is not. At leetcode you can find the challenge in more detail.
Palindrome examples
Example 1:
Number: x = 121 Expected result: true Explanation: 121 reads as 121 from left to right and from right to left.
Example 2:
Number: x = 1221 Expected result: true Explanation: 1221 reads as 1221 from left to right and from right to left.
Example 3:
Number: x = 123 Expected result: false Explanation: 123 reads as 123 from left to right and reads 321 from right to left, so it is not.
Palindrome solutions
In the following section, I will illustrate some solutions for this challenge.
Solution 1
function isPalindrome(x: number): boolean { if (x < 0) { return false; } let allString = x.toString(); if (allString[allString.length - 1] == '0' && allString[0] != '0') { return false; } let mid: number = Math.floor(allString.length / 2); let index1 = 0; let index2 = allString.length - 1; for (let i = 0; i < mid; i++) { if (allString[index1] != allString[index2]) { return false; } index1 += 1; index2 -= 1; } return true; }; let num = 10; let isPalindromeNumber = isPalindrome(num); console.log(isPalindromeNumber);
Solution 2
function isPalindrome(x: number): boolean { if (x < 0) { return false; } let allString = x.toString(); if (allString[allString.length - 1] == '0' && allString[0] != '0') { return false; } let mid: number = Math.floor(allString.length / 2); // Odd if (allString.length % 2 == 1) { console.log(`odd, mid=${mid}`); for (let i = 0; i <= mid; i++) { let part1 = allString[mid - i]; let part2 = allString[mid + i]; if (part1 != part2) { return false; } } } // even else { mid = mid - 1; console.log(`even, mid=${mid}`); for (let i = 0; i <= mid; i++) { let part1 = allString[mid - i]; let part2 = allString[mid + i + 1]; if (part1 != part2) { return false; } } } return true; }; let num = 12345678876543210; let isPalindromeNumber = isPalindrome(num); console.log(isPalindromeNumber);
Solution 3, c#
In the above solutions, the number was converted to string but there are other solutions that can be used without converting the number to string as in the following section, please note this solution was suggested at leetcode site.
public class Solution { public bool IsPalindrome(int x) { // Special cases: // As discussed above, when x < 0, x is not a palindrome. // Also if the last digit of the number is 0, in order to be a palindrome, // the first digit of the number also needs to be 0. // Only 0 satisfy this property. if(x < 0 || (x % 10 == 0 && x != 0)) { return false; } int revertedNumber = 0; while(x > revertedNumber) { revertedNumber = revertedNumber * 10 + x % 10; x /= 10; } // When the length is an odd number, we can get rid of the middle digit by revertedNumber/10 // For example when the input is 12321, at the end of the while loop we get x = 12, revertedNumber = 123, // since the middle digit doesn't matter in palidrome(it will always equal to itself), we can simply get rid of it. return x == revertedNumber || x == revertedNumber/10; } }
Conclusion:
The palindrome challenge is one of the most well-known coding challenges in different software coding interviews. In this post, I introduced this challenge with examples and some solutions using typescript and c# language You can find sample source codes at github Enjoy…!!!
I can help you to build such as software tools/snippets, you contact me from here