Fibonacci number
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
- 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!
Fibonacci number的更多相关文章
- Buge's Fibonacci Number Problem
Buge's Fibonacci Number Problem Description snowingsea is having Buge’s discrete mathematics lesson, ...
- [UCSD白板题] The Last Digit of a Large Fibonacci Number
Problem Introduction The Fibonacci numbers are defined as follows: \(F_0=0\), \(F_1=1\),and \(F_i=F_ ...
- [UCSD白板题 ]Small Fibonacci Number
Problem Introduction The Fibonacci numbers are defined as follows: \(F_0=0\), \(F_1=1\),and \(F_i=F_ ...
- (斐波那契总结)Write a method to generate the nth Fibonacci number (CC150 8.1)
根据CC150的解决方式和Introduction to Java programming总结: 使用了两种方式,递归和迭代 CC150提供的代码比较简洁,不过某些细节需要分析. 现在直接运行代码,输 ...
- 求四百万以内Fibonacci(number)数列偶数结果的总和
又对啦...开心~~~~ 只是代码可能不符合PEP标准什么的... Each new term in the Fibonacci sequence is generated by adding the ...
- Algorithms - Fibonacci Number
斐波那契数列(Fibonacci Number)从数学的角度是以递归的方法定义的: \(F_0 = 0\) \(F_1 = 1\) \(F_n = F_{n-1} + F_{n-2}\) (\(n \ ...
- 【LEETCODE】44、509. Fibonacci Number
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- 【leetcode】509. Fibonacci Number
problem 509. Fibonacci Number solution1: 递归调用 class Solution { public: int fib(int N) { ) return N; ...
- fibonacci number & fibonacci sequence
fibonacci number & fibonacci sequence https://www.mathsisfun.com/numbers/fibonacci-sequence.html ...
随机推荐
- 5. jdk路径配置
path , classpath 的配置及作用? 1) PATH环境变量.作用是指定命令搜索路径,在i命令行下面执行命令如javac编译java程序时,它会到PATH变量所指定的路径中查找看是否能找到 ...
- eclipse启动tomcat无法访问的解决方法
转自:https://www.cnblogs.com/longshiyVip/p/4637680.html 问题:: tomcat在eclipse里面能正常启动,但在浏览器中访问http://loca ...
- nginx 域名绑定
单个域名 upstream web{ server ;//这里绑定你要访问的服务器地址 keepalive ; } server { listen ; server_name www.xxxx.con ...
- windows10配置java开发环境
一.下载jdk 二.安装jdk路径,这个路径不能包含中文名 三.系统会提示安装jre,安装目录不要是jdk的安装目录,否则会覆盖掉jdk目录下的jre目录 四. .;%JAVA_HOME%\lib;% ...
- Mybatis知识(3)
1.JDBC编程有哪些不足之处,MyBatis是如何解决这些问题的? ① 数据库链接创建.释放频繁造成系统资源浪费从而影响系统性能,如果使用数据库链接池可解决此问题. 解决:在SqlMapConfig ...
- 在Linux Bash通过上下键快速查找历史命令
在centos 7中 ~/.bashrc 或者Mac中的 ~/.bash_profile 中添加,然后source一下以下内容: if [[ $- == *i* ]] then bind '" ...
- 【343】MathJax、LaTex、Mathml 数学公式
参考:cnblog中添加数学公式支持 分类参考: 1. 基本功能 MathJax 我的LaTeX入门 MathJax basic tutorial and quick reference 分段函数:矩 ...
- jsp常见的指令总结
一.三个编译指令 1.page指令: 首先,我们要明确一点就是page指令是一个全局指令,针对当前页面,其次我们再来深挖他的功能,它到底有哪些功能那,在我们程序中起到什么作用??? a.语法结构:&l ...
- 前端编程工具WebStorm 10 工具的快捷使用方式
1.如果是一个空白的文档,要想快速生成HTML的基本结构,可以写一个! 然后按一下tab键,如果是写的一个标签的名字,则会生成基本的标签结构. 2.h1{}:{}中写要显示的文本 3.h1[]:[]中 ...
- 吴裕雄 实战PYTHON编程(8)
import pandas as pd df = pd.DataFrame( {"林大明":[65,92,78,83,70], "陈聪明":[90,72,76, ...