## Roxar Interview Question for Software Engineer Interns

Country: United States
Interview Type: Phone Interview

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

Assuming all numbers in the set are positive.
Step 1. Add the numbers in the set.
Step 2. Subtract the given sum from added total at step1.
Step 3. Divide number by 2 at step 2. Reverse the sign of this particular number.

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

O(n) time, O(1) space

``````static int reverseSignElement(int[] array, int target) {
if (array == null || array.length == 0)
return 0;
int sum = 0;
for (int i : array) {
sum += i;
}
for (int i : array) {
if (sum - 2*i == target)
return i;
}
return 0;
}``````

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

How did you derive the " sum - 2*i "? It clearly works, but logically how did you get there?

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

logic derivation:

Change of sign a element in set will return the target number.

Assume R is the resultant element and we will change the sign of R

So in the sum of the all elements of set (Say Sum), R is also included. First exclude that.
So we have sum of all elements except R => Sum – R.
Now, we changed sign of R and add it to get new sum of all elements.
( Sum –R ) + ( –R) = target //we changed sign of R
i.e, Sum –R –R = target
i.e, Sum – 2*R = target

Now for which i in set, this equation will satisfy, that is the result.

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.