PPractick

DSA interview question

Reverse Integer

Easy · Algorithms · Topics: basic maths, number theory

The problem

A legacy payments system stores reference codes as signed 32-bit integers, and a new report needs each code with its digits reversed, keeping the sign. The catch: some codes reverse into numbers that no longer fit in 32 bits (reversing 1534236469 gives 9646324351), and the system has no 64-bit type available. For those, the function must return 0 instead of silently wrapping around. So the real problem is detecting overflow before it happens, using only 32-bit arithmetic.

Example: 123 → 321, -123 → -321, 120 → 21, 1534236469 → 0.

Function signature

int reverse(int x);  // reversed digits with the same sign, or 0 if the result overflows int

Test cases

Input xOutputExplanation
123321
-123-321The sign is kept
12021Trailing zeros become leading zeros and vanish
00
15342364690Reversed value 9646324351 exceeds INT_MAX
-21474836480Reversed value −8463847412 is below INT_MIN
14638474122147483641Fits just under INT_MAX (2147483647)

(Confirm: the environment can't store 64-bit integers, so overflow must be detected with 32-bit checks; return 0 on overflow.)

How interviewers phrase it

Clarifying questions to ask

  1. What should happen on overflow: return 0, throw, or saturate?
  2. Am I allowed to use a 64-bit type?
  3. How are negative numbers handled: keep the sign?
  4. Should trailing zeros disappear (120 → 21)?

Follow-ups to expect

  1. "How do you detect overflow without 64-bit arithmetic?"
  2. "Why 7 and −8?"
  3. "Why doesn't this code need special handling for negative numbers?"
  4. "What if 64-bit is allowed?"
  5. "Complexity?"

Practise questions like this with a real engineer

On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.

Book a free mock interview

Related questions

All 474 dsa interview questions

DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in