## Interview Question for SDE-2s

• 3

Country: Russia

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

the question is just to ask a fibonacci sequence actually

``````public static int readingBook (int n){
if (n == 0) {
return 0 ;
}
int a = 1 ;
int b = 1 ;
for (int i = 2 ; i <= n ; ++i) {
int tmp = b ;
b = a + b ;
a = tmp ;
}
return b ;``````

}

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

I missed the fibionacci trick and used binomial coefficient. Works!

``````def choose(n, k):
return math.factorial(n)/(math.factorial(k)*math.factorial(n-k))

twos = n/2
combos = 0
while twos >= 0:
ones = n - twos*2
combos += choose(twos + ones, ones)
twos -= 1
return combos``````

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

If there are another type of reading we could change solution like that:
ways(p) = ways(p-m)+ways(p-n)+....ways(p-s) ,
where ways(i) - number of ways we can read i pages
p - number of pages, m,n....s - ways we can read book - m pages per minute, n pages and so on.

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

``````// ZoomBA calculating Fibonacci( n )  :: beat that
#(l,s) = lfold([2:n+1] , [1,0] ) ->{
[ \$.p.0 + \$.p.1 , \$.p.1]
}
println( l ) // n'th fib``````

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

c++, fibonacci, O(log n)

``````typedef unsigned long long UINT64;

UINT64 fibonacci(int n) {
int k;
UINT64 a, b, ret;

if (n <= 2) return 1;

k = n / 2;
a = fibonacci(k + 1);
b = fibonacci(k);

if (n % 2 == 1) ret = a * a + b * b;
else ret = b * (2 * a - b);

return ret;
}

return fibonacci(n + 1);
}``````

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

really, the trick is to understand that the task is about Fibonacci sequence.

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

Count the number of leafs on recursion tree

``````def read_ways_count(x):
if x == 0:
return 1

if x < 0:
return 0

res = 0

res = c1 + c2

return res``````

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

How will you change your solution if we introduce another type of reading?

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.

### 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.