Google Interview Question for Software Developers


Country: United States
Interview Type: In-Person




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

leetcode 494.
take a look on the 282 - similar

- tyler_ua July 11, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Java code ?

- Pedro July 11, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I would suggest doing something like this...
1 send the ans, arr, index and current ans recursively
2 the good thing about using recursion is it reduces complexity of the idea
3 using OR '|' operator ensures that the program does not compute
through all combinations since it greedily returns true if it encounters a true
midway without processing the rest of the code

private static boolean isMgic(int ans, int[] arr)
{
    return isMgic(ans, arr, 0, 0);
}

private static boolean isMgic(int ans, int[] arr, int index, int tmp)
{
    if (arr.length > index)
        return (isMgic(ans, arr, index + 1, tmp + arr[index])
                | isMgic(ans, arr, index + 1, tmp - arr[index]));
    if (ans == tmp)
        return true;
    return false;
}

- PeyarTheriyaa July 11, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This problem can be proven to be NP-complete. There is a pseudo-polynomial algorithm (subset sum) that can solve it.

- Marcos July 11, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I came up with this:

def is_magic(seq, magic_number):
    # sum(seq) = sum_pos + sum_neg
    # if there is a solution:
    # sum_positive - sum_negative = magic_namber
    # Therefore, the task can be formulated as looking for a subset of seq such that:
    # sum_neg = (sum(seq) - magic_number)/2

    check_list = seq[1:]  # The first element of seq cannot be negative
    tmp = sum(seq) - magic_number
    if tmp % 2 != 0 or tmp < 0:
        return False

    # pseudo-polynomial algorithm see wikipedia Partition_problem
    target = tmp // 2
    n = len(check_list)
    table = np.zeros((target + 1, n + 1), dtype=bool)
    table[0, :] = True
    for i in range(1, target + 1):  # O(target)
        for j in range(n):  # O(N)
            if i - check_list[j] >= 0:
                table[i, j + 1] = table[i, j] or table[i - check_list[j], j]
            else:
                table[i, j + 1] = table[i, j]

    return table[target, n]

- Flint July 15, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I came up with this using Python

def is_magic(seq, magic_number):
    # sum(seq) = sum_pos + sum_neg
    # if there is a solution:
    # sum_positive - sum_negative = magic_namber
    # Therefore, the task can be formulated as looking for a subset of seq such that:
    # sum_neg = (sum(seq) - magic_number)/2

    check_list = seq[1:]  # The first element of seq cannot be negative
    tmp = sum(seq) - magic_number
    if tmp % 2 != 0 or tmp < 0:
        return False

    # pseudo-polynomial algorithm for Partition_problem
    target = tmp // 2
    n = len(check_list)
    table = np.zeros((target + 1, n + 1), dtype=bool)
    table[0, :] = True
    for i in range(1, target + 1):  # O(target)
        for j in range(n):  # O(N)
            if i - check_list[j] >= 0:
                table[i, j + 1] = table[i, j] or table[i - check_list[j], j]
            else:
                table[i, j + 1] = table[i, j]

    return table[target, n]

- Flint July 15, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The optimal solution should be the one using pseudo-polynomial algorithm for the partition problem. Hope this is correct:

def is_magic(seq, magic_number):
    # sum(seq) = sum_pos + sum_neg
    # if there is a solution:
    # sum_positive - sum_negative = magic_namber
    # Therefore, the task can be formulated as looking for a subset of seq such that:
    # sum_neg = (sum(seq) - magic_number)/2

    check_list = seq[1:]  # The first element of seq cannot be negative
    tmp = sum(seq) - magic_number
    if tmp % 2 != 0 or tmp < 0:
        return False

    # pseudo-polynomial algorithm for the partition problem
    target = tmp // 2
    n = len(check_list)
    table = np.zeros((target + 1, n + 1), dtype=bool)
    table[0, :] = True
    for i in range(1, target + 1):  # O(target)
        for j in range(1, n + 1):  # O(N)
            if i - check_list[j - 1] >= 0:
                table[i, j] = table[i, j - 1] or table[i - check_list[j - 1], j - 1]
            else:
                table[i, j] = table[i, j - 1]

    return table[target, n]

- Flint July 15, 2018 | 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