Pergunta de entrevista da empresa LinkedIn

Asked a question to determine the sum of an array of integers. Recursive problem.

Respostas da entrevista

Sigiloso

10 de jan. de 2014

Here is the recursive implementation for this in ruby def sum_of_arr(arr, n) if n == 0 return 0 else return arr[n-1] + sum_of_arr(arr,n-1) end end

1

Sigiloso

19 de fev. de 2014

static int sum =0; public static int recSum(int[] a, int start, int end) { if(start==end) return sum+a[start]; else return sum+a[start]+recSum(a,start+1,end); }

Sigiloso

30 de mar. de 2014

Tail recursion version in java: //tail recursion of get the sum of an array public static int getSumOfArray(int sum, int currentPos, int[] intArray){ if(intArray == null || intArray.length == 0){ return 0; } if(currentPos == (intArray.length - 1)){ return sum += intArray[currentPos]; }else{ sum += intArray[currentPos]; return getSumOfArray(sum, ++currentPos, intArray); } }

Sigiloso

5 de set. de 2014

C++ int sum(int[] array, int stopIndex) { if (array.size() == 0 || stopIndex >= array.size()) // so we don't try to access an index that doesn't exist { return 0; } if (stopIndex == 0) { return array[stopIndex]; } else { return array[stopIndex] + sum(array, stopIndex-1); } }