THN Interview Prep

7. Reverse Integer

At a Glance

  • Topic: math-geometry
  • Pattern: Math + overflow guard (canonical home for this problem)
  • Difficulty: Medium
  • Companies: Amazon, Apple, Bloomberg, Adobe, Microsoft
  • Frequency: High
  • LeetCode: 7

Problem (one-liner)

Reverse the decimal digits of a signed 32-bit integer; return 0 if the reversed value is outside the signed 32-bit range [-2³¹, 2³¹-1].

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

  • "Reverse integer"
  • Explicit overflow handling to zero
  • Digit pop/push pattern

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 — string reverse — needs parse overflow check anyway.
  • Optimal — iterative pop last digit, push to result — O(log |x|) time / O(1) space — check overflow before multiplying by 10.

Optimal Solution

Go

package main

const int32Max = 2147483647
const int32Min = -2147483648

func reverse(value int) int {
	result := 0
	for value != 0 {
		digit := value % 10
		value /= 10
		if result > int32Max/10 || (result == int32Max/10 && digit > 7) {
			return 0
		}
		if result < int32Min/10 || (result == int32Min/10 && digit < -8) {
			return 0
		}
		result = result*10 + digit
	}
	return result
}

JavaScript

const INT32_MAX = 2147483647;
const INT32_MIN = -2147483648;

function reverse(value) {
	let result = 0;
	while (value !== 0) {
		const digit = value % 10;
		value = Math.trunc(value / 10);
		if (
			result > Math.trunc(INT32_MAX / 10) ||
			(result === Math.trunc(INT32_MAX / 10) && digit > 7)
		) {
			return 0;
		}
		if (
			result < Math.trunc(INT32_MIN / 10) ||
			(result === Math.trunc(INT32_MIN / 10) && digit < -8)
		) {
			return 0;
		}
		result = result * 10 + digit;
	}
	return result;
}

Walkthrough

Input: value = 1534236469 (overflow case)

Each step checks whether result*10 + digit would exceed bounds before committing.

Edge Cases

  • Trailing zeros become leading zeros after reverse — dropped by integer semantics
  • value = 0
  • Min/max boundary reversals

Pitfalls

  • Using 64-bit accumulator without clamp — problem wants 0 on overflow
  • JS: use Math.trunc for division toward zero

Similar Problems

Variants

  • Reverse in arbitrary base.
  • Return clamped value instead of zero (different spec).

Mind-Map Tags

#overflow #digit-manipulation #math #int32-bounds

Mark this page when you finish learning it.

Last updated on

Spotted something unclear or wrong on this page?

On this page