Smallest number from odd digits

Python data engineering interview problem. Difficulty: beginner. Pattern: Arrays. About 8 minutes. Free to practice.

Keep the odd digits of a number and arrange them into the smallest possible number. Treat this as a production helper: match the contracted return shape, including empty and duplicate inputs.

A numeric cleanup rule keeps only odd digits and wants the smallest possible number made from them. Take every odd digit from n, including repeated occurrences, sort those digits into the smallest number, and ignore the sign. Return -1 when no odd digit exists. Write smallest_odd(n: int) -> int. The odd digits of 5231 are 5, 3 and 1; arranging them as 135 gives the smallest number. The starter file already contains the function name, harness and print lines. Keep them and fill only the function body.

Requirements

  • Keep every odd digit with its original multiplicity.
  • Arrange kept digits in ascending order.
  • Return -1 when the filtered digit list is empty.

Constraints

  • n is an integer and its sign is ignored.
  • The return type is int.

Examples

Input: smallest_odd(5231) Output: 135 The odd digits are 5, 3 and 1; ascending they form 135.

Topics: lakebench, python, digits, sorting.

More Python interview questions · All interview problems · Learn data engineering

beginner

Smallest number from odd digits

Interview-style drill: Keep the odd digits of a number and arrange them into the smallest possible number.

Implement `smallest_odd(n: int) -> int`. Take the odd digits (1, 3, 5, 7, 9) of `n`, using every odd digit exactly as often as it occurs, and arrange them to form the smallest possible number. Ignore the sign of `n`. Return `-1` if `n` has no odd digit. Example: `5231` has odd digits 5, 3 and 1, and the smallest number from them is `135`.