Sitemap

Easy Coding Problem: Palindrome Number

2 min readSep 18, 2026

--

Press enter or click to view image in full size

The problem is to check whether a number is a palindrome and provide binary output accordingly. A palindrome is a string that is read the same from left to right and right to left.

This problem can be solved in two ways: first, convert the integer to a string and compare it with its reverse. The second way is to reverse the number by dividing it by 10 in a loop and storing the number in a temp variable.

Get Nitin Kumar’s stories in your inbox

Join Medium for free to get updates from this writer.

Example:

  • Input: 121
  • Answer: True/1

String Comparison

Python has asimple way of reversing the number by using [::-1]. This is different from[:-1], as this will read the string from the first to second last characters. [::-1] uses empty start, empty stop, step -1 while [:-1] uses empty start, stop -1, step 1.

class palindromeNumber:
# Time complexity: O(log10(x)); Space complexity: O(log10(x))
def isPalindromeString(self, x: int) -> bool:
# Convert the number to a string.
number = str(x)
# Compare the string with its reversed version.
return number == number[::-1]


# Run the following code only when this file is executed directly.
if __name__ == "__main__":
# Read a number from the user.
x = int(input("Enter a number: "))

# Display the result from the string method.
print(palindromeNumber().isPalindromeString(x))

Integer Comparision

Python has a inbuild modulas operater which is %. So 10/3 will give you 3.333… as the result. But 10 % 3 will give you 1 as a remainder.

Apart from modulus, Python also has a floor division operator, which will give the quotient of the equation and is written as two slashes, like //. So 10//3 will give only 3. Kindly note that this operator only provides an integer result.

class palindromeNumber:
# Time complexity: O(log10(x)); Space complexity: O(1)
def isPalindromeInt(self, x: int) -> bool:
# Reject negative numbers.
if x < 0:
# A negative number is not a palindrome.
return False
# Handle zero as a special case.
if x == 0:
# Zero is a palindrome.
return True
# Save the original number for comparison.
original = x
# Start with an empty reversed number.
reversed_number = 0
# Continue while digits remain in x.
while x:
# Remove and retrieve the last digit.
digit = x % 10
x //= 10
# Append the digit to the reversed number.
reversed_number = reversed_number * 10 + digit

# Return whether the number equals its reverse.
return original == reversed_number


# Run the following code only when this file is executed directly.
if __name__ == "__main__":
# Read a number from the user.
x = int(input("Enter a number: "))
# Display the result from the integer method.
print(palindromeNumber().isPalindromeInt(x))
# Display the result from the string method.
print(palindromeNumber().isPalindromeString(x))
# Display the result from the half-integer method.
print(palindromeNumber().isPalindromeHalfInt(x))

--

--