GeeksforGeeks » Interview Questions

Amazon Interview Question for Software Engineer/Developer (0 - 2 Years)

(7 posts)
  • Started 5 months ago by Brainless
  • Latest reply from nagesforyou
  1. Brainless
    guest
    Posted 5 months ago #

    The next highest number in the given sequence
    ex: input:213476-->213467
    54321--> -1

  2. WgpShashank
    guest
    Posted 5 months ago #

    we have to generate the next higher permutation of given number isn't it ?

    1) From the end, keep decrementing till A[i] < A[i+1]..

    2) Now, find the closest element , greater than equal, to A[i] in

    A[i+1 ... n]. Say, the index of the found element is "j".

    3) Swap (A[i], A[j])

    4) Reverse array A[i+1 .. n]

    Example:

    Input 14532

    output 15234..

    1) 4 is the value where A[i] < A[i+1] when scanned from the end.

    2) The closest element grater than equal to 4 in the subarray 532 is

    5.

    3) Swap (4,5) : 14532 -> 15432

    4) Now, as we have identified in step 1 the index of i, we need to

    reverse the array from A[i+1, n] i.e. in 15(432) (432) needs to be

    reversed and hence we will get 15234...

    You Can find wiki permutation page for more detail :)

    let me know if any other method you are aware of ?

  3. Dedicated Programmer
    Member
    Posted 4 months ago #

    @WgpShashank ,
    can u explain me ur program for number: 15432
    I think here it gives wrong output.

  4. aks
    Member
    Posted 4 months ago #

    Add 9 repeatedly to the number until you arrive at a number which contains only the digits of original number. That will give you the expected number if there is one.

    To be optimized

    1. How long addition of 9 should repeat (Eg, for 12345, It is enough if we check till 54321)
    2. Method to check if it contains digits of Original number (Eg, 12354+9=12363. Compare(12354, 12363))

  5. sangeeta
    guest
    Posted 3 months ago #

  6. PsychoCoder
    Member
    Posted 3 months ago #

    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    
    char* getNextHighest (char *str) {
      int len = strlen (str);
      int i = len-2, index, minIndex ;
      char ch, minHighest;
    
      // Searching where we find str[i]<str[i+1] (pivot index)
      while (i>=0) {
        if (str[i] < str[i+1])
          break ;
        i -- ;
      }
    
      // Numbers are in decreasing order
      if (i<0)
        return "-1" ;
    
      index = minIndex = i+1 ;
      minHighest = str[index];
    
      // Search for the number which is equal or just higher than pivot index element
      while (index<len) {
        if ( (str[i] < str[index]) && (str[minIndex] >= str[index]) ) {
          minIndex = index ;
        } else break ;
        index ++ ;
      }
      ch = str[minIndex] ;
      str[minIndex] = str[i] ;
      str[i]= ch ;
    
      index = i+1 ;
      i = len-1;
    
      // Reverse the all the elements in the right to the pivot element
      while (index<i) {
        ch = str[index] ;
        str[index] = str[i] ;
        str[i]= ch ;
        index ++ ;
        i--;
      }
    
      return str ;
    }
    
    int main() {
      char buf[20] ;
      int num ;
      printf ( "\nEnter the number :\t" ) ;
      scanf ( "%d", &num );
      sprintf (buf,"%d",num) ;
      printf ("\nNext highest number of %d is %s\n", num, getNextHighest(buf) );
      return 0;
    }
  7. Nages
    Member
    Posted 3 months ago #

    Input = 11532
    Expected output = 12135

    1) Find a number (say the index as x) from right to left, which is smaller than its right. ( if can not find, then return -1)
    1 (1) 5 3 2
    2) Find a number (say the index as y) which is the least max of value of xth index, from the x to end, swap x and y
    1 (1) [5 3 (2)] ==> 1 (2) [5 3 (1)]
    3) Sort from x+1th index till end
    1 (2) [ 1 3 5 ] == > 12135


Reply

You must log in to post.

RSS feed for this topic