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)

- Anonymous May 14, 2019 | Flag Reply
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)

- Mehdi May 14, 2019 | Flag Reply
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

- swapnilkant11 May 21, 2019 | Flag Reply
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;
};

- Fabio August 27, 2019 | Flag Reply
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;
};

- Fabio August 27, 2019 | Flag Reply


Add a Comment
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.

Learn More

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.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More