Dell Technologies interview question

Sum up integer array?

Interview Answers

Anonymous

Feb 18, 2011

lol

3

Anonymous

Jan 8, 2013

p-code: sum = 0, i = -1, j = arr.size(); while (++i < --j) sum += arr[i] + arr[j]; if (i == j) sum += arr[i];

Anonymous

Sep 18, 2012

Hi, Is the 2nd case valid? I am thinking it may not be valid since the array is an integer array ( not unsigned int ) so what if the array has an actual value of -1? Then it would terminate prematurely.

Anonymous

Feb 20, 2011

Assuming array size is known: can be done in two ways 1) store size in the first element 2) End the last element with a sentinel value like (I feel this is more common) For 2nd case int64_t sum = 0; uint32_t index = 0; while (arr[i++] != -1) { sum+= arr[i]; } return sum;