Smallest Larger Integer That Uses None of the Digits of n
Company: Elevenlabs
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Online Assessment
Overview: Given a positive integer below 2^31, find the smallest integer strictly greater than it that uses none of its decimal digits, or return -1 when no such integer exists. The problem tests reasoning about allowed digit sets, leading zeros, answers longer than the input, and results that exceed the 32-bit range.
Read the full Elevenlabs Software Engineer interview experience this question came from
Constraints
- 1 <= n <= 2^31 - 1 (n is strictly positive and less than 2147483648)
- The returned value can exceed 2^31 - 1 (use long in Java and long long in C++); it is always less than 10^11
- Return -1 exactly when no positive integer greater than n can be written using only digits that do not appear in n
Examples
Input: (1,)
Expected Output: 2
Explanation: Minimum valid input: only digit 1 is forbidden, so the next integer 2 is the answer.
Input: (5,)
Expected Output: 6
Explanation: Singleton: 1-4 are allowed but smaller than 5; the lead must exceed 5, so the answer is 6, not the smallest allowed digit 1.
Hints
- Start by working out which digits are still allowed, and decide when no positive integer can be written with them at all.
- Think about how many digits the answer can have compared with n, and what decides whether an answer with exactly as many digits as n exists.
- Once the first digit of the answer is fixed, ask how the remaining positions should be filled to keep the number as small as possible, remembering that the first digit cannot be 0.