Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7557733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:12:34+00:00 2026-05-30T12:12:34+00:00

I just bombed an interview and made pretty much zero progress on my interview

  • 0

I just bombed an interview and made pretty much zero progress on my interview question.

Given a number, find the next higher number which has the exact same
set of digits as the original number. For example: given 38276 return
38627

I wanted to begin by finding the index of the first digit (from the right) that was less than the ones digit. Then I would rotate the last digits in the subset such that it was the next biggest number comprised of the same digits, but got stuck.

The interviewer also suggested trying to swap digits one at a time, but I couldn’t figure out the algorithm and just stared at a screen for like 20-30 minutes. Needless to say, I think I’m going to have to continue the job hunt.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T12:12:35+00:00Added an answer on May 30, 2026 at 12:12 pm

    You can do it in O(n) (where n is the number of digits) like this:

    Starting from the right, you find the first pair-of-digits such that the left-digit is smaller than the right-digit. Let’s refer to the left-digit by “digit-x”. Find the smallest number larger than digit-x to the right of digit-x, and place it immediately left of digit-x. Finally, sort the remaining digits in ascending order – since they were already in descending order, all you need to do is reverse them (save for digit-x, which can be placed in the correct place in O(n)).

    An example will make this more clear:

    123456784987654321
    start with a number
    
    123456784 987654321
             ^the first place from the right where the left-digit is less than the right  
             Digit "x" is 4
    
    123456784 987654321
                  ^find the smallest digit larger than 4 to the right
    
    123456785 4 98764321
            ^place it to the left of 4
    
    123456785 4 12346789
    123456785123446789
             ^sort the digits to the right of 5.  Since all of them except 
             the '4' were already in descending order, all we need to do is 
             reverse their order, and find the correct place for the '4'
    

    Proof of correctness:

    Let’s use capital letters to define digit-strings and lower-case for digits. The syntax AB means “the concatenation of strings A and B“. < is lexicographical ordering, which is the same as integer ordering when the digit-strings are of equal length.

    Our original number N is of the form AxB, where x is a single digit and B is sorted descending.
    The number found by our algorithm is AyC, where y ∈ B is the smallest digit > x (it must exist due to the way x was chosen, see above), and C is sorted ascending.

    Assume there is some number (using the same digits) N' such that AxB < N' < AyC. N' must begin with A or else it could not fall between them, so we can write it in the form AzD. Now our inequality is AxB < AzD < AyC, which is equivalent to xB < zD < yC where all three digit-strings contain the same digits.

    In order for that to be true, we must have x <= z <= y. Since y is the smallest digit > x, z cannot be between them, so either z = x or z = y. Say z = x. Then our inequality is xB < xD < yC, which means B < D where both B and D have the same digits. However, B is sorted descending, so there is no string with those digits larger than it. Thus we cannot have B < D. Following the same steps, we see that if z = y, we cannot have D < C.

    Therefore N' cannot exist, which means our algorithm correctly finds the next largest number.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

just a quick question: I am a CS undergrad and have only had experience
Just how much slower are events? I have written a streaming XML parser (that
Just found this out, so i am answering my own question :) Use a
Just had a question regarding the UILabel class. I know that the UITextField control
Just starting to figure Python out. I've read this question and its responses: Is
Just a stupid question. Provide a code snippet b=a=2 How to extract the value
Just a quick question. I have an NSArray that is generated in my applicationDidFinishLaunching
Just have a quick question about the GAC I created an assembly Awesome.DLL. Had
just find gives me: . ./bla-bla_(11) ./bla-bla_(1) ./rename ./rename~ This find . | grep
Just a quick question. If I learn how to develop apps for IPhone using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.