Google Interview Question for Software Engineer / Developers


Country: India




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

what is the index here? their index in their row or is it index starting from the topmost cup?
PLease post google questions with more clarity, they deserve it

- Anonymous February 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

http ://careercup.appspot.com/question?id=12217186

- Anonymous February 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Triangle of Yang...
1
1 2 1
1 3 3 1
1 4 6 4 1
so the answer is obvious: extra * (1/C(h-1,i-1))

- stupid February 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Triangle of Yang...
1
1 2 1
1 3 3 1
1 4 6 4 1
so the answer is obvious: extra * (1/C(h-1,i-1))

- stupid February 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

sorry ,it's extra * (C(h-1,i-1)/(2^(h-1)))... Am I Wrong

- stupid February 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

sorry ,it's extra * (C(h-1,i-1)/(2^(h-1)))... Am I Wrong

- stupid February 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Every node has to have two children.. why does the figure show otherwise? :O

- Dev February 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Please find it below address:

careercup.com/question?id=12217186

- googlybhai February 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here's an easy solution that lazily enumerates the amount in each cup in level order:

def cascade(total, limit):
    row = [float(total)]
    while 1:
        new_row = (len(row) + 1) * [0.0]
        for i, amount in enumerate(row):
            excess = max(0, amount - limit)
            yield amount - excess
            if excess:
                overflow = 0.5 * excess
                new_row[i] += overflow
                new_row[i+1] += overflow
        row = new_row

With this algorithm, finding the amount in the n first cups takes O(n) time and O(sqrt(n)) space. It also generalizes easily in various directions. One generalization is to arbitrary cascades represented as directed acyclic graphs. Another generalization is allowing arbitrary limits per cup rather than a fixed limit.

This is effectively a breadth-first traversal. The related question that googlybhai linked to contains a linear-time and log-space solution which is essentially the same algorithm using instead a depth-first traversal. However, that assumes that the cascade structure is presented to us explicitly as a tree and that we can cache values on the nodes. Without that assumption, creating the tree would require O(n) space, which is obviously worse than the O(sqrt(n)) space used in my algorithm.

- psykotic February 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@psykotic : in the provided link by googlybhai... whose code will give correct output with O(N) time.

- atul February 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes, so will mine. As I pointed out, that person's code assumes an explicit tree representation of the triangle structure, which is not given here.

- atui February 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public List<double> PourWater(int water)
        {
            List<double> result = new List<double>(){0};
            while (water > 0)
            {
                PourWater(result, 1, 0, 0);
                water--;
            }

            return result;
        }

        public void PourWater(List<double> cups, double water, int h, int i)
        {
            int index = h * (h + 1) /2 + i ;

            if (cups[index] + water > 1)
            {
                if (cups.Count <= index + h + 1)
                {
                    cups.AddRange(new double[h + 2]);
                }

                cups[index] += water - 1;
                PourWater(cups, 0.5, h + 1, i);
                PourWater(cups, 0.5, h + 1, i + 1);
            }
            else
            {
                cups[index] += water;
            }
        }

- Quan Li April 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 votes

static final int capacity = 1;

public static double amount(double total, int h, int i){
if(h==0 || i==0) return 0;
if(h==1 && i==1) return total;
return (amount((total-capacity)/2, h-1, i-1) + amount((total-capacity)/2, h-1, i));
}

- Anonymous February 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 votes

why do you think it's O(log n)? It's not a binary tree where each node has two child.

For the worst case, you may need to apply the function to more than half of the cups, which is already O(n)

Also, you might have applied the function multiple times to the same cup....

- Hei May 05, 2012 | Flag


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