## Credit Suisse Interview Question for Analysts

Country: India
Interview Type: Written Test

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

``````int sumCount(int* arr, int len, int total)
{
int count = 0;
for (int i = 0; i < len; ++i)
{
sumCountHelper(arr, i, len, total - arr[i], count);
}
return count;
}

void sumCountHelper(int* arr, int start, int len, int total, int& count)
{
if (total == 0)
{
++count;
return;
}
if (total < 0)
return;
for (int i = start + 1; i < len; ++i)
{
sumCountHelper(arr, i, len, total - arr[i], count) ;
}
}``````

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

The solution fails for the following table = {15, 5, 3, 2} and sum=15.

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

If you can get a single number equal to exactly the sum in the array and that should not be counted then yes, {15, 5, 3, 2} with sum 15 is a fail. However it would be easy to code around, e.g. add a "first_call" parameter to sumCountHelper; if total == 0 && first_call then return without incrementing count. If we get to the recursive call then call sumCountHelper with first_call = false.

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

Apply the subset algorithm -- iterate from 1 to (2^(array size) - 1). Use binary representation of i to select numbers that have 1 at corresponding position. Get sum of it. If it's equals to sought, output/count.

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

Yeps, nice solution.

For binary representation, we can also take array of boolean variables, instead of converting int to binary representation.

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

Mr Nasser Ghazali.
plz explain your solution .. am not able to trace it..

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

``````def sum_count(alist, target):
if len(alist) == 0 or target < 0:
return 0
elif alist[0] == target:
return sum_count(alist[1:], target) + 1
else:
return sum_count(alist[1:], target) + sum_count(alist[1:], target - alist[0])``````

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

int sumcount(int *arr,int len,int total)
{
int count=0,i,j;
int tot;
for(i=0;i<len;i++)
{
tot=total-arr[i];
for(j=i+1;j<len;j++)
{

tot=tot-arr[j];
if(tot==0)
{
count++;
tot=tot+arr[j];
}
if(tot<0)
break;
}
}
return count;
}

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

Not correct. not doing 10+3+2 from the stated problem. Look at it this way there are 2^n subset, but your logic is looking at only n squared. Subsets, not subarrays.

Comment hidden because of low score. Click to expand.

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.

### 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.