Magicsheet logo

Smallest Value of the Rearranged Number

Medium
95.6%
Updated 6/1/2025

Asked by 3 Companies

Smallest Value of the Rearranged Number

What is this problem about?

The "Smallest Value of the Rearranged Number" is a classic coding challenge that tests your understanding of number representation and sorting. Given an integer, your task is to rearrange its digits such that the resulting number is the smallest possible value. Importantly, the rearranged number must not have any leading zeros unless the number itself is zero.

This problem becomes interesting when dealing with both positive and negative integers. For a positive number, you want the smallest possible value, which means the digits should generally be in ascending order. For a negative number, the goal is to make it as small as possible in value, which means its absolute magnitude should be as large as possible—requiring the digits to be in descending order.

Why is this asked in interviews?

Companies like Microsoft and Meta ask this interview question to evaluate a candidate's ability to handle edge cases and basic data manipulation. While the core logic involves sorting, the "no leading zeros" rule for positive numbers and the different sorting requirements for negative numbers require careful conditional logic. It’s a great way to see if a candidate can translate simple mathematical rules into clean, bug-free code.

Algorithmic pattern used

The primary algorithmic pattern used here is Sorting combined with Greedy logic. By treating the number as a collection of digits (either by converting it to a string or a list of integers), we can apply a sort. For positive numbers, we sort digits in non-decreasing order. For negative numbers, we sort in non-increasing order. The greedy choice ensures that placing the smallest non-zero digit in the most significant position (for positive numbers) yields the overall minimum value.

Example explanation

Let's consider two examples:

  1. Input: 3105.
    • Digits: [3, 1, 0, 5].
    • Sorted: [0, 1, 3, 5].
    • Since we can't have a leading zero, we find the first non-zero digit (1) and swap it with the first digit.
    • Result: 1035.
  2. Input: -7605.
    • Absolute Digits: [7, 6, 0, 5].
    • Sorted descending: [7, 6, 5, 0].
    • Result: -7650. (Note: for negative numbers, larger absolute value means smaller numerical value).

Common mistakes candidates make

The most frequent mistake is failing to handle the "no leading zeros" constraint correctly for positive numbers. Simply sorting and joining digits will result in "0135" instead of "1035". Another common error is applying the same ascending sort to negative numbers, which actually produces the largest possible negative value (the one closest to zero) rather than the smallest. Finally, candidates sometimes forget to handle the number zero itself, which should simply return zero.

Interview preparation tip

To excel in the "Smallest Value of the Rearranged Number interview question," practice problems that involve digit manipulation without using high-level string conversions if possible (though string conversion is often fine in interviews). Get comfortable with sorting custom objects and handling different logic branches for positive and negative inputs. This problem is a perfect example of why you should always clarify constraints like leading zeros with your interviewer.

Similar Questions