Algorithm LogbookAlgorithm Logbook

Palindrome Number Check

Basic Math

๐Ÿ“‹ Problem Statement

Given an integer, determine if it is a palindrome. An integer is a palindrome if it reads the same forward and backward. For example, 121 is a palindrome; -121 and 10 are not. Return True or False.

๐Ÿ’ก Intuition

Just like reversing a number, we extract digits one by one using modulo. We don't need to fully reverse the number โ€” we can compare the reversed number to the original. Negative numbers are never palindromes (due to the plus sign), and numbers ending in 0 (except 0 itself) are never palindromes.

๐Ÿ”ง Approach

  1. Return False immediately for negative numbers.
  2. Return False for numbers that end in 0 but aren't 0 themselves.
  3. Extract digits one at a time to build reversed_num.
  4. Compare reversed_num with the original n โ€” if equal, it's a palindrome.

โšก Complexity Analysis

Time

O(logโก10N)O(\log_{10} N) โ€” number of digits in NN

Space

O(1)O(1) โ€” only integer variables

โš ๏ธ Common Mistakes

  • Converting to a string (str(n) == str(n)[::-1]) โ€” valid but not a true mathematical solution
  • Not handling negative numbers: they have a - prefix so can never be palindromes
  • Not handling numbers ending in 0: 10 reversed is 01 = 1, not 10, so it's not a palindrome

๐ŸŽฏ Final Thoughts

This builds directly on Day 1 (Reverse Number). A palindrome check is a reversal check โ€” the reversed number equals the original. The early exits for negatives and trailing zeros make the solution more robust.