What candidate are they looking for if they ask this question in the interview?

What candidate are they looking for if they ask this question in the interview?

Attached: L556.jpg (1440x2196, 209.07K)

a black trans lesbian so they can fill their diversity quota

isn't example 1 incorrect? Shouldn't it return 13?

A candidate serious and hungry to spend some time preparing for an interview before applying

one who knows how to calculate the next lexicographic permutation

brainlet
gatekeeping is honestly sad
hurr durr no implementation, just write a for loop bro

someone who can do basic programing operations such as writing a for loop and swapping array elements

1. Parse number into digits as a set.
2. Iterate over all partitions of the set (just google it).
3. Re-merge a partition into a number
4. Record smallest.

They're looking for people who can solve problems and apply basic principles under novel (to them) conditions. Get over your fear of your 7th grade math teacher's pendulous sagging breasts. This problem is simple as fuck.

>exponential runtime

retard. The optimal answer is a bubblesort with early exit.

>Iterate over all partitions of the set (just google it).
That's probably really slow. Isn't it just
>parse number into a list of digits
>start from the last digit, and for each digit...
>...check if there is a digit behind the current digit that's larger than it
>if there is, then select the smallest digit that's larger than the current digit and is behind the current digit
>swap the two
>sort the last digits behind the current digit descencding order
>check if the new number fits into a 32-bit int
>convert to 32-bit int

Example:
>897791832
>897792831
>897792138

meant to qoute

what did they mean by the second example?
how is there a valid answer that contains exaclty one digit 1 and one digit 2 that doesn't fit in 32 bit integer and it's greater than 21?

Parse to digits
Sort digits
Cast to int
If negative or equal to initial number - return -1.

The new number has to be bigger than the current number. 21 is already the biggest 2-digit number composed of the digits 1 and 2.

What would your algorithm return when the input is 123?

321?

Is that the correct anwser?

>Iterate over all partitions of the set (just google it).
Why would I do this when I can simply put the digits in an array and sort it in descending order. If it's already in order, I output -1.

They're looking for me
def next_greatest(n):
digits = list(map(int, str(n)))
largest = digits[-1]
for i in reversed(range(len(digits) - 1)):
if digits[i] < largest:
break
largest = digits[i]
else:
return -1
prefix = digits[:i]
postfix = digits[i:]
replacement = min(d for d in postfix if d > digits[i])
postfix.remove(replacement)
postfix.sort()
new = int(''.join(map(str, [*prefix, replacement, *postfix])))
if new > 2**31 - 1:
return -1
return new

>reversed(range(len(digits)-1))
I genuinely hate people who write like this. Just learn how to use range properly

You're forgetting about the "smallest" part, read again.

I know how to write range(len(digits) - 2, -1, -1) but it looks insane. I find this easier to reason about.
It should be about equally efficient.