The number(ptr) to be swapped is the number that BREAKS the increasing trend when working from the last digit to the first digit.

"Start from the last digit and work your way backwards.
When you encounter a digit < previous digit, stop and replace with the min digit > ptr. Sort the rest of the digits."

This should work.

Eg 1: 121832
ptr=2;
ptr=3;
ptr=8;
ptr=1 < 8
swap(ptr,min_digit > 1 from (8,3,2)) = swap(ptr,2);
Number = 122831
ptr now points to the third digit where we swapped 1 with 2.
Sort the rest of the number from the swapped ptr to the units place.
Numbers to sort here is 831 => sorted order 138
hence result => 122138

Eg2: 12342
ptr = 2;
ptr = 4;
ptr = 3 < 4;
swap(3, min >3(4,2))
Number = 12432
ptr now points to the third digit where we swapped 3 with 4.
Sort the rest of the number from the swapped ptr to the units place.
Number = 12423

Keep in mind there is no answer for numbers that do not break the increasing trend.
For eg: 6520 => No ans
4321 => No ans
9531 => No ans, etc.

Hmmm hitman, you are correct. The algorithm earlier is WRONG, my apologies.

The number(ptr) to be swapped is the number that BREAKS the increasing trend when working from the last digit to the first digit. Let me suggest a change:

"Start from the last digit and work your way backwards keeping a running min of digits encountered.
When you encounter a digit < previous digit, stop and replace with the min digit > ptr. Sort the rest of the digits."

This should work.

Eg 1: 121832
ptr=2;
ptr=3;
ptr=8;
ptr=1 < 8
swap(ptr,min_digit > 1 from (8,3,2)) = swap(ptr,2);
Number = 122831
ptr now points to the third digit where we swapped 1 with 2.
Sort the rest of the number from the swapped ptr to the units place.
Numbers to sort here is 831 => sorted order 138
hence result => 122138

Eg2: 12342
ptr = 2;
ptr = 4;
ptr = 3 < 4;
swap(3, min >3(4,2))
Number = 12432
ptr now points to the third digit where we swapped 3 with 4.
Sort the rest of the number from the swapped ptr to the units place.
Number = 12423

The above algorithm would fail for 15432. Result 52341. Expected 21345.
The following step is invalid in algorithm above.
"- Swap the digit on the left of the 'hotspot' with the last digit of the new number."
Instead use,
"- Swap the digit on the left of the 'hotspot' with the first digit right to itself that is greater than itself"
E.g.,
after sorting the initial number 15432, you would get 12345.
Now replace 1 with first digit right to itself that is larger than itself, i.e. 2.
which gives 21345

1. Find the hotspot position (as stated above)
2. Sort all the numbers from the hotspot position to the end
3. Let us say the digit to the left of hotspot position is 'a'. Now swap 'a' with other digit 'b' such that b-a > 0 and minimum. Also this b is to be picked up from the digits starting from the hotspot position to the end of the array.

1. Find the hotspot position (as stated above)
2. Sort all the numbers from the hotspot position to the end
3. Let us say the digit to the left of hotspot position is 'a'. Now swap 'a' with other digit 'b' such that b-a > 0 and minimum. Also this b is to be picked up from the digits starting from the hotspot position to the end of the array.

1. Find the hotspot position (as stated above)
2. Sort all the numbers from the hotspot position to the end
3. Let us say the digit to the left of hotspot position is 'a'. Now swap 'a' with other digit 'b' such that b-a > 0 and minimum. Also this b is to be picked up from the digits starting from the hotspot position to the end of the array.

1. Find the hotspot position (as stated above)
2. Sort all the numbers from the hotspot position to the end
3. Let us say the digit to the left of hotspot position is 'a'. Now swap 'a' with other digit 'b' such that b-a > 0 and minimum. Also this b is to be picked up from the digits starting from the hotspot position to the end of the array.

1. Find the hotspot position (as stated above)
2. Sort all the numbers from the hotspot position to the end
3. Let us say the digit to the left of hotspot position is 'a'. Now swap 'a' with other digit 'b' such that b-a > 0 and minimum. Also this b is to be picked up from the digits starting from the hotspot position to the end of the array.

Hi friends,
I have implemented the above in different logic using java
Let us take the number 15432 for which expected output is 21345
so, here the logic is first we need to identify the smaller number comparing with the prev number beginning from the end

for number 15432
1st comp: currNum=2 prevNum=-1(at the beginnin) cond= is curNum<prevNum
if cond failed then store the currNum to Array: 2

2nd comp: currNum=3 preNum=2, here also cond fails and stores the value to array: 2,3
Note: array here will be by default in sort order
.
.
5th comp: curNum=1 preNum=5, here cond satisfies and our logic will start,
now we found the number or hotspot, now start comparing the values in array with the hotspot number

is 1 < firstElment in Array (1<2) if yes replace the number 1 with 2 and 2 to 1 in array
and start adding all the array values to one's position of obtained digit one by one

After Swap num: 2 and in Array 1,3,4,5
After adding values of array: 21345

Use an array of 10 elements in which u can store the count of each digit of your given number.
Now traverse the array from end and those with (count > 0), pick and make a number out of them.
Eg:
number: 568 araay: 0 0 0 0 0 1 1 0 1 0
start from end and make the number = 8 6 5

Simple Algo (not optimized) would be
1. Break the number into digits.
2. Find all possible combinations of these digits.
3. Sort these combinations.
4. Find number next to the original number in sorted array.

Now optimize Step 2, while forming numbers from digits, discard all number who are less than original number.

1.Continue until we have the right to left increasing sequence going, starting from right end that is.
2.Let's call the point here the sequence breaks, the pivot point.
3.Swap the digit at the pivot point with a number to its right which is greater than itself by a minimum amount .
4.Sort the digits after the pivot point in ascending order.

Algorithm:
- Move from right -> left and stop when the current digit is greater than the next one, let's call this point, 'hotspot'.
- Sort all the digits from hotspot till the end of the number in increasing order.
- Swap the digit on the left of the 'hotspot' with the last digit of the new number.

//decrement i till digits are increasing from right to left
for(i=len-1; num[i] < num[i-1]; i--)
{
//return if the digits are sorted in decreasing
//order, number is already the largest.
if(i == 1)
return;
}

//sort everything from hotspot till the end
sort(num, i, len-1);

//swap digit before hotspot with the last one
swap(&(num[i-1]), &(num[len-1]));
}

What's Going On

Books

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Resume Review

Most engineers make critical mistakes on their resumes -- we can
fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.