Amazon Interview Question
SDE1sCountry: India
Interview Type: In-Person
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;
}
}
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();
}
}
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?
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);
}
}
}
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];
}
}
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);
}
}
}
## 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" ;
Can you explain you question clearly with example....??
- Rahul October 09, 2013