Airbnb questions

Smallest Permutation Above a Lower Bound

AirbnbPhone screenMedium

You are given the digits of a number as a list, and a lower bound. Rearrange all of the digits, each used exactly once, into the smallest number that is at least the lower bound.

Write smallest_permutation(digits, lower_bound).

Part 1: Smallest rearrangement at least the bound

Return the smallest number, as a string, that uses every digit of digits exactly once and is at least lower_bound. A rearrangement may not start with 0 (a single digit 0 is fine). Return "-1" when there is none.

For example, digits = [7, 1, 8] and lower_bound = 719 give "781", and digits = [1, 7, 8] with lower_bound = 200 gives "718".

  1. Example 1
    digits
    [7,1,8]
    lower_bound
    719
    Output
    "781"

    Why: Of the rearrangements of 7, 1 and 8, 718 is below 719 and 781 is the smallest that reaches it.

  2. Example 2
    digits
    [1,7,8]
    lower_bound
    200
    Output
    "718"

    Why: 178 and 187 are below 200, and 718 is the smallest rearrangement of 1, 7 and 8 that reaches it.

Constraints

  • 1 <= len(digits) <= 16
  • 0 <= digits[i] <= 9
  • 0 <= lower_bound <= 10^15
Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question
Smallest Permutation Above a Lower Bound: Airbnb interview question | LoopCoach