PayPal Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

/*
	 * Returns a maximum sum value in an array of integers
	 */
	public int maxScan(int[] array)
	{
		if(array.length == 0) return -1;
		else if(array.length == 1) 
			return array[0];
		else
		{	
			int maxSum = array[0];
			int current_max = array[0];
			for(int i = 1; i < array.length; i++)
			{
				current_max = Math.max(array[i], current_max + array[i]); 
				maxSum = Math.max(maxSum, current_max);
			}
			return maxSum;
		}
	}

- hello world January 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Out put for folloing input is wron.{-1,-2,3,4}.Expected out put is 7 but it's giving six.

- hyd November 07, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

this works perfect. but they asked how to solve time complexity. will it resolve that also?

- javid.anees November 26, 2015 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

search Maximum_subarray_problem in wikipedia

- abhishekatuw January 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can you give some examples, I can't understand the problem?

- V.Krestyannikov January 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Answered many times before on this site.

- eugene.yarovoi January 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int main(){

int array[] = {80,18,-56453,53,14};

int n = sizeof(array)/sizeof(int);

int max_sum=0;
int sum=0;

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

cout<< max_sum;

}

- Anonymous May 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This code fails for the below case where a big positive number is followed by a smaller negative number and again a set of positive numbers.
e.g. -1, -11, -2, 3, -5, 10, -1, 3, 2, 1
Here the subarry with max sum is 10, -1, 3, 2, 1 but your code recognizes it as 10 !!

- susi August 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

int LargestSum(int* index, int size, int* startIndex, int* endIndex){
	int i;
	int max_so_far, max_ending_here;
	max_so_far= INT_MIN;
	max_ending_here = 0;
	for(i=0; i< size;i++){
		max_ending_here=max_ending_here + index[i];
		if(index[i] > max_ending_here){
			max_ending_here = index[i]; 
			*startIndex = i;
		}
		if(max_ending_here > max_so_far){
			max_so_far = max_ending_here;
			*endIndex = i;
		}
		printf("%d, %d\n", max_so_far, max_ending_here);
	}
	//printf("%u, %u\n",startIndex,endIndex);
	//printf("%d, %d\n",*startIndex,*endIndex);
	return max_so_far;
}

int main(){
	int arr[8] = {-2, -3, 4, -1, -2, 1, 5, -3};
	int size = sizeof(arr)/sizeof(arr[0]);
	int start=0, end=7;
	//printf("%u, %u\n",&start,&end);

	printf("Starts at:%d, Ends at:%d,Largest Sum:%d\n",start, end, LargestSum(arr,size, &start, &end));
	
}

- Praveen August 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

Can anybody tell me if all numbers are negative then how can we solve this problem.

- Pranay Singhania May 20, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This code works for Negative Numbers also and also gives startIndex and endIndex of the Maximum SubArray

int LargestSum(int* index, int size, int* startIndex, int* endIndex){
	int i;
	int max_so_far, max_ending_here;
	max_so_far= INT_MIN;
	max_ending_here = 0;
	for(i=0; i< size;i++){
		max_ending_here=max_ending_here + index[i];
		if(index[i] > max_ending_here){
			max_ending_here = index[i]; 
			*startIndex = i;
		}
		if(max_ending_here > max_so_far){
			max_so_far = max_ending_here;
			*endIndex = i;
		}
		printf("%d, %d\n", max_so_far, max_ending_here);
	}
	//printf("%u, %u\n",startIndex,endIndex);
	//printf("%d, %d\n",*startIndex,*endIndex);
	return max_so_far;
}



int main(){
int arr[8] = {-2, -3, 4, -1, -2, 1, 5, -3};
int size = sizeof(arr)/sizeof(arr[0]);
int start=0, end=7;
//printf("%u, %u\n",&start,&end);


printf("Starts at:%d, Ends at:%d,Largest Sum:%d\n",start, end, LargestSum(arr,size, &start, &end));

}

- Praveen August 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
using namespace std;
#include<stdio.h>
int maxSubArraySum(int a[], int size)
{
int max_so_far = 0, max_ending_here = 0;
int i;
for(i = 0; i < size; i++)
{
max_ending_here = max_ending_here + a[i];
if(max_ending_here < 0)
max_ending_here = 0;
if(max_so_far < max_ending_here)
max_so_far = max_ending_here;
}
return max_so_far;
}

/*Driver program to test maxSubArraySum*/
int main()
{
int a[] = {-2, 10,-13, 4, 7, -6, 1, 5, -3};
int max_sum = maxSubArraySum(a, 8);
printf("Maximum contiguous sum is %d\n", max_sum);
return 0;
}

- Ajay Yadav Aricent July 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<conio.h>
int max(int a[],int size){
int maxso.maxend=0,i;
for(i=0i<size;i++)
{if(maxend<0)
maxend=0;
}if(maxso<maxend){
maxso=maxend;}return maxso;}
int main(){
int a[]={1,2,3,4,5,6,-7,-7}
int maxsum=max(a,8);
printf("Sum %d",maxsum);getchar();
return 0;


}


}

- tushki.dhingra September 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int maxSumInSubArr(int array[], int size, int *startI, int *stopI)
{
int max_here, max_far;
max_far = INT_MIN;
max_here = 0;
int tmpStart = *startI;

for (int i=0; i < size; i++) {
max_here = max_here + array[i];
if(array[i] > max_here)
{
max_here = array[i];
tmpStart = i;
}
if(max_here > max_far)
{
max_far = max_here;
*stopI = i;
*startI = tmpStart;
}
}
return max_far;
}

- Anatoly Macarov January 29, 2014 | 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
Comment hidden because of low score. Click to expand.
0
of 0 vote

I have written this code. I have taken the size of the array to be 20. You change it according to your need.

#include<iostream>
using namespace std;
int maxcross(int arr[],int low,int mid,int high){
	int left_sum = -1,sum=0,max_left,right_sum=-1,;
	for(int i=mid;i>=low;i--){
		sum+=arr[i];
		if(sum>left_sum){
			left_sum=sum;
			max_left = i;
		}
	}
	sum=0;
	for(int i=mid+1;i<=high;i++){
		sum+=arr[i];
		if(sum>right_sum){
			right_sum = sum;
		}
	}
	return left_sum+right_sum;
	
}
int max_sub_array(int arr[],int low,int high){
	if(high==low)
		return arr[low];
	else{
		int mid = (low+high)/2;
		int left_sum = max_sub_array(arr,low,mid);
		int right_sum = max_sub_array(arr,mid+1,high);
		int cross_sum = maxcross(arr,low,mid,high);
		if(left_sum>right_sum&&left_sum>cross_sum)
			return left_sum;
		else if(right_sum>cross_sum)
			return right_sum;
		else
			return cross_sum;
		
	}
}


int main(){
	int arr[20];
	cout<<"Enter the size of the array(max 20):\n";
	int n;cin>>n;
	cout<<"Enter the array:\n";
	for(int i=0;i<n;i++)
		cin>>arr[i];
	int ans = max_sub_array(arr,0,n-1);
	cout<<"The Maximum sum is: "<<ans<<endl;
	
	
	return 0;
}

Have any queries. Please comment

- sndpkmrshukla June 18, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int maxSubArray(vector<int> A) {
	int start = A[0];
	int sum = A[0];
	int max = A[0];
	
	for(int i = 1; i < A.size(); i++) {
		sum += A[i];
		if(sum - start > max) {
			max = sum - start;
		}
		if(sum < start) {
			start = sum;
		}
	}
	return max;
}

- Mhret November 12, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public int getMaximumSum(int[] inputs){
int sum = 0;
for(int i=0; i<inputs.length;i++){
if(sum + inputs[i] > sum){
sum = sum + inputs[i];
}
}
return sum;
}

- dev October 13, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

///
public int getMaximumSum(int[] inputs){
int sum = 0;
for(int i=0; i<inputs.length;i++){
if(sum + inputs[i] > sum){
sum = sum + inputs[i];
}
}
return sum;
}
///

- dev October 13, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public int getMaximumSum(int[] inputs){
        int sum = 0;
        for(int i=0; i<inputs.length;i++){
           if(sum + inputs[i] > sum){
                sum = sum + inputs[i];
           }
        }
        return sum;
    }

- dev October 13, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package amazonExcerise;

import java.util.Arrays;
import java.util.Collections;

public class TestNGValidation {
public static void main(String[] args) {
int a[]={-1, -11, -2, 3, -5, 10, -1, 3, 2, 1};
int[] b = new int[500];
int k=0,Result=0;
for(int i=0;i<=a.length-1;i++){
	Result=0;
	for(int j=i;j<=a.length-1;j++,k++){
		Result=a[j]+Result;
		b[k]=Result;
	}
}
Arrays.sort(b);
System.out.println("The maximum sum possible from different sub Arrays : "+b[b.length-1]);
	
}
}

- Sunil Ketha March 29, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Working code

int main(){

int array[] = {18,-18, -53, 53, 4};

int n = sizeof(array)/sizeof(int);

printf("\n Length-- %d --i \n",n);
int max_sum=0;
int sum=0;

for(int i = 0; i <= n; i++) {

sum+=array[i];

if(sum>max_sum){
max_sum=sum;}
else if (sum < 0) {
sum = 0;
}

}

printf("\n -> %d \n",max_sum);

}
~

- silvimasss January 20, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi, wont ur code fail if the first number in the array is a -ve. I guess the following code works!


int main(){

int array[] = {80,18,-56453,53,14};

int n = sizeof(array)/sizeof(int);

int max_sum=0;
int sum=0;

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

cout<< max_sum;

}

- Anonymous May 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@silivimass -
This code fails for the below case where a big positive number is followed by a smaller negative number and again a set of positive numbers.
e.g. -1, -11, -2, 3, -5, 10, -1, 3, 2, 1
Here the subarry with max sum is 10, -1, 3, 2, 1 but your code recognizes it as 10 !!

- susi August 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Both the code will fail when the array contain only negative numbers.... even if it is not advised to take sum in integer... because it is an integer array so two of the elements one is 2^32-1 and another any positive integer will fail the logic.

- abhradeep.kundu August 18, 2012 | Flag


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