Amazon Interview Question for Software Engineer / Developers


Country: India




Comment hidden because of low score. Click to expand.
11
of 11 vote

Just use counting sort ( O(n) ). Then rearrange the number starting with the biggest one.
Note that the nested for loop below make a total of n iterations only. So the resulting code is O(n).

public int getLargestRearranged( int number ){
   int[] digitCounts = new int[10];
      
   int remainingDigits = number;
   while( remainingDigits > 0 ){
      int currentDigit = remainingDigits % 10;
      remainingDigits /= 10;
      digitCounts[currentDigit]++;
   }
   
   int rearrangedNumber = 0;
   for( int i = 9; i >= 0; i-- ){
      
      if( digitCounts[i] == 0 ) continue;
      
      for( int j = 0; j < digitCounts[i]; j++ ){
         rearrangedNumber *= 10;
         rearrangedNumber += i;
      }
   }
   
   return rearrangedNumber;
}

- hakanserce January 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The solution is not O(n).
Only the array formation part is O(n).
Number formation part is O(n^2)

- popoff January 03, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi Exact name would be count sort.
It does put keys into buckets , however since the bucket can have only single element and we only store the count encountered.

- krbchd January 03, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@popoff As I mentioned in the answer above, the nested for loop part is actually O(n). The nested loop iterates as much as the number of digits. Note that having a nested loop does not always mean that we have O(n^2) execution time. This problem is a perfect example for this.

- hakanserce January 03, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@krbchd Thanks for the comment.

- hakanserce January 03, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I second that nested loop doesnot always mean n**2.. Good solution.

- isandesh7 February 17, 2013 | Flag
Comment hidden because of low score. Click to expand.
3
of 3 vote

public static int max(int n) {
	int[] digits = new int[10];
		
	while(n > 0) {
		digits[n%10] += 1;
		n = n/10;
	}
	int result = 0;
	int i = digits.length - 1;
	while(i > 0) {
		if(digits[i] > 0) {
			result = result * 10 + i;
			digits[i] -= 1;
			continue;
		}
		i--;
	}
	return result;
}

- Anonymous January 03, 2013 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

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.

Learn More

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.

Learn More

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.

Learn More