Fibonacci number

https://github.com/Premiumlab/Python-for-Algorithms--Data-Structures--and-Interviews/blob/master/Mock%20Interviews/Large%20Search%20Engine%20Company%20/Search%20Engine%20Company%20-%20Interview%20Problems%20-%20SOLUTIONS/Phone%20Screen.ipynb

Phone Screen

This phone screen will consist of a non-technical series of questions about you and the company, and then a second half of a simple technical question to be coded out


Non-Technical Questions

Answer the following questions (2-5 minute responses) technical answers not required, more interested in hearing about your reasoning

  • Give me some quick background about you (go over your resume)
  • Why do you want to work here?
  • What‘s your favorite programming language and why?
  • Where do you see yourself in 5 years?
  • Do you have any questions about the company for me?

Solution

There aren‘t really any "correct" answers here, just make sure you‘re prepared to answer the following questions about the company you‘re interviewing with. Be honest, friendly and ready to defend any statements you make with logical arguments to back them up. Note, you should ALWAYS have follow-up questions.


Technical Questions

Answer the following question in the Markdown cell below. It‘s important to note that the cell below does NOT have syntax highlighting, its common in a phone screen interview to be given a text editor hich doesn‘t have anything more than basic text support

  1. Write a function that computes the Nth fibonacci number

Solution

There‘s many ways to answer this question, you might be required to solve it multiple ways and discuss some pros and cons of each way. Listed below are various solutions

## Example 1: Using looping technique
def fib(n):

    a,b = 1,1
    for i in range(n-1):
        a,b = b,a+b
    return a

print fib(7)

# Using recursion
def fibR(n):
    if n==1 or n==2:
        return 1
    return fib(n-1)+fib(n-2)

print fibR(7)

## Example 3: Using generators
a,b = 0,1
def fibI():
    global a,b
    while True:
        a,b = b, a+b
        yield a
f=fibI()
f.next()
f.next()
f.next()
f.next()
f.next()
f.next()
print f.next()

## Example 4: Using memoization
def memoize(fn, arg):
    memo = {}
    if arg not in memo:
        memo[arg] = fn(arg)
    return memo[arg]

## fib() as written in example 1.
fibm = memoize(fib,7)
print fibm

## Example 5: Using memoization as decorator
class Memoize:
    def __init__(self, fn):
        self.fn = fn
        self.memo = {}
    def __call__(self, arg):
        if arg not in self.memo:
            self.memo[arg] = self.fn(arg)
            return self.memo[arg]

@Memoize
def fib(n):
    a,b = 1,1
    for i in range(n-1):
        a,b = b,a+b
    return a
print fib(7)
13
13
13
13
13

Below is a table depicting averaged relative performance time in seconds over 10 runs to caluclate the 15000th fibonacci number.

Fib(n=15000)
loops recursion generators memoization memoization as decorator
45 87 58 44 43
47 88 58 42 42
51 92 60 44 43
43 87 58 42 43
48 92 61 42 44
45 87 59 43 44
44 85 57 42 44
44 87 62 43 43
48 86 59 42 43
45 91 61 45 45
46 88.2 59.3 42.9 43.4 (Avg)

Good Job!

时间: 2024-12-02 20:43:07

Fibonacci number的相关文章

【HDOJ】3509 Buge's Fibonacci Number Problem

快速矩阵幂,系数矩阵由多个二项分布组成.第1列是(0,(a+b)^k)第2列是(0,(a+b)^(k-1),0)第3列是(0,(a+b)^(k-2),0,0)以此类推. 1 /* 3509 */ 2 #include <iostream> 3 #include <string> 4 #include <map> 5 #include <queue> 6 #include <set> 7 #include <stack> 8 #incl

hdu 3509 Buge&#39;s Fibonacci Number Problem

点击此处即可传送 hdu 3509 题目大意:F1 = f1, F2 = f2;; F(n) = a*F(n-1) + b*F(n-2); S(n) = F1^k + F2^k +-.+Fn^k; 求S(n) mod m; 解题思路: 1:首先一个难题就是怎么判断矩阵的维数(矩阵的维数是个变量) 解决方法:开一个比较大的数组,然后再用一个公有变量记一下就行了,具体详见代码: 2:k次方,找规律: 具体上代码吧: /* 2015 - 8 - 16 晚上 Author: ITAK 今日的我要超越昨日

(斐波那契总结)Write a method to generate the nth Fibonacci number (CC150 8.1)

根据CC150的解决方式和Introduction to Java programming总结: 使用了两种方式,递归和迭代 CC150提供的代码比较简洁,不过某些细节需要分析. 现在直接运行代码,输入n(其中用number代替,以免和方法中的n混淆)的值,可以得出斐波那契数. 代码如下: /* CC150 8.1 Write a method to generate the nth Fibonacci number Author : Mengyang Rao note : Use two me

Buge&#39;s Fibonacci Number Problem

Buge's Fibonacci Number Problem Description snowingsea is having Buge’s discrete mathematics lesson, Buge is now talking about the Fibonacci Number. As a bright student, snowingsea, of course, takes it as a piece of cake. He feels boring and soon com

projecteuler----&gt;problem=25----1000-digit Fibonacci number

问题描述: The Fibonacci sequence is defined by the recurrence relation: Fn = Fn1 + Fn2, where F1 = 1 and F2 = 1. Hence the first 12 terms will be: F1 = 1 F2 = 1 F3 = 2 F4 = 3 F5 = 5 F6 = 8 F7 = 13 F8 = 21 F9 = 34 F10 = 55 F11 = 89 F12 = 144 The 12th term

[LeetCode] 509. Fibonacci Number

Description The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is, F(0) = 0, F(1) = 1 F(N) = F(N - 1) + F(N - 2), for N

[LeetCode] Fibonacci Number 斐波那契数字

The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is, F(0) = 0,   F(1) = 1 F(N) = F(N - 1) + F(N - 2), for N > 1. Given

Fibonacci Number LT509

The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is, F(0) = 0,   F(1) = 1 F(N) = F(N - 1) + F(N - 2), for N > 1. Given

Project Euler:Problem 25 1000-digit Fibonacci number

The Fibonacci sequence is defined by the recurrence relation: Fn = Fn?1 + Fn?2, where F1 = 1 and F2 = 1. Hence the first 12 terms will be: F1 = 1 F2 = 1 F3 = 2 F4 = 3 F5 = 5 F6 = 8 F7 = 13 F8 = 21 F9 = 34 F10 = 55 F11 = 89 F12 = 144 The 12th term, F1