Amazon Interview Question for Software Engineer / Developers


Country: India
Interview Type: Written Test




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

Sorry for incomplete question, you need to give the number of swaps required.

- singh May 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
3
of 7 vote

* parse the string from right to left
* when doing so keep track of the number of zeros
* when you encounter a 1, add the current count of zeros to total counts

At the end of the loop, total counts will have the minimum number of swaps needed

- Bloodhawk May 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

this is the implementation of above logic

#include<stdio.h>

int main()
{
int arr[]={0,0,0,1,0,1,1,0,1};

int countZero = 0;
int arrSize = 9,i,j,temp,k=0;
int swap=0;


for(i=(arrSize-1);i>=0;i--)
{
   if(arr[i] == 0)
   {
    countZero++;
   }else
   {
      for(j=i,k=0;k<countZero;j++,k++)
      {
          swap++; 
          temp = arr[j];
          arr[j] = arr[j+1];
          arr[j+1] = temp;
      }
   }

}

for(i=0;i<arrSize;i++)
 printf("\t%d",arr[i]);

printf("\nSwaps = %d",swap);

return 0;
}

- student June 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

please test it and if it fails then please share the test case

- student June 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

can u explain it ....

- spider June 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@spider : this might help u understand

*parse the string from right to left
* when doing so keep track of the number of zeros(am using countZero variable for that)
* when you encounter a 1, add the current count of zeros to total counts (am doing it using for loop in else condition )

At the end of the loop, total counts will have the minimum number of swaps needed

- student June 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

The most efficient way is only when we are swapping only 0<->1 (not 0<->0 and not 1<->1)
So we have to count "1" on the left side of each "0".

public int countMoves(String stext) {
		int oneCount = 0;
		int result = 0;
		char[] ch = stext.toCharArray();
		for (int i = 0; i < ch.length; ++i) {
			if (ch[i] == '1') {
				oneCount++;
			} else {
				result += oneCount;
			}
		}
		return result;
	}

Complexity = O(n)

- sm.pavlenko February 01, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static String movefront(String stext){
int end=0;
char [] ch=stext.toCharArray();
for (int i=0;i<ch.length;++i){
if (ch[i]<='0'){
ch[end]=ch[i];
end++;
}
}

for (int j=end;j<ch.length;++j){
ch[j]='1';
}

return new String(ch,0,ch.length);
}

- Scott May 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

swapping is permitted only with adjacent elements

- Anonymous May 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can I Just Count the # of '0' and '1' ?
without using any swapping.

- NGloom May 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes you can, the only constraint is that swapping will be always allowed with the adjacent elements.

- singh May 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

isn't it the bubble sort algorithm?

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

Step 1 : Count number of 1s

for(count=0;n>0;count++) {
          n&=(n-1);    }

Complexity worst O(n)
Step 2: add number of 1s to a new number

n = ( 1<<(count +1) - 1);

Complexity O(1) : constant time

- Ashutosh May 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

u have assumed string to be a int bit sequence.
what if string length > sizeof(int)*8

- shani May 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

As mentioned by NGloom, we can just count the number of 0s during the first pass - O(n) and in the second pass set those many elements from the start as 0. the rest would be set as 1. We don't even need to swap.

- vijayvenkatesans May 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

question if not how you can give the output, its about your process. so no, what you are saying wont be valid. you 'need' to swap adjacent values only you get the desired output

- Hill Billy May 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

the most efficient way i can think of... thinking i NEED to swap is a modified bubble sort:

int swapsDone=0;
for ( int i=0; i<n-swapsDone; i ++ )
     if ( ch[i]=='1' )
           for ( int j=i; j< n-swapsDone-1; j++ ) 
           {
                 swap ( ch[j+1], ch[j] );
                  swapsDone++;
            }

// i don't think i need to explain the code

- alexandru.doro May 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

isn't this bubble sort?

- vijayvenkatesans May 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

well yeah! i told you its a modified bubble sort. It has to be one since you can only swap with your neighbor.

- alexandru.doro May 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

can you explain how you arrived at this ?

- amz May 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

well it's your typical bubble sort, just that knowing you only have 2 values.. you can do a lot less tests:). You dont have to swap 0-s and you dont have to take each 1 to the last position. ez!

- alexandru.doro May 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I agree with this answer. I don't think it's possbile to do better than bubble sort if you're limited to doing everything only through swaps and are further limited to only adjacent swaps.

- Anonymous May 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

If we want to only count the number of swaps needed, we can do much better than simulating the entire O(n^2) algorithm. We can do this in linear time. A 1 in position k (0-indexed) contributes k - x swaps, where x is the number of 1s that came before position k. As we go through the array, we'll keep track of the number of 1s we've seen so far so that the number is readily available for calculations.

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

what i think is there would be swaps amongs 1s also... which i guess we can avoid.... we dont need to perform swap among adjacent 1's

correct me if i am wrong

this is the implementation of above logic

#include<stdio.h>

int main()
{
int arr[]={0,0,0,1,0,1,1,0,1};

int countZero = 0;
int arrSize = 9,i,j,temp,k=0;
int swap=0;


for(i=(arrSize-1);i>=0;i--)
{
   if(arr[i] == 0)
   {
    countZero++;
   }else
   {
      for(j=i,k=0;k<countZero;j++,k++)
      {
          swap++; 
          temp = arr[j];
          arr[j] = arr[j+1];
          arr[j+1] = temp;
      }
   }

}

for(i=0;i<arrSize;i++)
 printf("\t%d",arr[i]);

printf("\nSwaps = %d",swap);

return 0;
}

- student June 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

i think that the question has changed .......now we have to find min no. of swaps.

string s=00101. .....ans=1;
string s=001101.....ans=2;
string s=0011001......ans=4;

bool flag1=0,flag2=0;
for(int i=0,j=n-1;i<n,j>=0;i++,j--)//n =s.length();
{
if(s[i]=='0' && flag==1)
count1++;

if(s[j]=='1' && flag2==1)
count2++;

if(s[i]=='1')
flag=1;
if(s[j]=='0')
flag2=1;
}

ans=count1*count2;

this show that.........................

- Manish Sharma May 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static int swapSort(String s) {
int i=0;
int count = 0;
int swapcount = 0;
char[] ch = s.toCharArray();
while(i<s.length()){
if(ch[i]== '1'){
count++;
i++;
}else{
swap(ch,i,count);
swapcount = swapcount+count;
if(count==0)
i++;
count=0;
}
}
return swapcount;
}

- Bhargava May 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

char[] str = "01010101001".ToCharArray();
for (int i = 0; i < str.Length; i++) {
if (str[i] == '0') {
int j = i;
while (j > 0 && str[j - 1] != '0') {
char temp = str[j];
str[j] = str[j - 1];
str[j - 1] = temp;
j--;
}
}
}
Console.WriteLine(str);

- Senthil May 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

char[] str = "01010101001".ToCharArray();
for (int i = 0; i < str.Length; i++) {
if (str[i] == '0') {
int j = i;
while (j > 0 && str[j - 1] != '0') {
char temp = str[j];
str[j] = str[j - 1];
str[j - 1] = temp;
j--;
}
}
}
Console.WriteLine(str);

- Senthil May 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

char[] str = "01010101001".ToCharArray();
for (int i = 0; i < str.Length; i++) {
if (str[i] == '0') {
int j = i;
while (j > 0 && str[j - 1] != '0') {
char temp = str[j];
str[j] = str[j - 1];
str[j - 1] = temp;
j--;
}
}
}
Console.WriteLine(str);

- Senthil May 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void Swap (char *str) {
    if (str == NULL) return;
    
    int flag = 1, i = 0;
    
    /* Flag in this while loop tells us if we did a swap in the
     * last pass. If we did, we go over the string again. If we
     * did not see the need to do a swap in the last pass,
     * then flag is zero and we are done.
     */
    while (flag == 1) {
    
        flag = 0;
        i = 0;
        
        while ( str[i + 1] ) {
                
            if (str[i] == '1' && str[i + 1] == '0') {
                str[i] = '0';
                str[i + 1] = '1';
                flag = 1;
                i = i + 2; /* Jump two characters ahead */
            }
            else i++; /* Jump one */
        }
    }
}

- Nitin May 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

while ( str[i+1] ) means while ( str[i+1] != 0 ) which means ... you will not do anything if on a position there will be a 0 which is not good at all... see my solution above

- alexandru.doro May 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Count the number of '0'(z) and '1'(o) in 1 parse. In second parse fill z '0' in beginning followed by o '1' is end

- Anonymous May 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

thats not swapping !

- alexandru.doro May 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think the algorithm can be like this...
First go on parsing the string until you get '1' as soon as you reach '1' now search for '0' and count the number of places from '1' to '0' that will be the number of moves for that '1' shifting so many places. similarly count for the remaining.

- rag May 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

To solve the problem, we can have two pointers, one beg, and another end. Then the algorithm could be something like this :
1) Keep incrementing beg until 1 is found
2) Keep decrementing end until 0 is found.
3) flip bit at beg, flip bit at end (or swap, but just flipping would work)
4) stop when beg > end

pseudocode :

for (beg = -1, end = size; beg > end;)
    while(bin_array[++beg] == '0');
    while(bin_array[--end] == '1');
    flip(bin_array[beg]);
    flip(bin_array[end]);

- Anonymous May 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

In the loop, a check can be put for not continuing if beg > end :

if(beg < end)
    flip(bin_array[beg])
    flip(bin_array[end])

- Anonymous May 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Correction : And, the for loop condition also has to be beg < end

- Anonymous May 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

dude you can sort only adjacent elements. beg and end are not always adjacent.

- Anonymous May 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Linear time solution to find minimum no. of swaps required

public static int min_swaps(int a[]){
		int min_swaps = 0;
		
		//Find no.0f 1's
		int no_of_ones = 0;
		for(int lp=0;lp<a.length;lp++){
			if(a[lp]==1){
				no_of_ones++;
			}
		}
		
		int position = a.length;
		for(int lp=a.length;lp>=1;lp--){
			if(a[lp-1]==1){
				min_swaps+= (position - lp);
				position--;
			}
		}
		
		return min_swaps;
	}

- Anonymous May 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Linear time solution to find minimum no. of swaps

public static int min_swaps(int a[]){
		int min_swaps = 0;
		
		//Find no.0f 1's
		int no_of_ones = 0;
		for(int lp=0;lp<a.length;lp++){
			if(a[lp]==1){
				no_of_ones++;
			}
		}
		
		int position = a.length;
		for(int lp=a.length;lp>=1;lp--){
			if(a[lp-1]==1){
				min_swaps+= (position - lp);
				position--;
			}
		}
		
		return min_swaps;
	}

- praveenkumar.chengi May 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int swapmin(char *ch)
{

        int n = strlen(ch);
        int i = 0, sc = 0, oc = 0;
        while(ch[i])
        {
                if(ch[i] == '1')
                        oc++;
                else if(ch[i] == '0')
                {
                        sc += oc ;
                }
                ++i;
        }
        return sc;

}

- Abs May 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

parse the string from right to left
* when doing so keep track of the number of zeros
* when you encounter a 1, add the current count of zeros to total counts

At the end of the loop, total counts will have the minimum number of swaps needed

what i think is there would be swaps amongs 1s also... which i guess we can avoid.... we dont need to perform swap among adjacent 1's

correct me if i am wrong

this is the implementation of above logic

#include<stdio.h>


int main()
{
int arr[]={0,0,0,1,0,1,1,0,1};



int countZero = 0;
int arrSize = 9,i,j,temp,k=0;
int swap=0;



for(i=(arrSize-1);i>=0;i--)
{
   if(arr[i] == 0)
   {
    countZero++;
   }else
   {
      for(j=i,k=0;k<countZero;j++,k++)
      {
          swap++; 
          temp = arr[j];
          arr[j] = arr[j+1];
          arr[j+1] = temp;
      }
   }



}



for(i=0;i<arrSize;i++)
 printf("\t%d",arr[i]);



printf("\nSwaps = %d",swap);



return 0;
}

- student June 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class BitSwapping {
	
	String retStr = new String();
	public int swapCtr =0;
	
	public void arrangeBits(String inpStr){
		int pos;
		char temp,temp1;
		String str2;
		StringBuffer i_strB = new StringBuffer(inpStr);
		StringBuffer strB = new StringBuffer();
		
		//Look for 10 in the input
		//Need to swap until there are no more 10s in the input
		while (i_strB.indexOf("10") > 0){
			pos = i_strB.indexOf("10");
			temp = i_strB.charAt(pos); 
			temp1 = i_strB.charAt(pos+1); 
			
			str2 = i_strB.substring(pos+2); 
			strB = new StringBuffer(i_strB.substring(0,pos));
			strB.append(temp1); 
			strB.append(temp);
			strB.append(str2);
			i_strB = strB; 
			swapCtr++;
		}

		MyPr.pln("printing output");
		MyPr.pln("===============");
		MyPr.pln(strB.toString());
		MyPr.pln("Count of swaps "+swapCtr);
	}

	public static void main(String[] args){
		
		String str = "0001111000101011";
		BitSwapping bSwap = new BitSwapping();
		bSwap.arrangeBits(str);

	}
}

- m80_m8 June 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>

main()
{
int arr[8]={0,0,0,0,1,0,1,0};
//printf("enter array 8 elemenets(only 0/1):");
//scanf();
int i,j,k,zc=0,swap=0,tmp;

for(i=7;i>=0;i--)
{
if(arr[i]==0)
zc++;
else
{
for(j=i,k=0;k<zc;k++,j++)
{
swap++;
tmp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=tmp;
}
}
}
printf("no. of swaps:%d\n",swap);
for(i=0;i<8;i++)
printf("%d",arr[i]);
}

- Amit June 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Keep On parsing the string from left to right, and count the no of ones in a variable...
No_Of_Ones++
whenever a zero occurs
SwapNeeded+= No_Of_Ones

- Appy June 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Here is the code:

public int SwapCounter(String bits)
	{
		int SwapNeeded = -1; //Error
		if (bits != null && bits.length() >1)
		{
			int len = bits.length();
			SwapNeeded = 0;
			int NoOfOnes = 0;
			for(int i = 0; i< len; i++)
			{
				String bit = bits.substring(i, i+1);
				if(bit.equals("1")) 
				{
					NoOfOnes++;
				}
				else if(bit.equals("0")) 
				{
					SwapNeeded += NoOfOnes;
				}
				else
				{
					// Un supported character in the string
					SwapNeeded = -1; //ErrorCode
					System.out.println("Un supported character in the string");
					return SwapNeeded;
				}
			}
		}
		System.out.println("No of swaps needed for stream:"+bits +" are: " + SwapNeeded);
		return SwapNeeded;
	}

- Appy June 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Time complexity is O(n), Space complexity is O(1)

- Appy June 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>


int main()
{
	char data[]="1000101110001000";
	int len=0;
	int i,j;

	for(;data[len]!='\0';len++);

	i=0;
	j=len-1;

	do
	{
		while(data[i]!='1') i++;
		while(data[j]!='0') j--;
		if(i<=j)
		{
			data[i++]='0';
			data[j--]='1';
		}
	}while(i<=j);

	printf("%s\n",data);
		
	return 0;
}

- okaysh June 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The nth 0 at distance i from the start will have to be swapped (i-n) times to reach its correct destination.

Count and distance are natural numbers(start from 1)

Run through the string and sum (n-i) for each 0.

That sum is the number of swaps required.

- ViX July 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Dutch flag sort

- Anonymous August 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>

using namespace std;

int main(){
string s ;
cin>>s;
int no_of_zeros = 0, no_of_swaps = 0;

for(int i=s.size()-1;i > -1; i--){
    if(s[i] == '0')
        no_of_zeros++;
    else
        no_of_swaps += no_of_zeros;
}

cout<<no_of_swaps;
return 0;
}

- Anonymous October 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This works.

int a[] = {0,0,0,1,0,1,1,0,1};
    int size = sizeof(a)/sizeof(a[0]);
    int correctPosition = size-1;
    int minSwaps = 0;
    for(int i=size-1; i>=0; i--)
    {
            if(a[i] == 1)
            {
                    minSwaps += correctPosition-i;
                    correctPosition--;
            }
    }
    cout<<"MIN SWAPS = "<<minSwaps<<"\n\n";

- varun sharma March 30, 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