Magicsheet logo

A Number After a Double Reversal

Easy
12.5%
Updated 8/1/2025

Asked by 4 Companies

Topics

A Number After a Double Reversal

What is this problem about?

The A Number After a Double Reversal coding problem is a logic-based math challenge. You are given an integer. You reverse it to get reversed1, then you reverse reversed1 to get reversed2. You need to determine if reversed2 is equal to the original number. For example, reversing 123 gives 321, and reversing that gives 123 (Match!). However, reversing 120 gives 21, and reversing that gives 12 (No match!).

Why is this asked in interviews?

While this is an Easy question, companies like Meta and Bloomberg use it to test for edge-case awareness and "out-of-the-box" thinking. A candidate could write a full reversal function, but an efficient candidate will realize there is a much simpler mathematical property at play.

Algorithmic pattern used

This falls under the Math interview pattern. The core insight is that a double reversal only fails if the number has trailing zeros (unless the number itself is 0). Trailing zeros are lost during the first reversal because leading zeros are not preserved in integers.

Example explanation

  • **Case 1: num = 526**. No trailing zeros. Reversing once gives 625. Reversing again gives 526. True.
  • **Case 2: num = 1800**. Reversing once gives 81 (the zeros disappear). Reversing again gives 18. 18 != 1800. False.
  • **Case 3: num = 0**. Reversing 0 gives 0, and again gives 0. True.

Common mistakes candidates make

  • Over-coding: Writing a full loop to reverse the digits twice. While it works, it is unnecessary and slower than a simple constant-time check.
  • Forgetting Zero: Thinking that all numbers ending in zero fail, forgetting that the number 0 itself actually passes the test.
  • String Conversion: Converting the number to a string to reverse it, which adds unnecessary O(D)O(D) space where DD is the number of digits.

Interview preparation tip

Always look for the "trick" in Easy math problems. If a problem seems like it requires a lot of manual steps (like reversing a number twice), there is often a property of numbers (like the parity, sign, or trailing zeros) that lets you answer the question in O(1)O(1) time.

Similar Questions