Meta Interview Question

write a function which returns a fibonacci number for a given number n.

Interview Answers

Anonymous

Oct 25, 2018

def fib(n): if n == 0: return 0 elif n == 1: return 1 else: return fib(n-1)+fib(n-2)

Anonymous

Oct 25, 2018

def fib(n): if n==0: return 0 elif n==1: return 1 else: x,y=0,1 while n>1: tot = x+y x=y y=tot n-=1 return y print(fib(999999))

Anonymous

Mar 21, 2019

java solution public static int fib(int n) { if(n == 0) return 0; int numN1 = 1, numN2 = 0, sum; //numN1 represents fib(n-1), numN2 represents fib(n-2) //at the end, we are adding values starting from 0, 1 //fib(2) = fib(1) + fib(0) = 1 + 0 = 1 //fib(3) = fib(2) + fib(1) = 1 + 1 = 2; //fib(4) = fib(3) + fib(2) = 2 + 1 = 3; //fib(5) = fib(4) + fib(3) = 3 + 2 = 5; for(int index = 2; index <= n; index++) { sum = numN1 + numN2; numN2 = numN1; numN1 = sum; } return numN1; }

Anonymous

Apr 24, 2019

What were the two coding problems asked during phone interview?