Amazon Interview Question for SDE1s


Country: India
Interview Type: In-Person




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

Can you explain you question clearly with example....??

- Rahul October 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In the code below, the function maxVal returns the maximum element in the array - one should be careful with the overflow (it is used as a flag hence any +ve value not already an element of the array will do).

int b[]={5,0,3,4,-3,-8,-9,-2,7,-1,-6,10};
	int m=sizeof(b)/sizeof(b[0]);
	int flag=maxVal(b,m)+1; 
	for(i=0;i<m;i++)
	{
		if(b[i]==0)
		{
			b[i]=flag;
			break;

		}
	}
	for(i=0;i<m;i++)
	{
		if(b[i]*b[i+1]>0)
		{
			for(j=i+1;j<m;j++)
			{
				if(b[j]*b[j+1]<0)
				{
					int tmp=b[j];
					b[j]=b[j+1];
					b[j+1]=tmp;
					i--;
					break;
				}
			}
		}
	}

	for(i=0;i<m;i++)
	{
		if(b[i]==flag)
		{
			b[i]=0;
			break;

		}
	}

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

public void reorderArray(int[] arr)
{
	bool wasPositive = true; //was the last number positive?
	if(arr[0] < 0)
	{
		wasPositive = false;
	}
	for(int i = 1; i < arr.Length; i++)
	{
		//is number negative
		if(arr[i] < 0)
		{
			//if previous number is positive, then current negative number is in the correct spot. no need to do anything else besides updating the value of wasPositive
			if(wasPositive)
			{
				wasPositive = false;
				continue;	
			}
			else  //if previous number was negative too, keep searching the array for a positive number. If found, swap it with the current negative number.
			{
				int j = i;
				while( j < arr.Length && arr[j]  < 0)
					j++;
				if( j == arr.Length)
					break;
				else
				{
					int temp = arr[i];
					arr[i] = arr[j];
					arr[j] = arr[i];
					wasPositive = true;
				}
			}
	}

	else //If current number is positive
	{
		//if previous number is negative, then current positive number is in the correct spot. no need to do anything else besides updating the value of wasPositive
			if(!wasPositive)
			{
				wasPositive = true;
				continue;	
			}
			else  //if previous number was positive too, keep searching the array for a negative number. If found, swap it with the current positive number.
			{
				int j = i;
				while( j < arr.Length && arr[j]  >= 0)
					j++;
				if( j == arr.Length)
					break;
				else
				{
					int temp = arr[i];
					arr[i] = arr[j];
					arr[j] = arr[i];
					wasPositive = false;
				}
			}
	}

	//Print Reordered Array.
	Console.Write("{0} ", arr[0]);
	for(int i = 1; i < arr.Length; i++)
	{
		Console.Write("{0} ", arr[i]);
		if( i % 10 == 0)
			Console.WriteLine();
	}
}

- DrFrag October 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Your program changes the order of elements in the output

- Sreenivas Doosa October 14, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sreenivas, You are right. For the sake of clarity, the bug is
//BUG:
// input = { 1, 3, 5, 7, 9, -2, -4, -6, -8 };
// output = { 1, -2, 5, -4, 9, -6, 3, -8, 0, 7 } --- order of elements changes.
// expected output = {1, -2, 3, -4, 5, -6, 7, -8, 9}


The only way I can think of to insert an element in the correct position is by shifting other elements to the right.
eg: In first iteration of the example above,
elements of input array between and including index 1-4 will be shifted to index 2-5 and -2 will be inserted at index 1.

Anyone got a better solution?

- DrFrag October 14, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Also, arr[j] = temp; is the correct instruction in the code for swapping two numbers. This occurs in 2 places.

- DrFrag October 14, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

arrangeArray(int[] arr)
{
int nextPositive = -1;
int nextNegative = 1;
if(arr[0] < 0)
isPrevPositive = false;
else
isPrevPositive = true;
for(int i = 1; i < Arr.len; i++)
{
if((arr[i] < 0 && isPrevPositive) || (arr[i] >= 0 && !isPrevPositive) )
continue;
else
if(arr[i] < 0)
{
nextNegative = arr[i];
if(nextPositive ! = -1)
{arr[i] = nextPositve;
nextPositve = -1;
}
else
arr[i] = getNextPositive(arr, int i);
}
if(arr[i] >= 0)
{
nextPositive = arr[i];
if(nextNegative ! = 1)
{arr[i] = nextNegative;
nextNegative = 1;
}
else
arr[i] = getnextNegative(arr, int i);
}
}

}

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

int getnextNegative(int[] arr, int start)
{
for(int i = start; i <arr.len; i++)
{

if(arr[i] < 0)
return arr[i];
}
}

int getnextPositive(int[] arr, int start)
{
for(int i = start; i <arr.len; i++)
{

if(arr[i] >= 0)
return arr[i];
}
}

- Anonymous October 15, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I took the liberty to add comments to your code to make it more readable. Unfortunately, your approach will fail when there are more than 2 negative or positive numbers in a row. eg: int[] arr = { 1, 3, 5, 7, 9, -2, -4, -6, -8 };

public void arrangeArray(int[] arr)
    {
        int nextPositive = -1;
        int nextNegative = 1;

        bool isPrevPositive;
        
        if(arr[0] < 0)
            isPrevPositive = false;
        else
            isPrevPositive = true;
        

        for(int i = 1; i < arr.Length; i++)
        {
            //arr[i] is in the correct place...move along.
            if((arr[i] < 0 && isPrevPositive) || (arr[i] >= 0 && !isPrevPositive) )
                continue;
            //arr[i] is not in the correct place...
            else

                //prev was negative and current is also negative
                if(arr[i] < 0)
                {
                    //store current number in nextNegative...
                    nextNegative = arr[i];

                    //if nextPositive has a number for us from a previous save, 
                    //assign it to arr[i]
                    if (nextPositive != -1)
                    {
                        arr[i] = nextPositive;
                        nextPositive = -1;
                    }
                    
                    //if there's no number in nextPositive that we can use, 
                    //get the next positive number and assign it to arr[i]
                    else
                    {
                        //a positive number does not exist in the remaining array.
                        //if that's the case, everything is arranged correctly.
                        if (getNextPositive(arr, i) == -1)
                            break;
                        else
                            arr[i] = getNextPositive(arr, i);
                        
                    }
                        
                }
            //prev was positive and current is also positive.
            if(arr[i] >= 0)
            {
                //store current number in nextPositive.
                nextPositive = arr[i];

                //if nextNegative has a number for us from a previous save,
                //assign it to arr[i]
                if (nextNegative != 1)
                {
                    arr[i] = nextNegative;
                    nextNegative = 1;
                }
                //if there's no number in nextNegative that we can use, 
                //get the next negative number and assign it to arr[i]
                else
                {
                    //a negative number does not exist in the remaining array.
                    //If that's the case, everything is arranged correctly.
                    if (getnextNegative(arr, i) == 1)
                        break;
                    else
                        arr[i] = getnextNegative(arr, i);
                }
                    
            }
        }

- DrFrag October 15, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

## In perl
#!usr/bin/perl -w
use strict;
use warnings;

$|=1;
my $i = 0 ; my $j = 0 ;
my @array = qw(-1 -2 -3 -4 5 6 7 -8 9 -10 -11 12 );
my $pos = 0 ; my $neg = 0 ;
my @pos_array ;
my @neg_array ;

complete_array(\@array);

sub complete_array
{

my @array_num = @{$_[0]};
foreach (@array_num)
{
print $_ . " ";
}

}
print "\n\n" ;


foreach my $num(@array)
{
if($num > 0 )
{
push @pos_array ,$num ;
}
else
{
push @neg_array,$num ;
}
}
print "Positive array : ";
complete_array(\@pos_array);
print "\n\n" ;
print "Negative array : ";
complete_array(\@neg_array);
print "\n\n" ;

my $pos_num = @pos_array ;
my $neg_num = @neg_array;
my $min_num = &minimum($pos_num,$neg_num);

print "\n\nHere is our Min num : ".$min_num ."\n\n" ;
for ($i = 0, $j = 0; $i< $min_num ; $i++,$j++ )
{
print $neg_array[$j] . " ". $pos_array[$i] . " " ;
}

my $diff_arrays = &differenc($pos_num,$neg_num);
print "\n Difference is : $diff_arrays \n";

sub differenc
{
$pos = shift ;
$neg = shift ;

if($pos > $neg)
{

my $diff_num = $pos - $neg ;
for($i ; $i < $min_num + $diff_num ;$i++)
{
print $pos_array[$i] ." " ;
}
return $pos - $neg ;
}
else
{
my $diff_num = $neg - $pos ;
for($j ; $j < $min_num + $diff_num ;$j++)
{
print $neg_array[$j] ." " ;
}
return $neg - $pos ;
}

}

sub minimum
{
$pos = shift ;
$neg = shift ;

if($pos > $neg)
{
return $neg ;
}
else
{
return $pos ;
}


}
print "\n" ;

- Anonymous October 18, 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