Interview Question for SDE-2s


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 ;

}

- Scott November 16, 2015 | Flag Reply
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))

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

- codestreaker November 17, 2015 | Flag Reply
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.

- EPavlova January 27, 2016 | Flag Reply
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

- NoOne October 08, 2016 | Flag Reply
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;
}

UINT64 casesOfReading(int n) {
	return fibonacci(n + 1);
}

- kyduke November 16, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Thank you for answers,
really, the trick is to understand that the task is about Fibonacci sequence.

- zr.roman November 17, 2015 | Flag Reply
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
        c1 = read_ways_count(x - 1)       
        
        c2 = read_ways_count(x - 2)

        res = c1 + c2

        return res

- dmytro.arkhypenko November 17, 2015 | Flag Reply
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?

- Iuri Sitinschi November 18, 2015 | 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