Google Interview Question for Software Engineer in Tests






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

for n=1, its one way
for n=2, its 2 ways
for n=3, its 3 ways
for n=4, its 5 ways
for n=5, its 8 ways
for n=6, its 13 ways, so on

It is a fibonacci series...
f(n) = f(n-1) + f(n-2)

- Uday October 07, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 vote

It is the Fibolasic Problem F(n)=F(n-1)+F(n-2)

- Carson October 07, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
3
of 0 votes

Indeed.

W(n) = W(n-1) + W(n-2)
where W(1)=1
W(2)=2

Number of ways to reach nth floor=Number of ways to reach (n-1)th floor (take 1 step after reaching n-1th floor)
or
Number of ways to reach nth floor=Number of ways to reach (n-2)th floor (take 2-step after reaching n-2th floor)

- Surana October 14, 2008 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

here is nice post on this question
campuscoke.blogspot.in/2014/12/given-n-stairs-how-many-number-of-ways.html

- Jack December 10, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The solution is analogous to the linear equation in 2 variable.
a+2b=n, n known, 0<a<=n, 0<b<=n/2
a can take (n+1) places, b can take (n/2+1)
Total cases = (n+1) * (n/2+1)

Please comment

- YetAnotherCoder October 07, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

to reach on floor it may go with one step or two step , so person has to choices.

lets take small example. if person wants to reach on 5th floor from ground then how many ways he can reach.

to reach ground to first floor - 2 ways ( one step or two step )
to reach first to second floor - 2 ways ( one step or two step )
to reach second to third floor - 2 ways ( one step or two step )
to reach third to forth floor - 2 ways ( one step or two step )
to reach forth to five floor - 2 ways ( one step or two step )

to total way = 2*2*2*2*2
similar case for n = 2*2*2......n = 2 power of n

- Deepak Garg October 07, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hi Deepak,
But check the case with reaching 3rd floor:
you have only 3 cases:
first, take all one steps
second, take a one step initially and followed by a two step
third, take a two step initially and followed by a one step

thats all...
Please correct me if am wrong..

- Uday October 07, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi You forgot one case:
Take all 2 steps.
And thus that makes this as 4 steps
Actually the solution above by Deepak is correct.
Here in your example for two floors we are having 2*2 = 4 steps.

- Rahul October 08, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

how can u reach floor three by taking two 2 steps??

- Anonymous March 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

how can u reach floor 3 by taking two 2 steps??

- Anonymous March 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

it's 2 to the power of (n - 1). To get to the 3 rd floor from the first floor you need 4 ways

- Anonymous October 08, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Well, I am not sure if I am reading too much in the question, but it says N floors and not N steps. So to get to the 1st floor, either you can take 1 or 2 steps, so you would require X: 1 steps and Y: 2 steps.. so to get to the Nth floor you need NX + NY steps and Y steps = 2X thus, you need 3NX steps or 1.5NY steps... i duno.. please correct me if I am wrong..

- SN October 08, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

dear Sn,
I think it seeems from Q that to reach a particular floor he can go 1 0r 2 step . so his step size should be same.
But you can take it otherwise too as you have taken.
Good!

- Ratn October 09, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Dear Sn,

If you require X number of 1-steps and Y number of 2-steps ,
then Y < X .
Hence, X = 2Y
So, to reach the nth floor, you need 3NY or 1.5NX steps.

- Sneha October 11, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is a permutation / combination problem. (rather only permutation)...We need to find the number of ways to reach the nth floor with 1 or 2 steps at a time.

So, if n=6, we can have
1 + 1 + 1 + ...
1 + 2 + 1 + ... and so on..

Draw a tree and u could see that at each level, there is a node for either 2 or 1. Chk the current sum at each node. Its hence a recursive program with the following DS;

struct StepNode
{
int curr_sum;
int *arr // if they want the type of combinations
};

- nanmaga October 11, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Number of integral solution for the below mention Linear Equation in two variable
a+2b=n, n known, 0<a<=n, 0<b<=n/2
a= n-2b; (1)
n >= n-2b >=0 implies 0<=b<=n/2
Total Integral solution for (1) equation is
(n-2b+1-1)C(1-1) = n-2b;
Now, b varies from 0 to n/2
Summation : i = 0 to n/2 , n-2i
n*(n+2)/2 - n(n+2)/4 = n*(n+2)/2

- Ankush Bindlish November 05, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Number of integral solution for the below mention Linear Equation in two variable
a+2b=n, n known, 0<a<=n, 0<b<=n/2
a= n-2b; (1)
n >= n-2b >=0 implies 0<=b<=n/2
Total Integral solution for (1) equation is
(n-2b+1-1)C(1-1) = n-2b;
Now, b varies from 0 to n/2
Summation : i = 0 to n/2 , n-2i
n*(n+2)/2 - n(n+2)/4 = n*(n+2)/4

- Ankush Bindlish November 05, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

DP :

step(n)
{
1 + max(step(n-1),step(n-2))
}

- Anonymous January 14, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

As per the question, we are required to find out TOTAL NUMBER OF DIFFERENT WAYS to reach from 1st floor to Nth Floor. We are not calculating the total number of STEPs needed to reach the Nth floor. Hence this question becomes a permutation & combination problem.

The correct answer should be power(2, n-1). "Deepak Garg" has explained it and "Anonymous" has corrected it.

Solutions (a+2b = n) and fibonacci numbers are not correct.

Fibonacci Number Solution :

From Floor 1 to Floor 3 :
a. (1,1,1)
b. (1,2)
c. (2,1)

Total number of ways is 3. In this solution, only (a) is correct. In (b) & (c) we are not even traversing 3 floors.

Hence solutin of fibonacci numbers does not work.

We can come up with similar arguments for linear equation solution.

- Nachiketha February 18, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

power(2, n-1) fails.
Say n = 4
the possible combinations are
1234
134
124
234
24
power(2, n-1) gives you 8

- Amit February 19, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

What I understand from the problem stmt, to reach 1 floor if there are 'm' steps, person can take 1 or 2 steps at a time to reach 1st floor. Thus for 'n' floors, there are total 'nm' steps. If a person takes 1 step throughout, then he will be climbing 'nm' steps to reach nth floor, while if he takes 2 steps at a time, he will have to climb 'nm/2' steps. Thus total no. of ways to climb nth floor are 'nm - nm/2' = 'nm/2'.
Correct me if I am wrong.

- Anonymous March 02, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I was wrong. Each of the 'nm/2' steps can be taken as 1 step or 2 steps. Thus 2 ^ (nm/2) possible ways to climb.

- Anonymous March 02, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

well somebody here needs to check their algorithm before declaring it as a solution. Like check the fact that the answer can be ODD so IT ISNT 2^ANYTHING!!!!!!!!!!!!11

- goober March 19, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{well somebody here needs to check their algorithm before declaring it as a solution. Like check the fact that the answer can be ODD so IT ISNT 2^ANYTHING!!!!!!!!!!!!}

- goober March 19, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It looks to me that there are two possible interpretations of this:

Q1) Taking 1 or 2 steps, how many ways to reach the nth step?
A) Fibonacci.

Q2) Taking 1 or 2 steps to next floor, how many ways to reach the nth floor?
A) Power of 2. Two ways to reach next floor.

Interpretation 2 does not really make sense in terms of a puzzle (or real life... 1 or 2 steps and you are already up one floor!?), but that is what the original poster seems to have written. Yet another case of bad problem statement.

IMO, the original poster goofed up and intended Q1 all along.

- T March 19, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Ppl, Isn't some data missing in this question? Say, the number of steps to climb up one floor perhaps. ie No.of steps/floor. This data is required to answer the question the way it is worded.

- Manish Krishnan April 30, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I guess the answer might be Summation from b=0 to b=n/2 of (n-b)!/a!b!

The number of steps/floor should not matter. ( assume there are k steps/floor , so total steps now are n*k=m. So instead of n now we have m, butthe problem remains same)

Let's consider this:
We need to climb n steps, using a number of 1 steps and b number of 2 steps.
so
a*1 + b*2=n
a+2b=n

ok,
now given a value of n, above equation can be satisfied by many possible value pairs (a,b).

Let's consider one such pair.
For this particular pair, how many possible ways are there to reach n?
Let's take an example?
if n=5, a=3 and b=1
the question is how many possible ways can I arrange 3 one's and 1 two?
this is typical combination/permutation problem with standard formula answer
(n-b)!/a!b!

so given a value pair a and b, there are (n-b)!/a!b! ways to reach step n.
so the second part is, how many such value pairs are possible? and for each such value pair calculate (n-b)!/a!b!.

not as lengthy as an explaination as I would like. but hth.

- morpheus September 22, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I have no idea what the fuss is all about. It is a simple Fibbonacci series.

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

You can reach the nth floor either from n-1 st floor or directly from n-2 nd floor. so total ways to reach nth floor = ways to reach n-1 st floor + ways to reach n-2 nd floor.

- jaiHo September 30, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

unsigned int countPaths(int height,int cur_step)
{
 static int tot=0;
 if(cur_step < height){
   countPaths(height,cur_step+1);
   countPaths(height,cur_step+2);
 }
 else if (cur_step==height){
   ++ tot;
 }
 return tot;
}

- Anonymous November 18, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It is a simple Fibbonacci series.

f(n) = f(n-1) + f(n-2)+2
where f(x) indicate how many u can go to x th floor..
You can reach the nth floor either from n-1 st floor or directly from n-2 nd floor. so total ways to reach nth floor = ways to reach n-1 st floor + ways to reach n-2 nd floor.

- dd October 23, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

after reaching n-1th floor there is only one way to reach nth floor;after reaching (n-2)th floor there are 2 ways to reach the nth floor...hence total 3 ways

f(n)=f(n-1)+f(n-2)+3

please tell me if that is wrong ?

- adhi January 12, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Adhi, it is exactly fibonacci series.

reason is
1. if n = 1, reaching 1st floor is only one way (in one step) so f(1) = 1
2. if n = 2, reaching 2nd floor is two ways (take two steps one a time or take two steps once) so f(2) = 2

f(3) = f(2) + f(1)...

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

perfectly fibonacci series.

- yours.ramesh February 01, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Surprising to see how many people got this question wrong!
It is really simple if you just think about it....

In order to go up one floor (N=1), there are 2 ways (1 step or 2 steps)
in order to go two floors (N=2), There are 4 ways (11,12,21,22 steps)
in order to go three floors (N=3), There are 8 ways (111,121,211,221,112,122,212,222)
so there are 2^N posibilities. If someone starts on the 1st floor, there are 2^8 = 256
possibilities. If someone starts from the basement, there are 2^9 = 512 possibilities.

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

i think question is not about going to n floor it is going to nth stair
so if i want to go to 3rd stair with 121 combination i will be on 4th
what answer needed is number of ways this can be achieved

- pathashala06 August 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can any buddy give me a complete code actually i have to submit assignment of shell on monday so dont want to waste time for this program simple my question has modified fibbonaci instead of 1 ,2 i have 1 , 2 , 3 no of stairs that i can climb together.

- pathashala06 August 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I viewed it as generating strings from length n/2 (just "2"s) to n (just "1"s) and came up with that formula (use as query term in wolframalpha.com to display):

sum (n-k) choose k, k=0 to n/2

Explanation: For n=6 stories I can go "222" (l = 3 = n/2), "111111" (l = n = 6), "2211" (l = 4 = n/2 + 1), "2121", "2112", "1212", ..., "21111" (l = 5 = n/2 + 2), ..., "11112".
In each String of length l (which can go from n/2 to n -- 3 to 6 in the example) i can "choose" only (n - l) possitions to take 2 steps at once.
In the example: (3 choose 3) + (4 choose 2) + (5 choose 1) + (6 choose 0) = 13

I read that it is the fibonacci series, but seriously -- how could one come up with this based on that question?!?

- gg June 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Assume each floor has m steps. Each floor can be reached in Fib(m) ways as given in the first answer. For for reaching nth floor as per permutation it should be (Fib(m))^n

- RockRaj July 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int no_of_ways(int n){
    
    if(n == 1) return 1;
    
    if(n == 2) return 2;
    
    return no_of_ways(n-1) + no_of_ways(n-2);
     
 
}

- nondescript October 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hey guys.....
I would like to post a query here....
Please response to it on my e-mail address : godharsh011@gmail.com
The question is ....There is a mat consisting of 11 steps (it means a person can take 11 steps to cross it ) . A person wishes to cross it . He may take 1 step or 2 steps at a time and may use any combination of these two . Find the total number of ways he can cross the mat .
.
.
Guys please post the answers with explaination.......
Waiting for your answers on my e-mail address...............

- harah raj October 22, 2015 | 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