Google Interview Question for Software Engineer / Developers






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

consider moving 1 in different position. When we put 1 at position 2, 3, ... n-1, it has no contribution to both view. When it is put at position 1, it add 1 to the left view and when it is put at n, it adds 1 to right view. Hence We have
F(n, L, R) = (n - 2)*F(n-1, L, R) + F(n-1, L-1, R) + F(n-1, L, R - 1)

- Anonymous January 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

nice but not optimized.

you don't need to keep both L & R in your recursion. You only need one.

- geniusxsy January 21, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Whats the answer to this qstn ?
Also what is the logic behind this equation

I am unable to figure it out..any help is appreciated

- Nick February 15, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

How to make it optimized by using only one of L and R in recursion?

- Prateek May 29, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I don't think this may be done without handling both, L,R. You cannot tell much about L when having information about R.

- mbriskar June 02, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>

int fun(int n,int L,int R)
{

if ( n <= 0 )
{
return 0;
}
else if( L > n or R > n)
{
return 0;
}
else if ( n == 1 && R == 1 && L == 1)
{
return 1;
}
else
{
return ((n-2)*fun(n-1,L,R) + fun(n-1,L-1,R) + fun(n-1,L,R-1));
}
}

int main()
{
int n = 3;
int L = 2;
int R = 2;
int ret;
ret = fun(n,L,R);
printf("\n %d \n",ret);

return 0;
}

If my solution is right then we can optimized it using dynamic programming

- anonymus January 22, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You need to consider the case when R=0, but L>0..

- Alvinup February 02, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

consider the place where you will put the block with height 1. Therefore,

F(N, L, R) = F(N-1, L-1, R) + F(N-1, L, R-1) + (N-2) F(N-1, L, R)

As this block has no contribution to L and R unless it is placed at left
most or right most.

- cplusmad April 28, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The question just asks the number of ways that this can be arranged in not a program.

If seeing from the left causes only L blocks to be seen, the Lth block has to be the tallest block seen. So the Lth block is already chosen. Similarly from the right if only R blocks are to be chosen, the Rth block is the tallest block that can be seen. So from either end the last block seen is the tallest block(Nth block). So the number of ways this can be arranged is

(N-1) C (L-1) * (N-L) C (R-1) + (N-1) C (R-1) * (N-R) C (L-1)

- Gautam July 05, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int permutation(int n, int k)
{
int u = 1;
for(int i=0; i<k; i++)
{
u *= n-i;
}
return u;
}

int combination(int n, int k)
{
return permutation(n, k)/permutation(k,k);
}

int seeWaysOfTopKFromOneSide(int n, int k)
{
if (n == k)
return 1;
if (n < k)
{
return 0;
}

if (k == 1)
return 1;

int c = 0;
for(int m=0; m<n; m++)
{
c+= combination(n-1, m) * seeWaysOfTopKFromOneSide(m, k-1) * permutation(n-m, n-m);
}
return c;
}

int seeWaysOfTopKFromBothSides(int n, int l, int r)
{
int c = 0;
for(int m=0; m<n; m++)
{
c+= combination(n-1, m) * seeWaysOfTopKFromOneSide(m, l-1) * seeWaysOfTopKFromOneSide(n-m-1, r-1);
}
return c;
}

- BIN December 31, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.


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