Amazon Interview Question for SDETs


Country: India
Interview Type: Phone Interview




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

We can use Hash Table to optimize further, --> O(n)

Algorithm:
1. Take a hash table with size 10 (0 to 9).
2. Store the count of every digit from 0 to 9.
3. Print the index of the Hash table with respect to digit count in the reverse direction (from 9 to 0).

Example:

Hash table after digit count for 8754365 -> (0 0 0 1 1 2 1 1 1 0)
Print the index of the hash table with respect to their count in reverse order -> 8765543
Time Complexity : O(n)

Correct me if I am wrong.
thanks

- Srinu Kolukuluri March 20, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The interviewer asked me to use hashMap.
I am able to count the number using HashMap but how will you print based on the count ?

- zammer March 20, 2015 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Hi, Let me try

Lets assume the following:
Count of digit 0 is ----- x0
Count of digit 1 is ----- x1
Count of digit 2 is ----- x2
Count of digit 3 is ----- x3
Count of digit 4 is ----- x4
Count of digit 5 is ----- x5
Count of digit 6 is ----- x6
Count of digit 7 is ----- x7
Count of digit 8 is ----- x8
Count of digit 9 is ----- x9
Lenth of given number is --- n
Take a constant ---- k (I will explain why we needed)

So, the following is true for every digit :

x0<=n, x1<=n, x2<=n, x3<=n, x4<=n, x5<=n, x6<=n, x7<=n, x8<=n, x9<=n (Consider the count is positive integer only) --- > 0<=xi<=n

x0+x1+x2+x3+x4+x5+x6+x7+x8+x9 = n



Time complexity to access Hash Table:

Here there are two types of cells will be there in our hash table/hash map,
1) Cell of Hash table with zero counts
2) Cell of Hash table with Non-zero counts

Lets assume Number of accesses for each cell of hash table is “ai”
for 0th index ---- a0; 1<=a0<=n
For 1st index -- a1; 1<=a1<=n
…………….
And so on to --a9

So, the total accesses will be: a0+a1+a2+a3+………a9 >=n
Lets put it in this way:
a0+a1+a2+a3+………a9 =n+k
Here, n is to access cells with non-zero count of hash table, k is to access cells with zero count.

Lets take an example:

1) 999999999999999
Hash table = (0 0 0 0 0 0 0 0 0 15)
Accessing time = 1*1 + 1*1 + 1*1 + 1*1 + 1*1 + 1*1 + 1*1 + 1*1 + 1*1 + 1*15
= 15 + 9
= O (n + k), always k<=n
= O(n)

2) 999999998883210

Hash table = (1 1 1 1 0 0 0 0 3 8)
Accessing time = 1*1 + 1*1 + 1*1 + 1*1 + 1*1 + 1*1 + 1*1 + 1*1 + 1*3 + 1*8
= 15 + 4
= O(n + k), always k<=n
= O(n)
I know that It was lengthy, I Just tried to analyse :)
Correct me If I am wrong
Srinu

- Srinuiitb March 20, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi, Let me try

Lets assume the following:
Count of digit 0 is -----> x0
Count of digit 1 is -----> x1
Count of digit 2 is -----> x2
Count of digit 3 is -----> x3
Count of digit 4 is -----> x4
Count of digit 5 is -----> x5
Count of digit 6 is -----> x6
Count of digit 7 is -----> x7
Count of digit 8 is -----> x8
Count of digit 9 is -----> x9
Lenth of given number is ---> n
Take a constant ----> k (I will explain why we needed)

So, the following is true for every digit :
x0<=n, x1<=n, x2<=n, x3<=n, x4<=n, x5<=n, x6<=n, x7<=n, x8<=n, x9<=n (Consider the count of each digit is positive integer only) --- > 0<=xi<=n

x0+x1+x2+x3+x4+x5+x6+x7+x8+x9 = n



Time complexity to access Hash Table:

Here there are two types of cells will be there in our hash table/hash map,
1) Cell of Hash table with zero counts
2) Cell of Hash table with Non-zero counts

Lets assume Number of accesses for ith cell of hash table is “ai”
i.e..., For 0th index ----> a0; 1<=a0<=n
For 1st index ----> a1; 1<=a1<=n
…………….
And so on upto --a9
So,

the total accesses will be: a0+a1+a2+a3+………a9 >=n

Lets put it in this way:

a0+a1+a2+a3+………a9 =n+k

Here, n is to access cells with non-zero count of hash table, k is to access cells with zero count.

Lets take an example:

1) 999999999999999
Hash table = (0 0 0 0 0 0 0 0 0 15)
Accessing time = 1*1 + 1*1 + 1*1 + 1*1 + 1*1 + 1*1 + 1*1 + 1*1 + 1*1 + 1*15
= 15 + 9
= O (n + k), always k<=n
= O(n)




2) 999999998883210

Hash table = (1 1 1 1 0 0 0 0 3 8)
Accessing time = 1*1 + 1*1 + 1*1 + 1*1 + 1*1 + 1*1 + 1*1 + 1*1 + 1*3 + 1*8
= 8 + 3 + 8
= 15 + 4
= O(n + k), always k<=n
= O(n)

Correct me If I am wrong, I Just tried to analyse

- Srinu Kolukuluri March 20, 2015 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

How about if the number is negative?

- mtb March 21, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

well, give me an example.

is it the following ????
input: -8754365
output: -3455678

if so, follow the same algo, print the numbers in the map from o'th index to last.

- Srinu Kolukuluri March 23, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Instead of using a hashmap, you can just use an array of size 10, where array[i] is the count of the digit i.

- Anonymous March 24, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thanks, and I know that. Just given the answer with that so called Hash Table, so continued with it :) :P

- Anonymous March 24, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

def sort(value):
    map = {}
    result = ""
    print "input", value
    for i in value:
        if i in map.keys():
            map[i] += 1
        else:
            map[i] = 1
    for i in range(9, -1, -1):
        i = str(i)
        while i in map.keys() and map[i] > 0:
            result += i
            map[i] = int(map[i]) -1
    return result

result = sort("1244522398968")
print "output", result

- aka December 01, 2015 | Flag
Comment hidden because of low score. Click to expand.
3
of 3 vote

If storage isn't an issue, just divide number by 10 to get each digit out increment the counter in array of size 10.so if there are two 9s, then a[9] will be 2 then just traverse a[9] to a[0] and multiple the result by 10, a digit is added.

- casper March 20, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

public static int getMax(int num) {
String arr[] = new String[10];
Arrays.fill(arr, "");
int index = 0;
while (num > 0) {
index = num % 10;
arr[index] += index;
num = num / 10;
}
String result = "";
for (int i = arr.length - 1; i >= 0; i--) {
result += arr[i];
}
return Integer.parseInt(result);
}

- Anonymous March 20, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I assume you said either mergesort or quicksort but backwards, so it sorts from large to smallest.

I don't think there is any real optimization for these.

- SK March 20, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Why hash table, simple array of size 10 could work as well.

- casper March 20, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yeah, Simple array would work :) thanks

- Srinu Kolukuluri March 20, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

simple array is a kind of hastable where hash function does nothing except returning its argument, so the answer is still correct

- Anonymous March 20, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Perhaps you can use a binary search tree and as you are doing an in order tree traversal push all the numbers into a queue. The number contained in the queue is the largest number.

- Alex March 20, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This would work, but the bottleneck would be adding numbers to the BST. This would be O(n^2) complexity, worse than just plain sorting the numbers in the first place.

- SK March 20, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

// find maximum number using digits
public void maxNum(){
int input = 567443;
ArrayList inputList = new ArrayList();
String str = Integer.toString(input);
for(int i=0; i<str.length(); i++){
inputList.add(str.charAt(i));
}
Collections.sort(inputList);
String str1 = "";
for(int i=str.length()-1; i>=0; --i){
str1 += inputList.get(i);
System.out.println("str = "+str1);
}
System.out.println("output = "+str1);
}

- Anonymous March 20, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// find maximum number using digits
	public void maxNum(){
		int input = 567443;
		ArrayList inputList = new ArrayList();
		String str = Integer.toString(input);
		for(int i=0; i<str.length(); i++){
			inputList.add(str.charAt(i));
		}
	    Collections.sort(inputList);
	    String str1 = "";
		for(int i=str.length()-1; i>=0; --i){
			str1 += inputList.get(i);
			System.out.println("str = "+str1);
		}
		System.out.println("output = "+str1);
	}

- Anonymous March 20, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Counting sort via array of counts seems the simplest solution and in O(n).

So you're on the right track thinking about merge/quicksort, however just need to keep in mind that there are certain faster sorting methods that can be applied in special situations (other popular ones include bucket and radix sort).

- JW March 20, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thanks.

later on I also found that the counting sort will be better and optimal solution.

- zammer March 20, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

# @Author :Ankit
def maxnum(n):
    numlist=list(n)
    numlist.sort(reverse=True)
    str1 = ''.join(numlist)
    return str1
print maxnum("8754365")

- Ankit March 20, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args) {
Map<Integer, Integer> mp = new HashMap<>();
mp.put(0, 0);
mp.put(1, 0);
mp.put(2, 0);
mp.put(3, 0);
mp.put(4, 0);
mp.put(5, 0);
mp.put(6, 0);
mp.put(7, 0);
mp.put(8, 0);
mp.put(9, 0);
String num = "78549203390";
for (char ch : num.toCharArray()) {
int i = Integer.valueOf(Character.toString(ch));
if (mp.containsKey(i)) {
int val = mp.get(i);
mp.put(i, ++val);
}
}
StringBuilder maxNum = new StringBuilder();
for (int key = 9; key >= 0; key--) {
if (mp.containsKey(key)) {
for (int i = 0; i < mp.get(key); i++) {
maxNum.append(key);
}
}
}
System.out.println(maxNum);
}

- manojdandy March 20, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static long MaxIntergerFromAlgarithmsIn(long x){
		
		long tens = 1;
		while(tens <= x)
		{
			tens*=10;
			
		}
		
		
		int[] hist = new int[10];
		for(long t = tens; t>=1 ;t /=10){
			hist[x/t]++;
			x -= (x/t)*t;
		}
		
		long res = 0;
		for(int j = 9 ; j>=0 ; --j){
			while(hist[j]>0){
				tens /=10;
				res += tens*j;
				hist[j]--;
			}
		}
		return res;

}

- albinoadriano March 20, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It should be

O(n)

where n is the number of digits in the input x.

- albinoadriano March 20, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int _tmain(int argc, _TCHAR* argv[])
{
	unsigned long long num = 8754365;
	cout << "Orig:   " << num << endl;
	cout << "Num:    " << GetLargestNumber(num) << endl;
	cout << "String: " << GetLargestNumberString(num) << endl;

	return 0;
}

std::string GetLargestNumberString(unsigned long long num)
{
	std::stringstream largestNumberString;

	// Count the digit values
	int digitArray[10] = {};
	while (num > 0)
	{
		digitArray[num % 10]++;
		num /= 10;
	}

	for (int arrayIndex = 9; arrayIndex >= 0; arrayIndex--)
	{
		for (int digitCount = 0; digitCount < digitArray[arrayIndex]; digitCount++)
		{
			if (digitArray[arrayIndex] > 0)
			{
				largestNumberString << arrayIndex;
			}
		}
	}

	return largestNumberString.str();
}

unsigned long long  GetLargestNumber(unsigned long long num)
{
	unsigned long long  largestNumber = 0;
	
	// Count the digit values
	int digitArray[10] = {};
	while (num > 0)
	{
		digitArray[num % 10]++;
		num /= 10;
	}

	int power = 0;
	for (int arrayIndex = 0; arrayIndex < 10; arrayIndex++)
	{
		for (int digitCount = 0; digitCount < digitArray[arrayIndex]; digitCount++)
		{
			if (digitArray[arrayIndex] > 0)
			{
				largestNumber += arrayIndex * pow(10, power);
				power++;
			}
		}
	}

	return largestNumber;
}

- CareerCupDom March 20, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int formLargestNum(int num) {
		int[] numArray = new int[10];
		while (num > 0) {
			int rem = num % 10;
			num = num / 10;
			numArray[rem] += 1;		
		}
		int output = 0;
		for ( int i = 9; i >= 0; i--) {
			int count = numArray[i];
			while (count > 0) {
				output *= 10;
				output += i;
				count--;				
			}		
		}
		return output;
	}

- Check This March 21, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public long findMaxNumber(long n)
	{
		long result=0;
		int[] a = new int[10];
		while(n!=0)
		{
			int temp = (int)n%10;
			a[temp]++;
			n = n/10;
		}
		for(int i=a.length-1;i>=0;i--)
		{
			if(a[i]==0)
				continue;
			else
				for(int j=0;j<a[i];j++)
					result = result*10+i;
		}
		return result;
	}

- Anonymous March 22, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public long findMaxNumber(long n)
	{
		long result=0;
		int[] a = new int[10];
		while(n!=0)
		{
			int temp = (int)n%10;
			a[temp]++;
			n = n/10;
		}
		for(int i=a.length-1;i>=0;i--)
		{
			if(a[i]==0)
				continue;
			else
				for(int j=0;j<a[i];j++)
					result = result*10+i;
		}
		return result;
	}

- coddicted March 22, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static long findMaxNumber(long num)
{
long maxNumber = 0,placeValue=1;
long temp;int count=0;
int hashMap[] = new int[10];
temp = num;
while(temp!=0)
{
hashMap[(int)temp%10]++;
temp=temp/10;
}
for(int i=0;i<hashMap.length;i++)
{
count=hashMap[i];
while(count>0)
{
maxNumber=maxNumber+placeValue*i;
placeValue=placeValue*10;
count--;
}
}
System.out.println(maxNumber);
return maxNumber;
}

- alregith March 22, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.Scanner;
import java.util.Arrays;
class Arrange 
{
	public static void main(String[] args) 
	{
		System.out.println("Enter Any Number");
		Scanner s=new Scanner(System.in);
		String str=s.next();
		byte b[]=str.getBytes();
		Arrays.sort(b);
		System.out.println("The maximum number formed by ur number is..");
		String str2=new String(b);
		StringBuilder sb=new StringBuilder(str2);
		System.out.println(sb.reverse());

	}
}

- pulipati.pulipati.prasad4 March 22, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package amazon.interview;

import java.io.IOException;
import java.util.Arrays;

/**
*
* @author Shrey
*/
public class AmazonInterview {

/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
// TODO code application logic here
String number="6733465998723145";
String result="";
char array[] = number.toCharArray();
Arrays.sort(array);
for(int i=array.length-1;i>=0;i--){
result=result+array[i];
}
System.out.println(result);

}
}

- Shreyas March 22, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package amazon.interview;

import java.io.IOException;
import java.util.Arrays;

/**
*
* @author Shrey
*/
public class AmazonInterview {

/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
// TODO code application logic here
String number="6733465998723145";
String result="";
char array[] = number.toCharArray();
Arrays.sort(array);
for(int i=array.length-1;i>=0;i--){
result=result+array[i];
}
System.out.println(result);

}
}

- Shreyas March 22, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int i = 87504365, o = 0
def a = []; 10.times{ a += 0 }
while(i){ a[i % 10]++; i = i / 10 }
(9..0).each{ d -> if (a[d]) a[d].times{ o = o * 10 + d } }
o // 87655430

- sebnukem March 24, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package com.javafries.number;
 
/**
* @author vishal.naik
*/
 
public class MaximumNumberProblem {
 
    private static int findMaxNumberFromDigits(int num) {
        int[] digits = getDigitsInSortedOrder(num);
 
        int maxNum = 0;
        for (int index = digits.length - 1; index >= 0; index--) {
 
            int frequency = digits[index];
            while (frequency > 0) {
                maxNum = maxNum * 10 + index;
                frequency--;
            }
        }
        return maxNum;
    }
 
    private static int[] getDigitsInSortedOrder(int num) {
        int[] digits = new int[10];
 
        while (num > 0) {
            // Get remainder
            int index = num % 10;
            digits[index]++;
            num /= 10;
        }
        return digits;
    }
 
    public static void main(String[] args) {
        int num = 38293367;
        int maxNum = findMaxNumberFromDigits(num);
        System.out.println("Maximum number : " + maxNum);
    }
}

- Anonymous March 24, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class MaximumNumberProblem {

private static int findMaxNumberFromDigits(int num) {
int[] digits = getDigitsInSortedOrder(num);

int maxNum = 0;
for (int index = digits.length - 1; index >= 0; index--) {

int frequency = digits[index];
while (frequency > 0) {
maxNum = maxNum * 10 + index;
frequency--;
}
}
return maxNum;
}

private static int[] getDigitsInSortedOrder(int num) {
int[] digits = new int[10];

while (num > 0) {
// Get remainder
int index = num % 10;
digits[index]++;
num /= 10;
}
return digits;
}

public static void main(String[] args) {
int num = 38293367;
int maxNum = findMaxNumberFromDigits(num);
System.out.println("Maximum number : " + maxNum);
}
}

- Vishal Naik March 24, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote
{{{public class MaximumNumberProblem { private static int findMaxNumberFromDigits(int num) { int[] digits = getDigitsInSortedOrder(num); int maxNum = 0; for (int index = digits.length - 1; index >= 0; index--) { int frequency = digits[index]; while (frequency > 0) { maxNum = maxNum * 10 + index; frequency--; } } return maxNum; } private static int[] getDigitsInSortedOrder(int num) { int[] digits = new int[10]; while (num > 0) { // Get remainder int index = num % 10; digits[index]++; num /= 10; } return digits; } public static void main(String[] args) { int num = 38293367; int maxNum = findMaxNumberFromDigits(num); System.out.println("Maximum number : " + maxNum); } } - Vishal Naik March 24, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package com.javafries.number;
 
/**
* @author vishal.naik
*/
 
public class MaximumNumberProblem {
 
    private static int findMaxNumberFromDigits(int num) {
        int[] digits = getDigitsInSortedOrder(num);
 
        int maxNum = 0;
        for (int index = digits.length - 1; index >= 0; index--) {
 
            int frequency = digits[index];
            while (frequency > 0) {
                maxNum = maxNum * 10 + index;
                frequency--;
            }
        }
        return maxNum;
    }
 
    private static int[] getDigitsInSortedOrder(int num) {
        int[] digits = new int[10];
 
        while (num > 0) {
            // Get remainder
            int index = num % 10;
            digits[index]++;
            num /= 10;
        }
        return digits;
    }
 
    public static void main(String[] args) {
        int num = 38293367;
        int maxNum = findMaxNumberFromDigits(num);
        System.out.println("Maximum number : " + maxNum);
    }
}

- vishal naik March 24, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

As digits are in 0-9 ..so can we use count sort or radix sort ??
time complexity O(n)..

- jimmy March 24, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

as digits or array elements are in array ..
so can we use count sort ??
time complexity O(n) :P

- vsingla160 March 24, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Count sort in Java:

public int maxInt(int n){
    int[] count = new int[10];
    
    while (n > 0){
        int d = n % 10;
        count[d]++;
        n /= 10;
    }

    int max = 0;

    for (int d = 9; d >= 0; d--){
        for (int k = 0; k < count[d]; k++){
            max *= 10;
            max += d;
        }
    }

    return max;
}

- inucoder March 24, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class MaxNumberFromDigits{
  
  public static void main(String[] args){
  	
		String s = "8754365";
		int[] digits = new int[10];
		char[] max = new char[s.length()];
		
		for(int i=0; i<s.length(); i++){
			int value = Character.getNumericValue(s.charAt(i));
			System.out.println(value);
			digits[value] += 1;
		}
		
		int digitsLen = digits.length;
		int k=0;
		for(int i=digitsLen-1; i>=0; i--)
			for(int j=0; j<digits[i]; j++){
				max[k++] = String.valueOf(i).charAt(0);
			}
		
		String maxS = new String(max);
 		System.out.println(maxS);
	}

}

- Zeeshan Ansari March 25, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/*
 * Given "input" digits, spits out the largest valued number that can be formed
 * using those digits.
 */
int main()
{
	int input;
	int output = 0;
	int arr[10];
	int i;

	printf("Enter the digits: ");
	scanf("%d", &input);

	for (i = 0; i < 10; i++) {
		arr[i] = 0;
	}

	while (input != 0) {
		arr[input % 10]++;
		input /= 10;
	}

	for (i = 9; i >= 0; i--) {
		while (arr[i] != 0) {
			output = output * 10 + i;
			arr[i]--;
		}
	}

	printf("\nOutput: %d\n", output);
	return 1;
}

- Gotham March 26, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/*
 * Given "input" digits, spits out the largest valued number that can be formed
 * using those digits.
 */
int main()
{
	int input;
	int output = 0;
	int arr[10];
	int i;

	printf("Enter the digits: ");
	scanf("%d", &input);

	for (i = 0; i < 10; i++) {
		arr[i] = 0;
	}

	while (input != 0) {
		arr[input % 10]++;
		input /= 10;
	}

	for (i = 9; i >= 0; i--) {
		while (arr[i] != 0) {
			output = output * 10 + i;
			arr[i]--;
		}
	}

	printf("\nOutput: %d\n", output);
	return 1;
}

- Gotham March 26, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/*
 * Given "input" digits, spits out the largest valued number that can be formed
 * using those digits.
 */
int main()
{
	int input;
	int output = 0;
	int arr[10];
	int i;

	printf("Enter the digits: ");
	scanf("%d", &input);

	for (i = 0; i < 10; i++) {
		arr[i] = 0;
	}

	while (input != 0) {
		arr[input % 10]++;
		input /= 10;
	}

	for (i = 9; i >= 0; i--) {
		while (arr[i] != 0) {
			output = output * 10 + i;
			arr[i]--;
		}
	}

	printf("\nOutput: %d\n", output);
	return 1;
}

- Gotham March 26, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

10 digits, 10 buckets

- bucket sort April 14, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int main()
{
int n,j = 0; int hashtab[10];
bool NEG_VAL = false;
printf("Enter the number = ");
scanf("%d", &n);
if(n < 0)
{
NEG_VAL = true;
n = n * -1;
}
for(j=0;j<10;j++)
{
hashtab[j] = 0;
}
while(n > 0)
{
j = n%10;
hashtab[j] = hashtab[j] + 1;
n = n/10;
}

if(NEG_VAL)
{
printf("Highest number formed = -");
for(j=1;j<=9;j++)
{
while(hashtab[j] > 0)
{
printf("%d", j);
--hashtab[j];
}
}
printf("\n");
}
else
{
printf("Highest number formed = ");
for(j=9;j>=0;j--)
{
while(hashtab[j] > 0)
{
printf("%d", j);
--hashtab[j];
}
}
}
printf("\n");
return 0;
}

- Rndp13 April 21, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.*;
class MaxNo1 {

static void findMax(int no)
{

String str="";
ArrayList<Integer> al=new ArrayList<Integer>();

int temp=0,count=0;

while(no>0)
{
temp=no%10;
no=no/10;
al.add(temp);

}
Collections.sort(al);

for(int a:al)
{
str=a+str;
}

System.out.println(str);

}
public static void main(String[] args)
{
int no=81098374; //9874310

findMax(no);
}
}

- Abhimanyu Chopra April 22, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.*;
public class MaxNo {

static void findMax(int no)
{
LinkedHashMap<Integer,Integer> map=new LinkedHashMap<Integer,Integer>();


map.put(1,0);
map.put(2,0);
map.put(3,0);
map.put(4,0);
map.put(5,0);
map.put(6,0);
map.put(7,0);
map.put(8,0);
map.put(9,0);
map.put(0,0);

int temp=0,count=0;

while(no>0)
{
count=0;
temp=no%10;
no=no/10;
count=map.get(temp);
map.put(temp, ++count);
}

Set<Integer> set=map.keySet();

String str="";

for(int i:set)
{
count=0;
count=map.get(i);

while(count>0)
{
str=i+str;
--count;
}
}
}
public static void main(String[] args)
{
int no=123150;

findMax(no);
}
}

- Abhimanyu Chopra April 22, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.*;
public class MaxNo {

static void findMax(int no)
{
LinkedHashMap<Integer,Integer> map=new LinkedHashMap<Integer,Integer>();


map.put(0,0);
map.put(1,0);
map.put(2,0);
map.put(3,0);
map.put(4,0);
map.put(5,0);
map.put(6,0);
map.put(7,0);
map.put(8,0);
map.put(9,0);

int temp=0,count=0;

while(no>0)
{
count=0;
temp=no%10;
no=no/10;
count=map.get(temp);
map.put(temp, ++count);
}

Set<Integer> set=map.keySet();

String str="";

for(int i:set)
{
count=0;
count=map.get(i);

while(count>0)
{
str=i+str;
--count;
}
}
}
public static void main(String[] args)
{
int no=123150;

findMax(no);
}
}

- Abhimanyu Chopra April 22, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class SolutionMaximumNumberFormed {
    public long solution(long num) {
        if (num == 0) {
            return 0;
        }
        long temp = num;
        if(num < 0) {
            temp = temp * -1;
        }
        int[] arr = new int[10];
        while (temp != 0) {
            System.out.print("TEMP: " + (int) (temp % 10) + "\t BEFORE: " + arr[(int) (temp % 10)]);
            int x = (int) (temp % 10);
            arr[x] = arr[x] * 10 + x;
            System.out.print("\t AFTER: " + arr[x]);
            System.out.println();
            temp = temp / 10;
        }
        int start = arr.length - 1;
        int end = 0;
        if(num < 0) {
            start = 0;
            end = arr.length;
        }
        String out = "";
        while(start != end) {
            if (arr[start] != 0) {
                out = out + arr[start];
            }
            if(num < 0) {
                start++;
            } else {
                start--;
            }
        }
        
        if(num < 0) {
            return Long.parseLong(out) * -1;
        }
        return Long.parseLong(out);
    }
}

public class MaximumNumberFromedFromDigits {

    public static void main(String[] args) {
        SolutionMaximumNumberFormed mSol = new SolutionMaximumNumberFormed();
        System.out.println(mSol.solution(87543655));
        System.out.println(mSol.solution(0));
        System.out.println(mSol.solution(123456789));
        System.out.println(mSol.solution(999999999));
        System.out.println(mSol.solution(889922443));
        System.out.println(mSol.solution(-889922443));
    }
}

- Scorpion King April 25, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sort the number in descending order.

public static int sortIntDescending(int num) {
		String sort = "";
		for (int l = 9; l >= 0; l--) {
			int rem = num % 10;
			int tx = num / 10;
			while (tx != 0) {
				if (rem == l)
					sort+=String.valueOf(rem);	
				rem = tx % 10;
				tx = tx / 10;
			}
		}
		return Integer.valueOf(sort);
	}

- Mukesh Dua August 02, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int sortIntDescending(int num) {
String sort = "";
for (int l = 9; l >= 0; l--) {
int rem = num % 10;
int tx = num / 10;
while (tx != 0) {
if (rem == l)
sort+=String.valueOf(rem);
rem = tx % 10;
tx = tx / 10;
}
}
return Integer.valueOf(sort);
}

- Mukesh Dua August 02, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int sortIntDescending(int num) {
String sort = "";
for (int l = 9; l >= 0; l--) {
int rem = num % 10;
int tx = num / 10;
while (tx != 0) {
if (rem == l)
sort+=String.valueOf(rem);
rem = tx % 10;
tx = tx / 10;
}
}
return Integer.valueOf(sort);
}

- Mukesh Dua August 02, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int sortIntDescending(int num) {
		String sort = "";
		for (int l = 9; l >= 0; l--) {
			int rem = num % 10;
			int tx = num / 10;
			while (tx != 0) {
				if (rem == l)
					sort+=String.valueOf(rem);	
				rem = tx % 10;
				tx = tx / 10;
			}
		}
		return Integer.valueOf(sort);
	}

- Mukesh Dua August 02, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int sortIntDescending(int num) {
String sort = "";
for (int l = 9; l >= 0; l--) {
int rem = num % 10;
int tx = num / 10;
while (tx != 0) {
if (rem == l)
sort+=String.valueOf(rem);
rem = tx % 10;
tx = tx / 10;
}
}
return Integer.valueOf(sort);
}

- Mukesh Dua August 02, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int sortIntDescending(int num) {
String sort = "";
for (int l = 9; l >= 0; l--) {
int rem = num % 10;
int tx = num / 10;
while (tx != 0) {
if (rem == l)
sort+=String.valueOf(rem);
rem = tx % 10;
tx = tx / 10;
}
}
return Integer.valueOf(sort);
}

- Mukesh Dua August 02, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Simple Ruby solution

def largest_int x
x.to_s.split("").sort.reverse.join.to_i
end

- Vince August 14, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Ruby -

def largest_int x
  x.to_s.split("").sort.reverse.join.to_i
end

- Vince August 14, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Worked out the same answer as Vince in Ruby.

def max_num_formed(num)
puts num.to_s.split("").sort.reverse.join("").to_i
end

max_num_formed(8754365)

# => 8765543

- Andrew September 06, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I worked out the same answer as Vince.

Agree, very simple...

def max_num_formed(num)
  puts num.to_s.split("").sort.reverse.join("").to_i
end

max_num_formed(8754365) 

# => 8765543

- Andrew September 06, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I worked out the same answer as Vince.

Agree, very simple...

def max_num_formed(num)
  puts num.to_s.split("").sort.reverse.join("").to_i
end

max_num_formed(8754365) 

# => 8765543

- Andrew September 06, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/*
 * Given a Integer, find the maximum number that can be formed from the digits. 
Input : 8754365 
output : 8765543 

I told solution in nlogn. He asked me optimize further.


 */
public class Amazon_SDET_maximum_number {

	public static void main(String[] args) {
		int[] array = new int[10];
		int n = 866487256;
		int temp = n;
		int rem = 0;
		while(temp > 0){
			rem = temp%10;
			temp = temp/10;
			array[rem]++;
		}
		String result = "";
		for(int i = array.length-1; i>=0; i--){
			while(array[i] >= 1){
				result = result + i;
				array[i]--;
			}
		}
		System.out.println(result);
	}
}

- sravale December 01, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#!/usr/bin/python
import sys
number = 8754365
l = [0] * 10
while number > 0 :
    l[number%10] +=1
    number /=10
for j in reversed(range(10)):
    num = l[j]
    for i in range(0,num):
        sys.stdout.write(str(j))

- gautam December 17, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#!/usr/bin/python
import sys
number = 8754365
l = [0] * 10
while number > 0 :
    l[number%10] +=1
    number /=10
for j in reversed(range(10)):
    num = l[j]
    for i in range(0,num):
        sys.stdout.write(str(j))

- gautam December 17, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void inter()
{
int a=123985968;
String str = Integer.toString(a);
char []b= str.toCharArray();
Arrays.sort(b);
for(int i = b.length - 1; i >= 0; i--) System.out.print(b[i]);
System.out.println();

}

- Anonymous February 20, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void inter()
    {
    int a=123985968;
    String str = Integer.toString(a);
       char []b=	str.toCharArray();
       Arrays.sort(b);
       for(int i = b.length - 1; i >= 0; i--) System.out.print(b[i]);
       System.out.println();

}

- Anonymous February 20, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int a=123985968;
String str = Integer.toString(a);
char []b= str.toCharArray();
Arrays.sort(b);
for(int i = b.length - 1; i >= 0; i--) System.out.print(b[i]);
System.out.println();

- Anonymous February 20, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int a=123985968;
    String str = Integer.toString(a);
       char []b=	str.toCharArray();
       Arrays.sort(b);
       for(int i = b.length - 1; i >= 0; i--) System.out.print(b[i]);
       System.out.println();

- Anonymous February 20, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int a=123985968;
String str = Integer.toString(a);
char []b= str.toCharArray();
Arrays.sort(b);
for(int i = b.length - 1; i >= 0; i--) System.out.print(b[i]);
System.out.println();

- m February 20, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{
{
{
int a=123985968;
String str = Integer.toString(a);
char []b= str.toCharArray();
Arrays.sort(b);
for(int i = b.length - 1; i >= 0; i--) System.out.print(b[i]);
System.out.println();
}
}
}

- m February 20, 2018 | 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