Interview Question


Country: United States




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

use this link :en.wikipedia.org/wiki/Maximum_subarray_problem

- anand August 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

haha.. the answer should be {40,-10,-10,100,1}
see: ideone.com/mttTG

- cobra August 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

used kadane's algorithm

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

Infact the question to get the subset giving the max sum on the sum value itself

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

sorry, i havent gone through the question properly..
i post the required code once i get it..

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

n1b-algo.blogspot.de/2010/03/largest-sum-of-consecutive-numbers.html

- nsdxnsk August 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
using namespace std;
#define N 8

int main(){
int A[]= {-2,-3,-5,40,-10,-10,100,1};
int minI, maxI, max, i, j, temp;
minI= maxI= 0;
max= A[0];

for(i= 0; i< N; i++){
temp= 0;
for(j= i; j< N; j++ ){
temp+= A[j];
if(temp> max){
minI= i;
maxI= j;
max= temp;
}
}
}
cout<<max<<endl;
cout<<"minI "<<minI<<" maxI "<<maxI<<endl;
for(i= minI; i<= maxI; i++)
cout<<A[i]<<" ";
return 0;
}

- Abhijeet Rokade August 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Result in subset or sub-array?

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

I think it should be subarray.
otherwise, we will pick all the positive numbers in the array to add together

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

hrhd

- Anonymous August 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Shouldnt' the answer be {40,100,1}, Please correct me if I am wrong...

- Anonymous August 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

scan the array for 1st +ve number and the smallest number
if (no +ve) => return {smallest number}
else

initialize MaxSubset, CurrentSubset ={1st +ve number}
      from next index to last index
            append this number to CurrentSubset
            if (sum(CurrentSubset) >= MaxSubset)
                  MaxSubset = CurrentSubset
            else if (sum(CurrentSubset) <= 0)
                  CurrentSubset = { }
     return MaxSubset

.
you can use indices to track the MaxSubset and CurrentSubset instead of using separate arrays and appending them
. worked in my head, should work when coded ;)

- Too lazy to write the code August 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

oopsi, I wrote "smallest" instead of "largest"

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

public ArrayList<Integer> longest(int[] data)
{
ArrayList<Integer> result = new ArrayList<Integer>();
ArrayList<Integer> tmp = new ArrayList<Integer>();
int max = 0;
int currentSum = 0;
for(int i = 0; i < data.length; i++)
{
currentSum += data[i];
tmp.add(data[i]);
if(currentSum > max)
{
result.clear(); // reset
result.addAll(tmp); // copy the values
max = currentSum;
}
else if(currentSum < 0)
{
tmp.clear();
currentSum = 0;
}
}
return result;
}

- Anonymous August 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

seesharpconcepts.blogspot.in/2012/05/maximum-sum-of-consecutive-numbers-on.html

Similar but complex version of above problem. Approach explained here!

- seesharpconcepts August 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

recursion function should be like this:

input[] = {-2,-3,-5,40,-10,-10,100,1}

result[i] = { max{result[i-1]+input[i], input[i]) if i>1
input[i] if i=0;
}


find the max value in result[], that is the answer

- devendar August 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

shouldn't the subset of the array making the max sum of the above considered array be {40,100,1} ?? i mean is it given in the questions that the subset of the array should contain consecutive elements?

- Shruti August 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

the element in the subset should be consecutive.. that is sub-array..
if it is not consecutive, then we can take only positive elements from the array which is not at all a problem to post and discuss here. ..

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

Use maximum subarray problem. Check code at ideone.com/xNf8z

- maverick August 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think this should work...and the runtime is O(n). Explanations are next to the code. Any feedback will be appreciated. :)

import java.util.Vector;

public class MaxArray {
	public static Vector<Integer> calcMax(int[] array) {
		Vector<Integer> temp = new Vector<Integer>();
		int max = 0;
		int sum = 0;
		for (int i = 0; i < array.length; i++) { // if value > 0 -> add to
													// result
			if (array[i] >= 0) {
				sum += array[i];
				max = sum;
				temp.add(array[i]);
			} else { // if value < 0 -> check conditions
				if (i + 1 == array.length) { // if is last index, return without
												// last index
					return temp;
				}
				if (sum + array[i] > 0) { // if previous sum + current negative
											// value > 0 -> add it
					sum += array[i];
					max = sum;
					temp.add(array[i]);
				} else { // else don't add and reset
					max = 0;
					sum = 0;
					temp.clear();
				}
			}
		}
		return temp;
	}

	public static void main(String args[]) {
		int[] arr = { -4, 1, 200, -10, 300, 300, 1, -1 };
		System.out.println(calcMax(arr));
	}
}

- Malluce August 13, 2012 | 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