Groupon Interview Question for Interns


Country: India
Interview Type: Written Test




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

1. First pass, find the sum S of all the numbers in the array
2. Second pass, keep a running sum RS and check if RS == (S - RS), return true
3. Return false at the end

- oOZz August 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi,

In your algorithm, if the case is 6,3,1,7,9,6. Then if we keep track of RS in second pass, then after 7, RS=17 and then it just leaves without checking further elements. So, you might require little bit modification to maintain O(n) complexity.

- Parin August 30, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

6,3,1,7,9,6 looks like an invalid input, as it can never be divided into sub arrays unless you sort them. Without sorting the algorithm given above will return false for the input 6,3,1,7,9,6

- vmzi March 30, 2015 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Return false if total sum of the array is odd.

else, good luck.. This is NP-complete ("partition" problem) :)
exponential time algo should be straightforward.. Compute the power set.. Ignore the empty set and the "full" set, and consider every other set from the power set as a possible candidate..

- AB August 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@AB..cant we just go like:-
if sum is odd then return false
else simultaneously calculate sum from backward and front of the array and stop if we get sum /2 or greater elsewhere side....and return...
correct me if i am wrong....

- dt August 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 4 vote

Please refer to the following link for a dynamic programming approach to the problem
geeksforgeeks.org/dynamic-programming-set-18-partition-problem/

- Nomad August 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

The link you provided solves the problem involving 'subsets' not 'subarrays' and takes >O(n) time.
However if only subarrays are involved then the problem can be solved without DP in O(n) time without using extra space.

- EOF August 14, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 votes

+1 EOF. This is not the same problem. This is partitioning the subarrays not subsets and there is a really easy solution that works in O(n) time and O(1) time that I've described above.

- oOZz August 14, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class ArrayPartition {
public static int [] integers = new int[]{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,10,6};

	public static void main(String [] args){	
		
		int sum = findSumOfArray();
		System.out.println(traverseArray(sum));
	}
	
	
	private static Boolean traverseArray(int sum){
		Boolean reply = false;
		int firstPart = 0;
		int lastPart = 0;
		for(int start = 0; start < integers.length; start++){
			firstPart += integers[start];
			lastPart = sum - firstPart;
			if(firstPart == lastPart)reply = true;
		}
		return reply;
	}
	private static int findSumOfArray(){
		int sum = 0;
		for(int start = 0; start < integers.length; start++){
			sum += integers[start];
		}
		return sum;
	}
}

- Vineet Singh August 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1>sort the array.
2>start from the largest element towards smallest
3>put the current element in the array which has lesser sum(if sum is equal,put on any side).
4>if it is possible to divide the array into two array with equal sum,it will get divided at the end of process else return -1.

please correct me if i am wrong

- kumar October 29, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Have two pointers one starts from the left of the array and the other starts from the right of the array with the numbers added to their sum as they pass, lets say S1 and S2 are the sums. Move the left pointer to the right if S1 < S2, else move right pointer to left. Do this until they meet each other. When they meet each other if S1==S2 then we have got a solution, otherwise there is no solution.

- Anonymous January 26, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public boolean hasPartition(int[] a) {
	if(a == null || a.length < 2) return false;

	int i = 0, j = a.length - 1;
	int leftSum = 0, rightSum = 0;
	while(i - 1 < j + 1) {
		if (leftSum < rightSum) {
			leftSum += a[i++];
		}
		else if (leftSum > rightSum) {
			rightSum += a[j--];
		}
		else {   // (leftSum == rightSum)
			if(i > j) return true;
			leftSum += a[i++];
			rightSum += a[j--];
		}
	}
	return false;
}

- Hong February 24, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

How come i-1<j+1 works. You have to check till i + 1 == j-1 or i ==j . Correct me if I am wrong

- Srinathkattula January 07, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Please check if my approach is incorrect

public static boolean isPartitionable(int a[])
{
int sum=0;
for(int i=0;i<a.length;i++)
sum+=a[i];

if(sum%2!=0)
return false;
sum=sum/2;
int rs=0;
for(int i=0;i<a.length;i++)
{
int t=rs+a[i];
if(t==sum)
return true;
else if(t<sum)
rs=t;

}

return false;
}

- braj March 03, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private static void findSplit(int[] array) {
        int startSum = array[0];
        int endSum = array[array.length - 1];
        int start = 0;
        int end = array.length - 1;

        while (start < end) {
            if(start == (end -1)) {
                break;
            }
            if (startSum == endSum) {
                start++;
                end--;
                startSum += array[start];
                endSum += array[end];
            } else if (startSum > endSum) {
                end--;
                endSum += array[end];
            } else {
                start++;
                startSum+= array[start];
            }
        }
        if(startSum == endSum) {
            System.out.println("There was a mid point at" + start );
            
        } else {
            System.out.println("There was no mid point");
        }

    }

- Anonymous March 04, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private static void findSplit(int[] array) {
        int startSum = array[0];
        int endSum = array[array.length - 1];
        int start = 0;
        int end = array.length - 1;

        while (start < end) {
            if(start == (end -1)) {
                break;
            }
            if (startSum == endSum) {
                start++;
                end--;
                startSum += array[start];
                endSum += array[end];
            } else if (startSum > endSum) {
                end--;
                endSum += array[end];
            } else {
                start++;
                startSum+= array[start];
            }
        }
        if(startSum == endSum) {
            System.out.println("There was a mid point at" + start );
            
        } else {
            System.out.println("There was no mid point");
        }

    }

- Anonymous March 04, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class EqualSumArraydetection {

/**
* @param args
*/
public static void main(String[] args) {
int[] arr= {5,1,5,11,10,5,7};
System.out.println(isqualSumarray(arr));

// TODO Auto-generated method stub

}

public static boolean isqualSumarray(int[] array){

for (int i=1; i<array.length; i++){
array[i]=array[i]+array[i-1];
}
for (int i=array.length-2; i>0; i--){
if (array[i]==array[array.length-1]-array[i]){
return true;
}
}

return false;
}
}

- kunwar sunil singh April 08, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class EqualSumArraydetection {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		int[] arr= {5,1,5,11,10,5,7}; 
		System.out.println(isqualSumarray(arr));
		
		// TODO Auto-generated method stub

	}

	public static boolean isqualSumarray(int[] array){
		
		for (int i=1; i<array.length; i++){
			array[i]=array[i]+array[i-1];
		}
		for (int i=array.length-2; i>0; i--){
			if (array[i]==array[array.length-1]-array[i]){
				return true;
			}
		}
		
		return false;
	}
}

- kunwar sunil singh April 08, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
#include<stdio.h>

using namespace std;

int main(void)
{
	int a[]={1 ,1 ,2 ,2, 1, 1} ;
	int size=sizeof(a)/sizeof(a[0]);
	
    int j=size-1;  //from end
    int i=0;       //from start
    int suml=0,sumr=0;
    while(i<=j)
    {
    	if(suml<=sumr)
    	{
		    	suml=suml+a[i];
		    	i++;
        }
        
        else
        {
        	sumr=sumr+a[j];
        	j--;
        }
    	
    }
    
    if(sumr==suml)
    cout<<"TRUE";
    
    else
    cout<<"FALSE";

	
	return 0;
}

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

#include<iostream>
#include<stdio.h>

using namespace std;

int main(void)
{
	int a[]={1 ,1 ,2 ,2, 1, 1} ;
	int size=sizeof(a)/sizeof(a[0]);
	
    int j=size-1;  //from end
    int i=0;       //from start
    int suml=0,sumr=0;
    while(i<=j)
    {
    	if(suml<=sumr)
    	{
		    	suml=suml+a[i];
		    	i++;
        }
        
        else
        {
        	sumr=sumr+a[j];
        	j--;
        }
    	
    }
    
    if(sumr==suml)
    cout<<"TRUE";
    
    else
    cout<<"FALSE";

	
	return 0;
}

- Akash August 09, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 3 vote

If this question is only concern with the sequentially formed elements it is an O(n) problem:

int i,j,sum=0,s=0,flag=0;
	for(i=0;i<n;i++)
		sum+=a[i];
	if(sum%2!=0)
	{
		printf("\nIt cannot be broken into two subarray of equal sums\n");
		return;
	}

	for(i=0;i<n;i++)
	{
		s+=a[i];
		if(sum-s==0)
		{
			flag=i;
			break;
		}
		sum-=s;
	}
	if(flag!=0)
	{
		for(j=0;j<flag;j++)
			printf("%d\t",a[j]);
		for(j=flag;j<n;j++)
			printf("%d\n",a[j]);
	}
	else
		printf("\n It cannot be broken into two subarray of equal sums\n");

However, its complexity grows exponentially, as pointed out by others, with the size of the input.

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

its not working for 1 1 2 2 1 1 and will not work for similar arrays

- kavita September 07, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Instead of updating sum every time, we can compare "s" with sum/2 directly which can give correct ans

- Sandeep September 18, 2013 | 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