TP Interview Question for SDE1s


Country: United States
Interview Type: Phone Interview




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

Please let me know if something is wrong here

bool IsSumForOdd(int *arr, int n, int count, int sum)
{
if(sum == 0 && count%2 == 0)
return true;
if(sum < 0)
return false;
if(sum < arr[n-1])
return IsSumForOdd(arr, n-1, count, sum);
else
{
return (IsSumForOdd(arr, n-1, count+1, sum-arr[n-1])) || (IsSumForOdd(arr, n-1, count, sum));

}

}

- DashDash May 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Shouldn't it be count%2==1 since we need odd no of nos to make up the sum?

- alex May 10, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Ditto what alex said.

This check is unnecessary: if(sum < arr[n-1])
return IsSumForOdd(arr, n-1, count, sum);

An additional check that is needed is if (n == 0) { return false; }

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

@DashDash:
int arr[] = {0, 3, 11, 23, 44,};
check for this input your program??

- aka May 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think the following will work

int  IsSumForOdd(int* a,int n,int counter,int sum)
{
	if(a[n] == 0)
		return 1;
	if(sum == 0)
		if (counter%2 == 0)
			return 1;
		else
			return 0;
	if(sum<0)
		return 0;
	if(n==0)
		return 0;
	if(sum<a[n-1])
		return doSearch(a,n-1,counter,sum);
	return doSearch(a,n-1,counter+1,sum-a[n-1])||doSearch(a,n-1,counter,sum);
}

- dadakhalandhar May 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

aka what is sum in the array that you have given

- DashDash May 12, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@DashDash: check for 15.

- aka May 12, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes it works aka only thing is as alex has pointed out count%2 == 1 instead of 0

- DashDash May 12, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@DashDash: ideone.com/ftaKXG doesn't work

- aka May 13, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

there is a runtime error here
Please add a check for n i.e if (n<0) then return false;

- DashDash May 13, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

ideone.com/1aW6DV it works now.

- aka May 13, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Modified subset sum problem and it works here as well.

int total=0;
#define SIZE(a) sizeof(a)/sizeof(a[0])
int arr[] = {2, 11, 20, 30, 31};
int counter = 0;

void IsSubArray(int n, int N, int sum) // N is the number of elements in the array 
{
    counter++;
	if(sum == N) {
        if(((counter%2) == 1)) {
            printf("got it\n");
            counter--;
    	}
		return;
	}

	if((n <= (SIZE(arr)-1)) && arr[n]+sum <= N) {
		IsSubArray(n+1, N, sum+arr[n]);
	}

	if((n <= (SIZE(arr)-1)) && arr[n+1]+sum <= N) {
        counter--;
		IsSubArray(n+1, N, sum);
	}
}

int main()
{
	int j, i;
	int target_sum=51;

	IsSubArray(0, target_sum, 0);
	return 0;
}

- aka May 13, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

@EK MACHCHAR

Isnt the input array supposed to be sorted?

- arun_lisieux May 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool HasOddSum(int arr[], int len, int count, int sum)
{
	if ((sum == 0) && (count % 2 == 1))
		return true;
	else if (sum < 0 || len < 0)
		return false;
	else if (sum < arr[len - 1])
		return HasOddSum(arr, len - 1, count, sum);
	else
		return HasOddSum(arr, len - 1, count + 1, sum - arr[len - 1]) || HasOddSum(arr, len - 1, count, sum);
}

void RunDriver()
{
	//input
	int arr[] = {3, 23, 44, 2, 11};
	int sum = 16;

	cout << endl << "Array " << (HasOddSum(arr, sizeof(arr) / sizeof(arr[0]), 0, sum) ? "has" : "has't") << " sum " << sum << " with odd number of elements" << endl;
}

- coding.arya May 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@coding.arya
int arr[] = {0, 3, 11, 23, 44,};
check for this input your program?

- aka May 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You are right... my code doesn't work for 0... but it can be modified slightly to resolve that problem as well.. :)

- coding.arya May 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Complexity of you code is O(2^N).

- jigs May 12, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This solution is a tweak of Subset Sum Problem(Dynamic Programming) solution and As per I know this is best known solution for it.
O(sum*len)

- coding.arya May 12, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

function checkExists(arr,length,count,k)
{
    if(k==0 && count%2==1)
        return true;
    else if(k<0 || length<0)
        return false;
    else if(k<arr[length-1])
        return checkExists(arr,length-1,count,k);
    else
        return checkExists(arr,length-1,count+1,k-arr[length-1]) || checkExists(arr,length-1,count,k);
}

explanation:

1. check if sum=0 and count is odd [one end case of recursion]
2. check if sum<0 or length<0 [recursion lands into out of boundary cases]
3. check if the sum to be found is lesser than last element of array considered as it is sorted if yes ....dont consider last element and recurse.
4. if the sum is greater than the last element, it[last element] can or cannot be a part of the numbers which are involved in the odd sum. so consider once in recursion "or" dont consider

- anand.shenoy14 April 13, 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