AT&T Interview Question

Write an algorithm to compute the nth fibonacci number

Interview Answers

Anonymous

Oct 21, 2018

A better solution would be to solve this with dynamic programming. Keep track of the previous 2 numbers to get the next in the sequence. This cuts down the complexity from O(n^2) in the recursive approach to O(n).

1

Anonymous

Oct 18, 2018

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