THN Interview Prep

9. Palindrome Number

At a Glance

  • Topic: math-geometry
  • Pattern: Reverse half (integer / math)
  • Difficulty: Easy
  • Companies: Amazon, Microsoft, Apple, Adobe, Bloomberg
  • Frequency: High
  • LeetCode: 9

Problem (one-liner)

Return whether a signed 32-bit integer reads the same forward and backward, without converting the entire number to a string (follow problem constraints as stated).

Core Basics

  • Opening move: classify the object (interval, multiset, trie node, bitmask, overlapping sub-intervals…) and whether the answer lives in an enumeration, ordering, partition, graph walk, DP table, etc.
  • Contracts: spell what a valid partial solution looks like at each step—the thing you inductively preserve.
  • Complexity anchors: state the brute upper bound and the structural fact (sorting, monotonicity, optimal substructure, greedy exchange, hashability) that should beat it.

Understanding

  • Why brute hurts: name the repeated work or state explosion in one sentence.
  • Why optimal is safe: the invariant / exchange argument / optimal-subproblem story you would tell a staff engineer.

Memory Hooks

  • One chant / rule: a single memorable handle (acronym, operator trick, or shape) you can recall under time pressure.

Recognition Cues

  • "Palindrome" on integer
  • Avoid extra string storage — reverse half or compare digit stacks

Study Pattern (SDE3+)

  • 0–3 min: restate I/O and brittle edges aloud, then verbalize two approaches with time/space before you touch the keyboard.
  • Implementation pass: one-line invariant above the tight loop; state what progress you make each iteration.
  • 5 min extension: pick one constraint twist (immutable input, stream, bounded memory, parallel readers) and explain what in your solution breaks without a refactor.

Diagram

At-a-glance flow (replace with problem-specific Mermaid as you refine this note). camelCase node IDs; no spaces in IDs.

Loading diagram…

Approaches

  • Brute force — stringify and two pointers — O(log n) time / O(log n) space.
  • Optimal — reverse the second half of digits — O(log n) time / O(1) space — stop at middle.

Optimal Solution

Go

package main

func isPalindrome(value int) bool {
	if value < 0 || (value%10 == 0 && value != 0) {
		return false
	}
	reversedHalf := 0
	for value > reversedHalf {
		reversedHalf = reversedHalf*10 + value%10
		value /= 10
	}
	return value == reversedHalf || value == reversedHalf/10
}

JavaScript

function isPalindrome(value) {
	if (value < 0 || (value % 10 === 0 && value !== 0)) {
		return false;
	}
	let reversedHalf = 0;
	while (value > reversedHalf) {
		reversedHalf = reversedHalf * 10 + (value % 10);
		value = Math.trunc(value / 10);
	}
	return value === reversedHalf || value === Math.trunc(reversedHalf / 10);
}

Walkthrough

Input: value = 1221

phasevaluereversedHalfnote
iter1221 → 122 → 121 → 12peel digits
end1212equal halves

Odd length: when value == reversedHalf/10 after middle digit folded into reversedHalf.

Edge Cases

  • Negative numbers — not palindrome per definition
  • Trailing zeros on positive numbers — 10, 100 fail fast
  • Single digit — true

Pitfalls

  • Full reversal can overflow on edge inputs — half reversal avoids that
  • Confusing odd vs even digit count termination

Similar Problems

Variants

  • Base base palindrome check.
  • Next palindrome generation.

Mind-Map Tags

#palindrome #reverse-half #math #no-extra-space

Mark this page when you finish learning it.

Last updated on

Spotted something unclear or wrong on this page?

On this page