Amazon Interview Question for Software Engineer / Developers


Team: Retail
Country: India
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
4
of 6 vote

public class SortingByAlphabeth {
    char[] color;

    public char[] getColor() {
        return color;
    }

    public void setColor(char[] color) {
        this.color = color;
    }

    public char[] run(char[] arr) {
        int lastSwapIndex = 0;
        for (int i = 0;i<color.length; i++) {
            char pivot = color[i];
            for (int j=0; j<arr.length; j++) {
              if (pivot==arr[j]) {
                  arr[j] = arr[lastSwapIndex];
                  arr[lastSwapIndex ] = pivot;
                  lastSwapIndex++;
              }
          }

        }
        return        arr;
    }

}
public class SortingByAlphabethTest {

    @org.junit.Test
    public void testSoring() throws Exception {
        SortingByAlphabeth sortingByAlphabeth = new SortingByAlphabeth
                ();
        char[] color = {'a', 'b', 'c', 'd', 'e', 'f', 'g'};
        char[] arr = {'f', 'a', 'b', 'b', 'a', 'a', 'a'};
        sortingByAlphabeth.setColor(color);
        char[] run = sortingByAlphabeth.run(arr);
        char[] expected = {'a','a','a','a','b','b', 'f'};
        assertEquals(new String(expected), new String(run));

    }
}

- Radim Pavlicek February 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

How about this input?
{'b,'a','a','a'}
it seems to me that you will swap 'b' once for each 'a' - 3 times

- Theo February 12, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I agree with that comment. I would also say that the conditions are conflicting:
2) Every element may repeat minimum 5 times [...]
3) 'a' can be swapped only 1 time [...]

I don't see how one can swap 'a' only one time if there are five randomly given 'a' instance.

Relaxing the number of swap constraint, the above solution is great.

- martin.pernollet February 27, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Use of color array :::
'a' can be swapped only 1 time, 'b' can be swapped max 2 times,'c' can be swapped 3 times ...... z can be swapped max 26 times.

if color array is {c,a,b,......z} , c can be swapped once , a can be swapped twice and b can be swapped thrice.. and so on

- Ravi Kumar February 06, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Sorry
But I am not able to understand your's conditions. Please provide one example which satisfy all the conditions

Thanks in advance :)

- NoMind February 06, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

didn't get the question, subscribing to the thread.

- S.Abakumoff February 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Code on java....
public class ArrangeColors {
public static void main(String []args){

char color[] = {'a','b','c','d'};
char arr[] = {'b','b','c','a','c','a','a','a','d','d','d','c','d','b','a','a','b','b','c','d'};
System.out.println("Before Swapping");
printArray(arr);
arr = sortArray(arr,color);
System.out.println("After Swapping");
printArray(arr);

}

private static char[] sortArray(char[] arr,char[] color){
int lastSwapIndex = 0;
for(int i= 0 ;i<color.length;i++){
int pivot = color[i];
int j = arr.length-1;
while(lastSwapIndex<=j){
if(arr[j]==pivot){
if(arr[lastSwapIndex]!=pivot){
swap(lastSwapIndex, j, arr);
lastSwapIndex += 1;
}
else{
while(lastSwapIndex<arr.length && arr[lastSwapIndex]==pivot){
lastSwapIndex += 1;
}
if(lastSwapIndex<=j){
swap(lastSwapIndex, j, arr);
lastSwapIndex += 1;
}
}
}
j--;
}
}
return arr;
}

private static void swap(int swapIndex,int j,char[] arr){
char temp = arr[swapIndex];
arr[swapIndex] = arr[j];
arr[j]=temp;
}

private static void printArray(char[] arr){
for(int i=0;i<arr.length;i++)
System.out.print(arr[i]+",");
System.out.println();
}
}

- Narender March 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

subscribing to thread

- Nitin Gupta February 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1st time, swap all 'a' s to their positions. 2nd, swap 'b's .... So each ‘a’ makes one swap, each ‘b’ makes at most 2 swaps, and so on . This will take O(n^2) time

- peng February 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nice! But what do I need this "color" array for?

- Chih.Chiu.19 February 07, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You mean to say that applying selection sort on given array.
Then there i dont find any use of that color array.

- praveen February 07, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

char[] sort(char[] color, char[] arr) {
int temp;
int ae=0;
int as=0;
int root=0;
while(root <arr.length) {
if(arr[ae]==color[as]){
temp=arr[ae];
arr[ae]=arr[as];
arr[as]=temp;
as++;
}
ae++;
}
root++;
ae=as;
return arr[];
}

- Stefan February 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This does not work --- try it yourself

- Chih.Chiu.19 February 07, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Code In 'C'

#include<stdio.h>


char color[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g'};
char arr[] = {'f', 'a', 'b', 'b', 'a', 'a', 'a','c','g','e'};
int length = sizeof(color)/sizeof(char);
int alength = sizeof(arr)/sizeof(char);
void sortArr()
{
int i ,j;
printf("\ncolor len:%d arr len:%d\n",length,alength);
int lastSwapIndex = 0;
for (i = 0;i<length; i++) {
char pivot = color[i];
for (j=0; j<alength; j++) {
if (pivot==arr[j]) {
arr[j] = arr[lastSwapIndex];
arr[lastSwapIndex ] = pivot;
lastSwapIndex++;
}

}

}
}


int main()
{

int i,j;
printf("Color arr===>\n");
for(i = 0; i<length;i++)
printf("%c ",color[i]);

printf("\n\nArr ===>\n");
for(i = 0;i<alength;i++)
printf("%c ",arr[i]);

sortArr();

printf("\n\nAfter Color arr===>\n");
for(i = 0; i<length;i++)
printf("%c ",color[i]);

printf("\n\nAfter Arr ===>\n");
for(i = 0;i<alength;i++)
printf("%c ",arr[i]);

printf("\n====END====\n\n");

}


OUTPUT::::
Color arr===>
a b c d e f g

Arr ===>
f a b b a a a c g e
color len:7 arr len:10


After Color arr===>
a b c d e f g

After Arr ===>
a a a a b b c e f g
====END====

- Anonymous February 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

"a can only be swapped one time, b 2 times, ......" this condition doesnot work well in your code!

- Anonymous February 13, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Conditions are not clear.

- codewarrior February 15, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <string>
#define LENGTH(chars) sizeof(chars)/sizeof(char)
using namespace std;

int main(int argc,char* argv[]){
char color[]={'a','b','c','d','e','f','g','h'};
char arr[]={'b','a','a','a','b','c','g','h','g','b'};
int targetIndex=0;
int colorIndex=0;
while(targetIndex<LENGTH(arr)&&colorIndex<LENGTH(color)){
int i=LENGTH(arr)-1;
while(i>=targetIndex){
if(arr[i]==color[colorIndex]){
if(arr[i]!=arr[targetIndex]){
cout<<"Swap:"<<arr[i]<<"("<<i<<") and "<<arr[targetIndex]<<"("<<targetIndex<<")"<<endl;
char temp=arr[targetIndex];
arr[targetIndex]=arr[i];
arr[i]=temp;
--i;
}
++targetIndex;
}
else --i;
}
++colorIndex;
}
for(int i=0;i<LENGTH(arr);++i){
cout<<arr[i]<<" ";
}
cout<<endl;
}

- stevenh47 February 23, 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