Smallest Number From Digits Without Leading Zeros

Quick Overview

Given a non-negative integer, rearrange its decimal digits into the smallest possible integer that has no leading zeros. The problem tests digit counting, a short greedy argument about which digit must go first, and careful handling of repeated zeros and the input 0.

Smallest Number From Digits Without Leading Zeros

Company: Airbnb

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given a non-negative integer `n`, return the smallest possible integer that can be formed using its decimal digits. Treat the digits as a multiset: every digit of `n` is used exactly once, repeats included. The result should not have leading zeros unless the number itself is `0`. ### Function Signature ```python def smallest_number(n: int) -> int: ``` ### Rules - Each digit is used exactly as many times as it appears in `n`. No digit may be added or dropped. - The result has no leading zeros, so it has the same number of digits as `n`. The only exception is `n = 0`, whose answer is `0`. - Return the result as an integer. ### Constraints - `0 <= n <= 10^9` ### Examples ```text Input: n = 310 Output: 103 ``` The digits are 3, 1 and 0. The number cannot start with 0, so the smallest allowed first digit is 1, followed by the remaining digits 0 and 3 in ascending order. ```text Input: n = 9876 Output: 6789 ``` There is no zero, so arranging the digits 9, 8, 7, 6 in ascending order gives 6789. ```text Input: n = 1020 Output: 1002 ``` The non-zero digits are 1 and 2, so the number starts with 1. The other arrangements that start with 1 are 1020 and 1200, and both are larger than 1002.

Overview: Given a non-negative integer, rearrange its decimal digits into the smallest possible integer that has no leading zeros. The problem tests digit counting, a short greedy argument about which digit must go first, and careful handling of repeated zeros and the input 0.

Airbnb logo
Airbnb
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given a non-negative integer n, return the smallest possible integer that can be formed using its decimal digits. Treat the digits as a multiset: every digit of n is used exactly once, repeats included.

The result should not have leading zeros unless the number itself is 0.

Function Signature

def smallest_number(n: int) -> int:

Rules

  • Each digit is used exactly as many times as it appears in n . No digit may be added or dropped.
  • The result has no leading zeros, so it has the same number of digits as n . The only exception is n = 0 , whose answer is 0 .
  • Return the result as an integer.

Constraints

  • 0 <= n <= 10^9

Examples

Input: n = 310
Output: 103

The digits are 3, 1 and 0. The number cannot start with 0, so the smallest allowed first digit is 1, followed by the remaining digits 0 and 3 in ascending order.

Input: n = 9876
Output: 6789

There is no zero, so arranging the digits 9, 8, 7, 6 in ascending order gives 6789.

Input: n = 1020
Output: 1002

The non-zero digits are 1 and 2, so the number starts with 1. The other arrangements that start with 1 are 1020 and 1200, and both are larger than 1002.