00:00

Problem Statement

Input/Output Examples

Visual Explanation

The loop stops at the middle of the number, so it does half the work of a full reversal, and the reversed half always fits in an int.

Approaches

Both approaches reverse digits arithmetically. One stops halfway; the other reverses everything in a long.

Java Solutions

Each tab is a complete program that runs on Java 17 or later. Both programs print the same output for the same inputs.

Complexity Analysis

Here d is the number of digits, at most 10 for an int. The half method does about d / 2 steps.

Interview Tips and Recommended Approach

  • Mention overflow. Reversing 1999999999 in an int overflows; reversing half cannot.
  • Handle trailing zeros before the loop.
  • Explain the odd-length case and why / 10 removes the middle digit.
  • Know the string version, but say why the arithmetic one is preferred: no allocation.

Recommended Approach: Reverse Half of the Digits

Why: it needs no string, cannot overflow and stops halfway, which makes it the most efficient arithmetic answer.