Adobe Interview Question for Developer Program Engineers






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

Its a DP solution of O(n).

SUM=MAX{CURRENT SUM + A[i], A[i]} where 1<=i<=N

void maxSum (int* pArray, int size)
{
	int sum=pArray[0], curSum=pArray[0], startInd=0, endInd=0, index=0;
	for(index=1; index<size; index++)
	{
		if((curSum + pArray[index])> pArray[index])
		{
			curSum = curSum + pArray[index];
		}
		else
		{
			curSum = pArray[index];
			if(sum<0)
			{
				startInd = endInd;
			}
			else
			{
				startInd=index;					
			}
		}
		if(sum<=curSum)
		{	
			sum=curSum;
			endInd = index;
		}
	}
	printf("[Start Index]:%d\n[End Index  ]:%d\n", startInd, endInd);
	printf("[MaxSum     ]:%d\n", sum);
}

- Tulley March 27, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Kadane's solution.
As shown above.

- souravghosh.btbg March 27, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Kadane's with modification. Original Kadane's doesnt work for an array having negative numbers only.

- Tulley March 27, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Wow, didn't see your solution.
Good work making the change.
Thanks.

- souravghosh.btbg March 27, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Tulley: seems your code has bugs in the logic. Check out with below input, for which output is expect 10 (your code shows 9).

int a[] = { 5, -2, -4, 3, 7 }

Here's my code to get the max value. Pls try to catch flaw in it. Thanks.

ASSUMPTION: input has atleast 1 element
void maxSum2 (int *a, int n)
{
	int result = a[0];
	int curSum = a[0];
	
	for(int i=1; i < n; i++ ) 
	{
		if ( curSum > 0 ) curSum += a[i] ;
		else  curSum = a[i] ;

		if ( curSum > result ) result = curSum;
	}

	printf("MaxSum   :%d\n", result);
}

- anon March 28, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Anon: Thanks for pointing out the mistake. I am not able to find any mistake in your code :)
I was more concern about printing the start and end index of the array. I have corrected the my prev code now.

- Tulley March 28, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@anon
Check your solution for this input:
-6 4 20 -15 -10 15
Your code outputs 15 but the correct answer is 24. :)

- Vip May 31, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry my bad.Your code works fine :)

- Vip June 01, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

@ Anon : For this set of 7 numbers , your code will give incorrect output
-1,2,3,4,-5,1,10

It will give output as 15 instead of 11 .

- Niraj March 29, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

How smart are you to claim that 15 is less than 11 :-O
Hope you are not drunk........lol

- LOL March 29, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

answer is not 11..its 15...Kadane's solution is the best with a modification that checks whether all the elements are negative or not..

- DarkLord May 25, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int in[];
int i=j=k=0;
int sum1=sum2=0;
sum1=sum2=in[0];
for (k=1;k<N;k++)
{
if(sum1>=sum1+in[k])
{
sum2=sum1;
i=k;
j=k;
sum1=in[k];

}
else{
j2++;
sum1+=in[k];

}
}
printf("Start %d and end % index sum=%d\n",i,j,(sum2>sum1)sum2:sum1);

- S May 04, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

sum=a[1]+a[2];
for 1=2 to n
{
if (sum>(a[i]+a[i+1])) {do nothing}
else {sum=a[i]+a[i+1]}
}
Print sum,it will be having max

- Rashid May 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

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

onestopinterviewprep.blogspot.com/2014/03/namespace-arrayproblem-write-function.html

- Wellwisher March 26, 2014 | 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