Google Interview Question for Software Engineers


Country: United States
Interview Type: In-Person




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

Looking for interview experience sharing and coaching?

Visit AONECODE.COM for private lessons by FB, Google and Uber engineers

Our ONE TO ONE class offers

SYSTEM DESIGN Courses (highly recommended for candidates of FB, LinkedIn, Amazon, Google & Uber etc.)
ALGORITHMS (conquer DP, Greedy, Graph, Advanced Algos & Clean Coding),
latest interview questions sorted by companies,
mock interviews.

Our students got hired from G, U, FB, Amazon, LinkedIn, MS and other top-tier companies after weeks of training.

Email us aonecoding@gmail.com with any questions. Thanks!

SOLUTION 1

public Integer random(int[] weights) {
        int totalWeight = 0;
        Integer selected = null;
        Random rand = new Random();
        for(int i = 0; i < weights.length; i++) {
            int r = rand.nextInt(totalWeight + weights[i]);
            if(r >= totalWeight) {
                selected = i;
                totalWeight += weights[i];
            }
        }
        return selected;
    }

FOLLOW-UP

//main
//RandomSelectTree tree = new RandomSelectTree();
//while(tree.n > 0) tree.randomPoll();

//Segment Tree O(nlogn)
public class RandomSelectTree {

    int[] tree;
    int[] weights;
    int n; //number of remaining balls

    RandomSelectTree(int[] weights) {
        tree = new int[2 * weights.length + 1];
        this.weights = weights;
        n = weights.length;
        buildTree(0, 0, weights.length - 1);
    }
    //build segment tree for weights[]
    private void buildTree(int root, int start, int end) { 
        if(start > end) return;
        if(start == end) {
            tree[root] = weights[start];
            return;
        }
        int mid = (start + end) / 2;
        int leftChild = 2 * root + 1, rightChild = 2 * root + 2;
        buildTree(leftChild, start, mid);
        buildTree(rightChild, mid + 1, end);
        tree[root] = tree[leftChild] + tree[rightChild];
    }

    public int randomPoll() {
        if(n == 0) {
            //Exception
        }
        int rand = new Random().nextInt(tree[0]);
        int start = 0, end = weights.length - 1, node = 0;
        while(start < end) {
        //chosen ball is between start to (start + end) / 2
            if(rand < tree[node * 2 + 1]) {
               node = node * 2 + 1;
                end = (start + end) / 2;
            } else { //chosen ball between (start + end) / 2 + 1 and end
                rand -= tree[node * 2 + 1];  //!!! narrow down to remaining range
                node = node * 2 + 2;
                start = (start + end) / 2 + 1;
            }
        }
        delete(start, node);
        return start;
    }

    //remove ball at idx.
    private void delete(int idx, int node) { 
       n--;
        while(node > 0) {
            tree[node] -= weights[idx];
            node = (node - 1) / 2;
        }
        tree[0] -= weights[idx];
    }

- aonecoding January 20, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.Random;

public class RandomBallExtraction {
    Random random = new Random();

    class Ball {
        public final int index;
        public final int weight;
        Ball(int index, int weight) {
            this.index = index;
            this.weight = weight;
        }
    }

    int[] chooseBalls(int[] weights) {
        int n = weights.length;
        int weightSum = 0;
        for (int i = 0; i < n; ++i) {
            weightSum += weights[i];
        }

        Ball[] balls = new Ball[n];
        for (int i = 0; i < n; ++i) {
            balls[i] = new Ball(i, weights[i]);
        }

        int[] sequence = new int[n];
        chooseBalls(sequence, 0, balls, weights.length, weightSum);

        return sequence;
    }

    void chooseBalls(int[] sequence, int index, Ball[] balls, int n, int weightSum) {
        if (n == 0) return;

        StringBuilder debug = new StringBuilder();
        debug.append("Debug: ");

        int x = random.nextInt(weightSum);
        debug.append("rand: " + x + ", ");

        Ball selected = null;
        for (int i = 0; i < n; ++i) {
            x -= balls[i].weight;
            if (x < 0) {
                debug.append("chosen: " + balls[i].index + ", weight: " + balls[i].weight);
                selected = balls[i];
                swap(balls, i, n - 1);
                break;
            }
        }

        System.out.println(debug);

        sequence[index] = selected.index;
        chooseBalls(sequence, index + 1, balls, n - 1, weightSum - selected.weight);
    }

    void swap(Ball[] balls, int i, int j) {
        Ball tmp = balls[i];
        balls[i] = balls[j];
        balls[j] = tmp;
    }

    public static void main(String[] args) {
        RandomBallExtraction rbe = new RandomBallExtraction();
        int[] weights = {5, 10, 35};

        for (int t = 0; t < 1000; ++t) {
            int[] sequence = rbe.chooseBalls(weights);

            for (int i : sequence) {
                System.out.println(weights[i] + " ");
            }
            System.out.println();
        }

    }
}

- Wonkyu Lee March 01, 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