Skip to main content

9. Palindrome Number

easyAsked at Goldman Sachs

Given an integer x, return true if it reads the same forward and backward. Goldman Sachs uses Palindrome Number to test whether you can solve it without converting to a string — the math-only solution is the actual grade.

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

Source citations

Public interview reports confirming this problem appears in Goldman Sachs loops.

  • Glassdoor (2026-Q1)Goldman Sachs SWE candidates report Palindrome Number with the explicit follow-up 'do it without string conversion'.
  • LeetCode Discuss (2025-11)Palindrome Number is in the top-15 of LeetCode's Goldman Sachs company tag.

Problem

Given an integer x, return true if x is a palindrome, and false otherwise. A palindrome reads the same forward and backward. Follow up: could you solve it without converting the integer to a string?

Constraints

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

Examples

Example 1

Input
x = 121
Output
true

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.

Example 3

Input
x = 10
Output
false

Approaches

1. String reversal

Convert to string and compare to its reverse.

Time
O(log|x|)
Space
O(log|x|)
function isPalStr(x) {
  const s = x.toString();
  return s === s.split('').reverse().join('');
}

Tradeoff: Trivial to write but defeats the actual point of the problem. Mention it then immediately pivot.

2. Reverse half the digits (optimal)

Negatives + multiples of 10 (except 0) are not palindromes. Otherwise, reverse only the last half of x and compare it to the first half.

Time
O(log|x|)
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);
  }
  return x === reversed || x === Math.floor(reversed / 10);
}

Tradeoff: Constant extra space and avoids the overflow risk of reversing the entire integer. The when-to-stop invariant (x <= reversed) is the part Goldman is grading.

Goldman Sachs-specific tips

Goldman Sachs interviewers almost always ask the 'without string conversion' follow-up. If you only know the string version you'll be downgraded even though it's technically correct. The half-reverse trick also gives you a clean answer to 'what if reversal overflows?' — since you only reverse half, the reversed integer fits inside the same 32-bit range.

Common mistakes

  • Forgetting the negative-number short-circuit (-121 is NOT a palindrome).
  • Reversing the entire number, which can overflow in 32-bit languages.
  • Forgetting to handle the odd-length case where reversed is one digit longer than x at the stop point.

Follow-up questions

An interviewer at Goldman Sachs may pivot to one of these next:

  • What if the integer can be 64-bit? (Same algorithm, different overflow bound.)
  • How would you check if a linked list is a palindrome? (Reverse the second half in place.)
  • Could you do it in O(1) extra space without any reversal at all? (Compare leading and trailing digits via div/mod with a power-of-10 mask.)

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 the negative-number check needed?

The minus sign breaks palindromicity at the textual level — -121 read backwards is 121- which isn't a number. The problem explicitly returns false for negatives.

Why x > reversed and not x >= reversed?

Strict inequality is the correct stop condition because at the midpoint of an even-length number, x and reversed are equal; at the midpoint of an odd-length number, the leading digit of reversed is the middle digit which doesn't need to match itself. Floor-divide reversed by 10 to drop that middle digit and compare.

Free learning resources

Curated free links for this problem.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →