Skip to main content

9. Palindrome Number

easyAsked at HubSpot

HubSpot asks Palindrome Number to see whether candidates look for math-based solutions before reaching for string conversion — a habit that signals strong algorithmic instincts beyond language-specific shortcuts.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in HubSpot loops.

  • Glassdoor (2026-Q1)Listed in HubSpot SWE interview reports as an occasional warm-up with a follow-up on avoiding string conversion.
  • r/cscareerquestions (2025-08)HubSpot candidates mention Palindrome Number as a secondary screen problem testing math intuition.

Problem

Given an integer x, return true if x is a palindrome, and false otherwise. An integer is a palindrome when it reads the same forward and backward.

Constraints

  • -2^31 <= x <= 2^31 - 1

Examples

Example 1

Input
x = 121
Output
true

Explanation: 121 reads as 121 from left to right and from right to left.

Example 2

Input
x = -121
Output
false

Explanation: From left to right it reads -121. From right to left it reads 121-. Not a palindrome.

Approaches

1. String conversion

Convert x to a string, then check if it equals its reverse. Simple and readable but uses O(d) extra space for the string, where d is the number of digits.

Time
O(d)
Space
O(d)
function isPalindrome(x) {
  if (x < 0) return false;
  const s = x.toString();
  return s === s.split('').reverse().join('');
}

Tradeoff: Acceptable as a first approach, but HubSpot interviewers often ask 'can you do it without converting to a string?' Pivot to the half-reversal math approach.

2. Reverse half the number (math)

Negative numbers and numbers ending in 0 (except 0 itself) are never palindromes. Reverse only the second half of the number by extracting digits. Compare the first half with the reversed second half.

Time
O(d)
Space
O(1)
function isPalindrome(x) {
  if (x < 0 || (x % 10 === 0 && x !== 0)) return false;
  let reversed = 0;
  while (x > reversed) {
    reversed = reversed * 10 + (x % 10);
    x = Math.floor(x / 10);
  }
  // For even length: x === reversed
  // For odd length: x === Math.floor(reversed / 10) (middle digit doesn't matter)
  return x === reversed || x === Math.floor(reversed / 10);
}

Tradeoff: O(1) space, no string allocation. The loop terminates at the midpoint, so it processes only half the digits. The odd-length check discards the middle digit by flooring reversed / 10.

HubSpot-specific tips

State both solutions and let the interviewer pick: 'The string approach is O(d) space. The math approach reverses only half the number in O(1) space — would you like that version?' HubSpot values clear trade-off articulation. Catch the edge cases upfront: negatives are always false, multiples of 10 (except 0) are always false because leading zeros aren't valid in integers.

Common mistakes

  • Not handling negative numbers — they can never be palindromes due to the leading minus sign.
  • Not handling numbers ending in 0 — x = 10 reversed is 01 = 1, which doesn't equal 10.
  • Reversing the full number and risking integer overflow in languages with fixed-width integers — reversing only half avoids this.
  • Off-by-one in the loop condition — the loop should stop when x <= reversed (half processed).

Follow-up questions

An interviewer at HubSpot may pivot to one of these next:

  • Palindrome Linked List (LC 234) — check if a linked list reads the same in both directions.
  • Valid Palindrome (LC 125) — check if a string is a palindrome ignoring non-alphanumeric characters.
  • What is the largest palindromic number formed by rearranging the digits of x?

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why is -121 not a palindrome?

The minus sign is part of the number's string representation but has no mirrored counterpart on the right side — so negative numbers never qualify.

Why do numbers ending in 0 fail (except 0 itself)?

A palindrome's first digit equals its last digit. For positive integers, no number starts with 0, so no positive multi-digit number ending in 0 can be a palindrome.

Does this work for x = 0?

Yes — the early return catches x % 10 === 0 && x !== 0, so 0 falls through to the loop. reversed stays 0, x reaches 0, and 0 === 0 returns true.

Practice these live with InterviewChamp.AI

Drill Palindrome Number and other HubSpot interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →