DSA interview question
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.
int reverse(int x); // reversed digits with the same sign, or 0 if the result overflows int
Input x | Output | Explanation |
|---|---|---|
| 123 | 321 | |
| -123 | -321 | The sign is kept |
| 120 | 21 | Trailing zeros become leading zeros and vanish |
| 0 | 0 | |
| 1534236469 | 0 | Reversed value 9646324351 exceeds INT_MAX |
| -2147483648 | 0 | Reversed value −8463847412 is below INT_MIN |
| 1463847412 | 2147483641 | Fits 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.)
long long. How do you detect overflow?"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 interviewAll 474 dsa interview questions
DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in