Amazon Interview Question for Software Engineer / Developers

• 0

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

``````int Kadane(int a[],int n)
{
int max_start=0;
int max_end=0;
int max_sum=1<<31;

int sum = 0;
int i=0,j =0;
for(j=0;j<n;j++)
{
sum = sum + a[j];
if(sum > max_sum)
{
max_start=i;
max_end=j;
max_sum = sum;
}
if(sum < 0 )
{
sum = 0;
i = j+1;
}
}
for( int i = max_start ; i <= max_end ; i++)
printf("%d\n", a[i]);

return max_sum;
}``````

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

int max_subarray(int* array, int size)
{
int max_ending = 0, max_so_far = 0, i = 0;
int start = 0, end = 0, curr_start = 0, curr_end = 0;

if (array == NULL)
return -1;

while (i < size) {
if (max_ending > 0) {
max_ending = max_ending + array[i];
curr_end = i;
} else {
max_ending = array[i];
curr_start = curr_end = i;
}

if (max_so_far < max_ending) {
max_so_far = max_ending;
start = curr_start;
end = curr_end;
}

i++;
}

printf("Start = array[%d]=%d and End = array[%d]=%d \n", start,
array[start], end, array[end]);
printf("Start = %d and End = %d \n", start, end);

return max_so_far;
}

int main(void)
{
int my_array[] = {-2, 4, -4, -6, 6, -1, 5, -3, 5, -8};

printf("Max subarray = %d \n",
max_subarray(my_array, 10));

return 0;
}

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

``````int findMaxContSum(){
max = numbers[0];
sum = numbers[0];
for(int i=1; i<n; i++){
sum = sum + numbers[i];
if( sum > max ) {
max = sum;
}
else if (sum<0) {
sum=0;
}
}
}``````

Comment hidden because of low score. Click to expand.
0

in above solution...

``return max;``

Comment hidden because of low score. Click to expand.
0

what if the array contains {-1, -2, -3}?

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

``````//s is the start index of the sequence
//e is the end index of the sequence
void f(int[] a, int& s, int& e)
{
if(!a)
return;

int sum=-MAX;
s=e=0;
int s1=e1=0;
int sum1=0;
int p=0;
while(p<a.len)
{
if(sum1+a[p]>sum)
{
s=s1;
e=p;
sum=sum1+a[p];
}

if(sum1+a[p]<0)
{
e1=s1=p+1;
sum1=0;
}
}
}``````

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

So wat happens when we get the following sequence?

{ 1 2 -2 1 2 -4 5 -1}

So going thru the code, whats the o/p??? we make the index 0 when we reach a -ve number right?

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.

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.