Interview Question


Country: United States




Comment hidden because of low score. Click to expand.
7
of 13 vote

Use Recursion : Pass the value of the range and keep increasing the range

int sum(int i, int j, int[] array)
{
  if( i <=j)
    {
       return array[i] + sum(i+1,j,array);
     }
     else
        return 0;

}

- Abhi January 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is inefficient code.

- Bijendra January 27, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nice and clean solution, though it requires the programmer to know the size of the array (at compile time), as there is no bounds checking.

- Elwin January 27, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

While elegant solution it misses the point which is avoiding the stack depth limitation. The right solution here is to use tail recursion, see my answer below.

- dev.cpu January 28, 2013 | Flag
Comment hidden because of low score. Click to expand.
2
of 4 vote

Use a while loop? goto?

Stupid question for an interview.

- Anonymous January 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 vote

Use tail recursion. Plain recursion is inefficient and most likely will blow up the stack. The following code allows tail recursion optimization (with option -O2 at least in gcc), try it, it won't blow up.

#include <stdio.h>
#include <stdlib.h>

int tailSum(int*buf, int n, int sum) {
    if (n == 0)
        return sum;
    return tailSum(buf + 1, n - 1, sum + buf[0]);
}

int main() {
    int *buf = (int*)malloc(sizeof(int) * 1000000);
    // initialize buf contents
    printf("%d\n", tailSum(buf, 1000000, 0));
    return 0;
}

- dev.cpu January 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Could you tell me Why are you decrementing n value while you are incrementing buf to buf+1;,
One side it is incremented and another side decremented .Half array sum will be calculated..?
Please let me know.

- Raj January 28, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

No, the whole array will be computed. I'm using n only to count the recursive calls and terminate when n == 0. In each call I'm incrementing buf to access the next element, so in the end all elements from buf to buf + n - 1 will be used

- dev.cpu January 28, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 3 vote

aaye yaar while loop laga lo, if for loop is not allowed.

- sonesh January 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

This comment has been deleted.

- Administrator January 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Elwin...good Approach.But Can you write without loop.I admit my mistake I pasted question wrong.We should not use any loop/

- Raj January 28, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Using recursion :)

private static int sumArray(int a[], int pos) {
		if(pos==a.length)
			return 0;
		return a[pos]+sumArray(a, pos+1);
	}

- rajeevkrishnanv January 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Int FindTheSum(int x,int y , int a[])
{
if(x==y) // Cover Terminating cases
{
return a[x];
}
if(x<y){
return a[x]+FindTheSum(x+1,y,a);
}
else
throw exception;
}

- Samantra January 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Tail recursive algorithm.

private long subarraySum( int[] arr, int from, int to, long res ){
		
		if( from > to ){
			throw new IllegalArgumentException("from > to: " + from + " > " + to);
		}
		
		if( from < 0 || from >= arr.length || to >= arr.length ){
			throw new IllegalArgumentException("'from' or 'to' is incorrect");
		}
		
		if( from == to ){
			return res + arr[to];
		}
		
		return subarraySum( arr, from+1, to, res + arr[from] );		
	}

- m@}{ January 28, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

My instinct for this problem is ... interviewer is trying test some wide range of data structure and algorithm that we should know. It obviously needs preprocessing to avoid loop for range sum. This is sum range problem. Cumulutive sum or prefix sum gives us solution in O(n) preprocessing time but O(1) query time. However with segment tree preporcessing it will be <O(log n ) , O(logn)>

- Prajwal Rupakheti January 29, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 3 vote

#include <functional>
#include <numeric>

long sum(int i, int j, int [] numbers)
{
        return accumulate(numbers,numbers+abs(i-j)+1,0);
}

- Bijendra January 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

good use of stl algorithm

- Anonymous January 28, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 3 vote

We can maintain a cumulative array from the begining. Let it be c[].

Then, summation(a[i]...a[j]) will be c[j] - c[i].

- abyrupus January 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

Guys, in above solution of mine, last statement is = return n*(n+1) / 2 and not "%"

- Bijendra January 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 votes

Guys, in above solution, last statement is = return n*(n+1) / 2 and not "%"

- Bijendra January 27, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You have no idea what is going on :-)

- Abhi January 27, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Abhi : It looks you have never read mathematics. You use re-cursion in simple summation program. Looks you are just a school student. Your method is most inefficient method. In my solution it was just a typo : "%" ... i meant, division that is "/" .

- Bijendra January 27, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Ok Dear : Solve the below condition

Array content = {10,56,17,23,99,819}
starting index = 1;
ending index = 4;
Answer = 56+17+23+99

- Abhi January 27, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Bijendra : You are making an assumption that array would be having sequential elements from i to j, which is not the case here. I agree with @abhi that you have no idea what is going on :)

- Ashu January 27, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry guys, i misunderstood the problem. I will post my solution soon. :(

- Bijendra January 27, 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