## Riverbed Interview Question for Software Engineer / Developers

Country: United States

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

We can sort the input array and then do a scan from both ends. O(nlogn) + O(n) = O(nlogn) time complexity. O(1) space.

``````public static int[] sumNums(int[] array, int targetSum) {
if(array == null) return null;
int n = array.length;
if(n < 2) return null;
Arrays.sort(array);
int low = 0;
int high = n - 1;
while(low < high) {
int sum = array[low] + array[high];
if(sum < targetSum) {
low++;
} else if (sum > targetSum) {
high--;
} else {
return new int[] {array[low], array[high]};
}
}
return null;
}``````

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

@eugene.yarovoi: Thanks.

``````public static int[] sumNums(int[] array, int targetSum) {
if(array == null) return null;
int n = array.length;
if(n < 2) return null;
Set<Integer> set = new HashSet<Integer>();
for(int i = 0; i < n; i++) {
int num = array[i];
int diff = targetSum - num;
if(set.contains(diff)) {
return new int[] {num, diff};
}
}
return null;
}``````

Comment hidden because of low score. Click to expand.
-1
of 1 vote

let sum is = s.
for i=1 to ARRAY_SIZE
check if (sum- array[i]) is in array

optimize code so that 2+3 , and 3+2 does not repeat.

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.