Amazon Interview Question for Software Engineer / Developers


Country: India




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

Isn't this same as Kadane ?

- dhawalparkar December 04, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

My answer to above question

#include <iostream>
#include <string.h>
#include <stdlib.h>
using namespace std;

int main()
{
    int i=0;
    int j=0;
    int sum=0;
    int n = 0;
    int a[100] = {0, 0};
    int currSum=0;
    int maxSum=0;
    int outputArray[10];
    int outputArraySize = 0;
    char str[100];
    char *array[10];
    char c;
    cin.getline(str,100);

    array[i] = strtok(str," ");

    while(array[i]!=NULL)
    {
       array[++i] = strtok(NULL," ");
    }
    n = i;
    for(i=0; i< n; i++)
    {
        a[i] = atoi(array[i]);
    }
    for(i=0; i<n; i++)
    {
        sum += a[i];
    }

    if(sum<1)
    {
        return -1;
    }


    for(i=0, j=0; i<n; i++)
    {
        currSum+=a[i];
        if(currSum<0)
        {
            currSum=0;
            continue;
        }
        if(currSum>maxSum)
        {
            maxSum = currSum;
            outputArray[j++] = i;
        }
    }
    outputArraySize = j;
    for(i=0; i<outputArraySize; i++)
    {
        cout<<outputArray[i] << " ";
    }
}

- Abhijeet Muneshwar November 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def suc_run(inputA):
    best_I = -1
    best = -1
    for i in range(0,len(inputA)-1):
        if inputA[i] + inputA[i+1] > 0:
            if inputA[i] + inputA[i+1] > best:
                best = inputA[i]+inputA[i+1]
                best_I = i;
    if best == -1:
        return -1
    else:
        return best_I, best_I+1

s1 = [3, 2, -6, 2, 1]
s2 = [-2, -3, -1, 0, -6]
print suc_run(s1)
print suc_run(s2)

- jpompe November 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi jpompe,
Your answer won't work for:
{3,8,-6,9,1}

- Adi November 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thanks Adi, you are correct. I misread the problem and thought each value was that year's net income. I think this could be quickly solved with a greedy recursive function.

- jpompe November 26, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Its a maximum sum subsequence problem

- Mukesh November 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think so.

- Abhijeet Muneshwar November 28, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

i think it is maximum sub array...years should be next to each other...

- siva December 01, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Calculate maximum sum of consecutive items, if adding previous to current doesn't increase then restart counting.

p is auxillary array to track consecutive segment contributing to sum. maxi is index of highest sum, tracking maxi, p[maxi], p[p[maxi]] until 0 or p[i]==i would give required answer

sum[0] = a[0];
maxi = -1;
max = 0;

for(i=1; i<n; i++)
{
	sum[i] = 0;
	if( sum[i-1] + a[i] > a[i] )
		p[i] = i-1;
	else
		p[i] = i;
	
	sum[i] = sum[ p[i] ] + a[i] 
	if( max < sum[i] )
	{
		max = sum[i]
		maxi = i;
	}
}

for( i = maxi; i >=0 && i!=p[i]; i=p[i])
{
	printf(
}

- confused_banda November 28, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is a simple solution..

def max_run(x):
#x = randint(-5, 8, 12)

y = x[1:]+x[:-1]

ind = where(y==max(y))[0][0]
return (ind, ind+1)

a = array([3,8,-6,9,1])

print max_run(a)}

- aykut December 06, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int[] intArray = {3, 8, -6, 9, 1};
        int maxIndex = 0;
        int maxProfit = -1;

        for (int i = 0; i < intArray.length - 1; i++) {
            int currentProfit = intArray[i] + intArray[i + 1];
            if (maxProfit < currentProfit) {
                maxProfit = currentProfit;
                maxIndex = i;
            }
        }

        if (maxProfit < 0)
            System.out.println("-1");
        else
            System.out.println("<" + maxIndex + "," + (++maxIndex) + ">");

- Shehan Simen December 29, 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