## Uber Interview Question for Software Engineers

Country: United States
Interview Type: Phone Interview

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

``````def subset_sum_1(arr, target, partial=[], cur_sum=0):
if cur_sum == target:
yield partial
if cur_sum >= target:
return
for i, n in enumerate(arr):
remaining = arr[i + 1:]
yield from subset_sum_1(remaining, target, partial + [n], cur_sum + n)``````

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

``````def subset_sum_1(arr, target, partial=[], cur_sum=0):
if cur_sum == target:
yield partial
if cur_sum >= target:
return
for i, n in enumerate(arr):
remaining = arr[i + 1:]
yield from subset_sum_1(remaining, target, partial + [n], cur_sum + n)``````

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

One of the most efficient solutions to the above problem could be to use an
1. unordered set in c++.
2. push all the array elements into it.
3. now, one by one pick the elements from the array and calculate the difference between the total sum and the array element and find for the result in the set, if found push the set into the vector, and repeat this step till all the array elements have been processed and then, finally return the vector, which will be you final solution.
Thanks

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

Probably not as compact and elegant as the solutions above, but it does it job.

``````const getSequences = (list, sum, sequences={}, curSequence=[], index=0) => {
for (; index < list.length; index++) {
const seqSum = curSequence.reduce((s, value) => {
return s + value;
}, 0);

if (seqSum + list[index] > sum) {
continue;
}

if (seqSum + list[index] == sum) {
const matchSeq = [...curSequence, list[index]];
const hashCode = matchSeq.reduce((s, value) => {
return s + ',' + value;
}, '');

if (!(hashCode in sequences)) {
sequences[hashCode] = matchSeq;
}

continue;
}

const newSequences = getSequences(list, sum, sequences, [...curSequence, list[index]], index+1);
for (let [hashCode, sequence] of Object.entries(newSequences)) {
sequences[hashCode] = sequence;
}
}

return sequences;
};``````

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

Probably not as compact and elegant as the solutions above, but it does it job.

``````const getSequences = (list, sum, sequences={}, curSequence=[], index=0) => {
for (; index < list.length; index++) {
const seqSum = curSequence.reduce((s, value) => {
return s + value;
}, 0);

if (seqSum + list[index] > sum) {
continue;
}

if (seqSum + list[index] == sum) {
const matchSeq = [...curSequence, list[index]];
const hashCode = matchSeq.reduce((s, value) => {
return s + ',' + value;
}, '');

if (!(hashCode in sequences)) {
sequences[hashCode] = matchSeq;
}

continue;
}

const newSequences = getSequences(list, sum, sequences, [...curSequence, list[index]], index+1);
for (let [hashCode, sequence] of Object.entries(newSequences)) {
sequences[hashCode] = sequence;
}
}

return sequences;
};``````

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.