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) ;
    }
}

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

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

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

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.

- merlinme December 17, 2014 | Flag
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.

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

Yeps, nice solution.

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

- HS August 27, 2012 | Flag
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..

- Jammy August 27, 2012 | Flag Reply
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])

- Anonymous August 27, 2012 | Flag Reply
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;
}

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

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.

- dc360 September 05, 2012 | Flag
Comment hidden because of low score. Click to expand.


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