Google Interview Question


Country: United States




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

Number of paths for part one = m ^ layers
Number of paths for part two = sum(i = 1 to layers) (m ^ i)
Where 'layers' is the number of layers considered i, i+1, etc

Edit:
The above approach is incorrect. After thinking about it some more, the actual count is

f(n) = m + m ( f (n -1) ) + f (n - 1)  when n > 0,  0 otherwise

where n is the number of layers.

Which roughly means
m possible paths if the only middle layer is layer 'n'
m ( f(n - 1) ) possible paths if layer 'n' is considered part of the path in combination with all the other layers < n
f(n -1) possible paths if layer 'n' is not part of the path using all the other layers < n

This simplifies to

f (n) = m + f (n -1) (m + 1)

- zortlord August 28, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is what I thought initially too. But I think this doesn't handle paths like this:

Consider 5 levels and 3 nodes per level (i.e. n = 5 and m = 3)

start ---> Choose one node from level 1 --> choose one node from level 4 --> sink

Can you check my answer and tell me I got it right? Thanks! :)

- june.pravin August 29, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You can't go from level 4 to directly sink. You would have to go to level 5 before you reach to sink.

- Mukesh Kumar August 30, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

1) m**n
2) (m + 1) ** (n - 2) * m * m if n > 1 else m

- Anonymous August 28, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The answers are m^n and (m+1)^n respectively. It can be calculated by mathematical induction.

- zhaotianzju August 28, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It is m^n and m*(m+1)^(n-1).

- Yola October 08, 2022 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This can be solved using DFS, right?

- coyg August 28, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Part one: m^n
Part two: m^n + m^(n-1) + m^(n-2) + ... m^(n-n) .... (Mathematical induction)

- june.pravin August 29, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Correcting this slightly

Part one: m^n
Part two: m^n + 2! x m^(n-1) + 3! x m^(n-2) + ... n! x m^(1)

- june.pravin August 29, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Close, but not quite there. Check out my answer below.

- cicciopuzzolo August 29, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The solution is Dynamic programming the you'll calculate the number of path from node i to the sick no suppose you are analysis a edge i -> j, here you have calculate from j to sink the the number of path from i to j is D[j], so the total number of path from i to sink is the sum of the path for all children of node i. Here is the code for a better understatement because my english is a mess lol.
int D[1000] Fill it with -1
int calc(int node){
if(node == sink) return 1;
if(D[node==-1]){
D[node] = 0;
for(over all children of node called next)
D[node] += calc(next);
}
return D[node];
}


the solution is calc(source)

- Vladimir August 29, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Part one is of course m^n.
Part two is sum_(i in 1..n)(m^i*(n choose n-i)).
Imagine paths that jump one level. The number of paths are in m^(n-1), but only for one level, hence you need to multiply by n (which is n choose 1).
Now imagine paths that jump two levels. The number of paths (if you fix which levels are being jumped) is m^(n-2), and this must be multiplied by the number of choices of the two levels to jump, that is n choose 2.
And so on and so forth :)

- cicciopuzzolo August 29, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Solution:
- f(0) = 1
- f(1) = m
- f(n) = m*f(n-1) = pow(m, n);
- f(i, j) = f(i-1) * f(m-j) = pow(m, n+i-j-1), where i < j and i and j <= n.(a jump connection i, j)

See more here: cpluspluslearning-petert.blogspot.co.uk/2015/08/find-number-of-paths-from-source-to.html

- peter tang August 30, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

First question, the total paths are m^n.
Second part, for each edge from level i to level i+k, there are m^(n-k-1) paths that use this edge, so this value is added to the total number of paths.

- gen-y-s August 30, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The answer will be -

m^2 + m^3 + m^4 + .... + m^n

How?

Let's consider the base case first -
n = 1, paths = m
n = 2, paths = m(m)
i.e. only possible paths are m*m
n = 3, paths = m(m + m(m))
i.e. we have three layers -
1. either directly reach the last layer (3nd layer) from first m nodes, paths = m*m
2. or reach via middle layer (1st layer), paths = m*m*m
so total paths = m*m + m*m*m
similarly, for n = 4
4 layers, 4 options
1. reach directly to 4th layer = m*m*m*m
2. reach via second last layer (3rd layer) = m*m*m
3. reach via second layer = m*m
total paths = m*m + m*m*m + m*m*m*m

- Anonymous August 30, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Couldn't be problem attacked in the following way? :
There are N levels by M nodes each( for simplicity now I ignore source and sink levels ). So we have 2 ^ N - 1 possible ways to find a way between first and last level (no taking into account when path go only through nodes sink and source). For each combination of levels L, there are M^K ways to get node, where K is count of all set bits in L.So number of ways between first and last level would be : for (int l = 1; l< 2^n ;l++) sum+=m^k; where k is number of set bits in l. We now have to take into account sink and source nodes , which provides us addtional ways - > m^2*sum + 1 if n > 1 or m+1 if n = 1- this is total ways.

- EPavlova September 02, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Couldn't be problem attacked in the following way? :
There are N levels by M nodes each( for simplicity now I ignore source and sink levels ). So we have 2 ^ N - 1 possible ways to find a way between first and last level (no taking into account when path go only through nodes sink and source). For each combination of levels L, there are M^K ways to get node, where K is count of all set bits in L.So number of ways between first and last level would be : for (int l = 1; l< 2^n ;l++) sum+=m^k; where k is number of set bits in l. We now have to take into account sink and source nodes , which provides us addtional ways - > m^2*sum + 1 if n > 1 or m+1 if n = 1- this

- epavlova September 02, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I used inductive logic to get an answer of m^n for part 1 and m (m+1)^(n-1).
This assumed you can only get to the sink from row i=n. But the source can jump to any i.

To check: assume m=10.
N=1->10
N=2->1 + 10 *1 +10*(10+1)
N=3->13310
etc.

- GoBuckeyes September 10, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The second part can be solved with dinamic programming, i think this function does the job

long long int NumberOfWays(int n, int m, vector<pair<int, int> > edges){
          vector<long long int> levels (n, 0);
          sort(edges.begin(), edges.end());
          v[0] = m;
          int j=0;
          for (int i=0;i<n-1;i++){
                v[i+1] +=v[i]*m;
                while(j<edges.size() and edges[j].first==i){
                      v[edges[j].second] += v[i]*m;
                       j++;
                 }
           }
          return v[n-1]*m;
}

- Nicoshev September 17, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

long long int NumberOfWays(int n, int m, vector<pair<int, int> > edges){
          vector<long long int> levels (n, 0);
          sort(edges.begin(), edges.end());
          v[0] = m;
          int j=0;
          for (int i=0;i<n-1;i++){
                v[i+1] +=v[i]*m;
                while(j<edges.size() and edges[j].first==i){
                      v[edges[j].second] += v[i]*m;
                       j++;
                 }
           }
          return v[n-1]*m;

}

- Nicoshev September 17, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think this a DP problem. Define dp[i] is the number of paths from source to level i, then we have dp[i] = m * (dp[1] + dp[2] + ... + dp[i - 1]) = m * dp[i - 1] + m * (dp[1] + dp[2] + ... + dp[i - 2]) = m * dp[i - 1] + dp[i - 1] = (m + 1) * dp[i - 1]

- malinbupt September 24, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Isn't the second question solved with binomial coefficients?

- Anonymous July 06, 2016 | 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