Amazon Interview Question for SDETs


Country: India
Interview Type: In-Person




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

Comparison should be based on appending one number at the from of other and vice versa.

#include <iostream>
#include <cmath>
#include <algorithm>

int numDigits(int num)
{
    return floor(log10(num)) + 1;
}

bool myCompare(int dig1, int dig2)
{
    int numDig1 = numDigits(dig1);
    int numDig2 = numDigits(dig2);

    int dig11 = dig1 * pow(10, numDig2) + dig2;
    int dig22 = dig2 * pow(10, numDig1) + dig1;

    if(dig22 > dig11)
    {
        return false;
    }

    return true;
}


int main()
{
    int array[] = {1, 5, 8, 19, 21, 9, 100};

    /// Biggest no: 9|8|5|21|19|1|100;
    /// Sort the list in a order, of their Most Significant Bit.

    int size = sizeof(array)/sizeof(int);
    std::sort(array, array+size, myCompare);
    for(int i=0; i < size; i++)
    {
        std::cout << array[i];
    }
    std::cout << std::endl;
}

- neebanv November 14, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Is it working???
I don't understand your logic... Check it again!

- Rajendra November 15, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

yes, great solution.

- zr.roman November 16, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

example
Input -->{54, 546, 548, 60},
output --> 6054854654

- poojaarora014 December 13, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Just reverse sort the group of numbers in case of single digits.

- Unknown factor November 14, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I am bit confused with the question. Isnt desc order sequence will yield in a desired result ? any examples to the above question

- ankushbindlish November 16, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Just sort the numbers in reverse lexicographic order. (Think of them as strings and sort them descending).

- MongoLikeCode November 16, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package com.learn;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class MaxNum {

	public static void main(String[] args) {	
		
		List<Integer> num = new ArrayList<Integer>();
		
		num.add(11);
		num.add(10);
		num.add(990);
		num.add(870);
		num.add(32);
		num.add(94);
		num.add(210);
		
		num.sort(new Comparator<Integer>() {
			@Override
			public int compare(Integer o1, Integer o2) {
				String o1str = o1.toString();
				String o2str = o2.toString();
				Integer returnint = 0;
				
				int len1 = o1str.length();
				int len2 = o2str.length();
				
				// if length of first digit is less than second
				if(len1<len2){
					int o2Int = new Integer(o2str.substring(0, len1));
					returnint = o2Int - o1;
				// if length of first digit is less than second	
				}else if(len1>len2){
					int o1Int = new Integer(o1str.substring(0, len2));
					returnint = o2 - o1Int;
				// if both the lengths are same	
				}else{
					returnint = o2-o1;
				}
									
				return returnint;
			}			
		});	
		
		for(int i=0;i<num.size();i++){
			//commas just to show the numbers
			System.out.print(num.get(i)+",");
		}	
	}

}

- Falco November 17, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

this code will not work sort is not a method of List class

- t November 29, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

package com.learn;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class MaxNum {

	public static void main(String[] args) {	
		
		List<Integer> num = new ArrayList<Integer>();
		
		num.add(11);
		num.add(10);
		num.add(990);
		num.add(870);
		num.add(32);
		num.add(94);
		num.add(210);
		
		num.sort(new Comparator<Integer>() {
			@Override
			public int compare(Integer o1, Integer o2) {
				String o1str = o1.toString();
				String o2str = o2.toString();
				Integer returnint = 0;
				
				int len1 = o1str.length();
				int len2 = o2str.length();
				
				// if length of first digit is less than second
				if(len1<len2){
					int o2Int = new Integer(o2str.substring(0, len1));
					returnint = o2Int - o1;
				// if length of first digit is less than second	
				}else if(len1>len2){
					int o1Int = new Integer(o1str.substring(0, len2));
					returnint = o2 - o1Int;
				// if both the lengths are same	
				}else{
					returnint = o2-o1;
				}
									
				return returnint;
			}			
		});	
		
		for(int i=0;i<num.size();i++){
			//commas just to show the numbers
			System.out.print(num.get(i)+",");
		}	
	}

}

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

package com.learn;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class MaxNum {

	public static void main(String[] args) {	
		
		List<Integer> num = new ArrayList<Integer>();
		
		num.add(11);
		num.add(10);
		num.add(990);
		num.add(870);
		num.add(32);
		num.add(94);
		num.add(210);
		
		num.sort(new Comparator<Integer>() {
			@Override
			public int compare(Integer o1, Integer o2) {
				String o1str = o1.toString();
				String o2str = o2.toString();
				Integer returnint = 0;
				
				int len1 = o1str.length();
				int len2 = o2str.length();
				
				// if length of first digit is less than second
				if(len1<len2){
					int o2Int = new Integer(o2str.substring(0, len1));
					returnint = o2Int - o1;
				// if length of first digit is less than second	
				}else if(len1>len2){
					int o1Int = new Integer(o1str.substring(0, len2));
					returnint = o2 - o1Int;
				// if both the lengths are same	
				}else{
					returnint = o2-o1;
				}
									
				return returnint;
			}			
		});	
		
		for(int i=0;i<num.size();i++){
			//commas just to show the numbers
			System.out.print(num.get(i)+",");
		}	
	}

}

- Anonymous November 17, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Wont work in this case 100, 10.
Need to update it with below statement

returnint = o2Int - o1 == 0? (o2str.length()<o1str.length()?1:-1):o2Int - o1;

- Mav023 November 18, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Just convert all ints to string, and use String comparator

import java.util.*;
public class MaxNumber {
	
	public static void main(String... args) {
		
		if (args.length == 0) {
			return;
		}
		
		int n = args.length;
		
		Integer[] ints = new Integer[n];
		for (int i = 0; i < n; i++) {
			ints[i] = Integer.parseInt(args[i]);
		}
		
		Arrays.sort(ints, new Comparator<Integer>() {
			@Override
			public int compare(Integer o1, Integer o2) {
				return o2.toString().compareTo(o1.toString());
			}
		});
		
		for (int i = 0; i < n; i++) {
			System.out.print(ints[i]);
		}
		
		System.out.println();
	
	}
}

- madi.sagimbekov November 17, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This won't work correctly for this - 100, 10

- Mav023 November 18, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

struct Comparator{
        bool operator()(string & a, string & b){
            return (a + b) > (b + a);
        }
    } comparator;

    string largestNumber(vector<int>& nums) {
        
        vector<string> strs;
        for(int num : nums){
            strs.push_back(to_string(num));
        }
        
        sort(strs.begin(), strs.end(), comparator);
        
        string res;
        for(string s : strs){
            res += s;
        }
        
        return res[0] == '0' ? "0" : res;
    }

- PoWerOnOff November 18, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

sdfb

- rfbw b November 23, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def lesser ( str1,str2 ):
    if len(str1) > len(str2) :
        str2 += str1
    else :
        str1 += str2
    return str1 < str2
for i in range(1,len(numbers)) :
    for j in range(0,len(numbers)-i) :
        if lesser(numbers[j],numbers[j+1]) :
            temp = numbers[j]
            numbers[j] = numbers[j+1]
            numbers[j+1] = temp
numb = ""
for num in numbers :
    numb += num
print numb

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

def lesser ( str1,str2 ):
    if len(str1) > len(str2) :
        str2 += str1
    else :
        str1 += str2
    return str1 < str2
for i in range(1,len(numbers)) :
    for j in range(0,len(numbers)-i) :
        if lesser(numbers[j],numbers[j+1]) :
            temp = numbers[j]
            numbers[j] = numbers[j+1]
            numbers[j+1] = temp
numb = ""
for num in numbers :
    numb += num
print numb

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

def lesser ( str1,str2 ):
    if len(str1) > len(str2) :
        str2 += str1
    else :
        str1 += str2
    return str1 < str2
for i in range(1,len(numbers)) :
    for j in range(0,len(numbers)-i) :
        if lesser(numbers[j],numbers[j+1]) :
            temp = numbers[j]
            numbers[j] = numbers[j+1]
            numbers[j+1] = temp
numb = ""
for num in numbers :
    numb += num
print numb

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

def lesser ( str1,str2 ):
    if len(str1) > len(str2) :
        str2 += str1
    else :
        str1 += str2
    return str1 < str2
for i in range(1,len(numbers)) :
    for j in range(0,len(numbers)-i) :
        if lesser(numbers[j],numbers[j+1]) :
            temp = numbers[j]
            numbers[j] = numbers[j+1]
            numbers[j+1] = temp
numb = ""
for num in numbers :
    numb += num
print numb

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

def lesser ( str1,str2 ):
    if len(str1) > len(str2) :
        str2 += str1
    else :
        str1 += str2
    return str1 < str2
for i in range(1,len(numbers)) :
    for j in range(0,len(numbers)-i) :
        if lesser(numbers[j],numbers[j+1]) :
            temp = numbers[j]
            numbers[j] = numbers[j+1]
            numbers[j+1] = temp
numb = ""
for num in numbers :
    numb += num
print numb

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

def lesser ( str1,str2 ):
    if len(str1) > len(str2) :
        str2 += str1
    else :
        str1 += str2
    return str1 < str2
for i in range(1,len(numbers)) :
    for j in range(0,len(numbers)-i) :
        if lesser(numbers[j],numbers[j+1]) :
            temp = numbers[j]
            numbers[j] = numbers[j+1]
            numbers[j+1] = temp
numb = ""
for num in numbers :
    numb += num
print numb

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

def lesser ( str1,str2 ):
    if len(str1) > len(str2) :
        str2 += str1
    else :
        str1 += str2
    return str1 < str2
for i in range(1,len(numbers)) :
    for j in range(0,len(numbers)-i) :
        if lesser(numbers[j],numbers[j+1]) :
            temp = numbers[j]
            numbers[j] = numbers[j+1]
            numbers[j+1] = temp
numb = ""
for num in numbers :
    numb += num
print numb

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

def lesser ( str1,str2 ):
    if len(str1) > len(str2) :
        str2 += str1
    else :
        str1 += str2
    return str1 < str2
for i in range(1,len(numbers)) :
    for j in range(0,len(numbers)-i) :
        if lesser(numbers[j],numbers[j+1]) :
            temp = numbers[j]
            numbers[j] = numbers[j+1]
            numbers[j+1] = temp
numb = ""
for num in numbers :
    numb += num
print numb

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

#include <iostream>
#include <cmath>
#include <algorithm>

using namespace std;


int main()
{
	int i,j,k,temp,c,n,min,op,no,temp_no[20],pos[10],in;
    int array[10];
int nod[10];
j=0;
cout<<"enter the amount of number's you want";
cin>>n;
for(i=0;i<n;i++)
cin>>n;
for(i=0;i<n;i++)
pos[i]=i;

for(i=0;i<n;i++)
{
	c=0;
	temp=array[i];
	while(temp!=0)
	{
		c++;
		temp=temp/10;
	}
	nod[j]=c;
	j++;
}
   
   min=nod[0];
   for(k=0;k<n;k++)
   {
   	if(min>nod[k])
   	min=nod[k];
   }
   
   for(i=0;i<n;i++)
   {
   	op=nod[i]-min;
   	no=array[i];
   	  for(k=0;k<op;k++)
   	  {
   	  	no=no/10;
		 }
   	temp_no[i]=no;
   	
   }
   
   for(k=0;k<n-1;k++)
   {
   	for(j=0;j<n-k-1;j++)
   	{
   		if(temp_no[j]<temp_no[j+1])
   		{
   			temp=temp_no[j];
   			temp_no[j]=temp_no[j+1];
   			temp_no[j+1]=temp;
   	
	   	temp=pos[j];
		   pos[j]=pos[j+1];
		   pos[j+1]=temp;
		   }
	   }
   }
   
   
   
   for(k=0;k<n;k++)
   {
   	in=k;
   	for(j=k+1;j<n;j++)
   	{
   			if(temp_no[in]==temp_no[j])
   			{
   				if(array[pos[in]] < array[pos[j]])
   				{	   
					   temp=pos[in];
					   pos[in]=pos[j];
					   pos[j]=temp;
				   }
			   }
		   
	   }
   }
  for(k=0;k<n;k++)
  {
   cout<<array[pos[k]];
  }
   
   
}

- ashish December 22, 2015 | 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