interview question- fibonacci series

50
{ { Fibonacci Fibonacci Problem Solving and Thinking in Problem Solving and Thinking in Engineering / Programming Engineering / Programming

Upload: sana-nasar

Post on 18-Jul-2015

255 views

Category:

Engineering


1 download

TRANSCRIPT

Page 1: Interview Question- Fibonacci Series

{{

Fibonacci Fibonacci

Problem Solving and Thinking in Problem Solving and Thinking in Engineering / ProgrammingEngineering / Programming

Page 2: Interview Question- Fibonacci Series

The Fibonacci Series is of interest and The Fibonacci Series is of interest and excitement to Mathematicians and Scientists.excitement to Mathematicians and Scientists.

The Series is:The Series is: 0,1,1,2,3,5,8,13,21,34,55,89,…0,1,1,2,3,5,8,13,21,34,55,89,…

To calculate a Fibonacci Number simply add To calculate a Fibonacci Number simply add the two previous numbers together.the two previous numbers together.

We always start with zero and one (We always start with zero and one (0 and 10 and 1))

Understand the ProblemUnderstand the Problem

Page 3: Interview Question- Fibonacci Series

High Level English Description (or Pseudocode High Level English Description (or Pseudocode Version 1)Version 1)

Calculate and Display the first Calculate and Display the first ‘‘XX’’ Fibonacci Fibonacci NumbersNumbers

What is the RequirementsWhat is the Requirements

Page 4: Interview Question- Fibonacci Series

Start with 0 and 1 (by definition)Start with 0 and 1 (by definition) Start of sequence is: Start of sequence is: 0,10,1 Add these two together: 1Add these two together: 1 Expanded sequence is:Expanded sequence is: 0,1,10,1,1 Add last two numbers togetherAdd last two numbers together

1+1 = 21+1 = 2 Expanded sequence is: Expanded sequence is:

0,1,1,20,1,1,2 Add last two numbers togetherAdd last two numbers together

1+2 = 31+2 = 3 Expanded sequence is:Expanded sequence is: 0,1,1,2,30,1,1,2,3

Really Understand the Really Understand the ProblemProblem

Page 5: Interview Question- Fibonacci Series

0 0 add the first number add the first number1 1 to the second number to the second number1 1 to get the next number to get the next number

Now What?Now What?

Do it by Hand!Do it by Hand!

Page 6: Interview Question- Fibonacci Series

001 1 now add this number now add this number1 1 to this number to this number2 2 to get the next number to get the next number

Now What?Now What?

Do it by Hand!Do it by Hand!

Page 7: Interview Question- Fibonacci Series

00111 1 now add this number now add this number2 2 to this number to this number3 3 to get the next number to get the next number

Now What?Now What?

Do it by Hand!Do it by Hand!

Page 8: Interview Question- Fibonacci Series

0011112 2 now add this number now add this number3 3 to this number to this number5 5 to get the next number to get the next number

Now What?Now What?

Do it by Hand!Do it by Hand!

Page 9: Interview Question- Fibonacci Series

001111223 3 now add this number now add this number5 5 to this number to this number8 8 to get the next number to get the next number

Now What?Now What?

Do it by Hand!Do it by Hand!

Page 10: Interview Question- Fibonacci Series

001111223 3 now add this number now add this number5 5 to this number to this number8 8 to get the next number to get the next number

1.1. the previous numberthe previous number2.2. the number before thatthe number before that3.3. the current numberthe current number

What does the ProgramWhat does the Programneed to know at Each step?need to know at Each step?

Page 11: Interview Question- Fibonacci Series

Pseudocode Version 2:Pseudocode Version 2:1.1. set the first number to 0set the first number to 02.2. set the second number to 1set the second number to 13.3. Add previous two numbers together to Add previous two numbers together to

get current numberget current number4.4. repeat step 3 until donerepeat step 3 until done

Question: Question:

Are the Are the ““last two numberslast two numbers”” always the always the same?same?

What happens at each What happens at each step?step?

Page 12: Interview Question- Fibonacci Series

1.1. Add previous two numbers together to get Add previous two numbers together to get current numbercurrent number

2.2. repeat step 1 until donerepeat step 1 until done

1.1. while not donewhile not done– Add previous two numbers together to get Add previous two numbers together to get

current numbercurrent number

endend

Transform Repeat to Transform Repeat to WhileWhile

Page 13: Interview Question- Fibonacci Series

22ndnd Previous Number Previous Number Previous NumberPrevious Number Current NumberCurrent Number

We need We need VARIABLESVARIABLES to store each of these to store each of these

What informatino do we need to What informatino do we need to ““knowknow”” or or ““computecompute”” at Each at Each Step?Step?

Page 14: Interview Question- Fibonacci Series

second_previous = 0;second_previous = 0; previous = 1;previous = 1; current_number = ????current_number = ???? current_number = second_previous + … current_number = second_previous + …

previous;previous;

Create Variables for our Create Variables for our ProgramProgram

Page 15: Interview Question- Fibonacci Series

1.1. Add previous and 2Add previous and 2ndnd previous numbers to previous numbers to get the current Fibonacci numberget the current Fibonacci number

2.2. Then update our Then update our ““previousprevious”” variables to variables to contain the contain the ““newnew”” previous numbers previous numbers

Question: Is the ordering of these two steps Question: Is the ordering of these two steps important?important?

Is the ordering of the two operations in step 2 Is the ordering of the two operations in step 2 important?important?

What happens at each What happens at each step?step?

Page 16: Interview Question- Fibonacci Series

Now is it:Now is it: current = second_previous + previous;current = second_previous + previous; previous = current;previous = current; second_previous = previous;second_previous = previous;

Or is it:Or is it: current = second_previous + previous;current = second_previous + previous; second_previous = previous;second_previous = previous; previous = current;previous = current;

Which of these produces the correct Which of these produces the correct values in our variables?values in our variables?

Page 17: Interview Question- Fibonacci Series

Case 1:Case 1: current = second_previous + previous;current = second_previous + previous; % current is assigned the value 2% current is assigned the value 2 previous = current;previous = current; % previous is assigned the value 2% previous is assigned the value 2 second_previous = previous;second_previous = previous; % second_previous is assigned the value 2% second_previous is assigned the value 2

Lets Confirm our Understanding:Lets Confirm our Understanding:previous = 1, second_previous=1;previous = 1, second_previous=1;

Page 18: Interview Question- Fibonacci Series

Case 2:Case 2: current = second_previous + previous;current = second_previous + previous; % current is assigned the value 2% current is assigned the value 2 second_previous = previous;second_previous = previous; % 2% 2ndnd previous is assigned the value 1 previous is assigned the value 1 previous = current;previous = current; % previuos is assigned the value 2% previuos is assigned the value 2

Lets Confirm our Understanding:Lets Confirm our Understanding:previous = 1, second_previous=1;previous = 1, second_previous=1;

Page 19: Interview Question- Fibonacci Series

1.1. print print ““0,10,1””::2.2. set the first two values to 0 and 1set the first two values to 0 and 13.3. While we havenWhile we haven’’t reached our goalt reached our goal

1.1. add these values to get the next (or current) add these values to get the next (or current) valuevalue

2.2. print the current value:print the current value:3.3. update the previous two valuesupdate the previous two values

Pseudocode ( 3rd Version)Pseudocode ( 3rd Version)

Page 20: Interview Question- Fibonacci Series

fprintf(fprintf(““0, 1, 0, 1, ““);); second_previous = 0second_previous = 0 previous = 1previous = 1 current = previous + second_previous;current = previous + second_previous; fprintf(fprintf(““%d, %d, ””, current);, current);

Onward to CodeOnward to Code

Page 21: Interview Question- Fibonacci Series

second_previous = 0second_previous = 0 previous = 1previous = 1 current = previous + second_previous;current = previous + second_previous; fprintf(fprintf(““%d, %d, ””, current);, current); second_previous = previous; second_previous = previous; previous = current; previous = current; current = previous + second_previous;current = previous + second_previous; fprintf(fprintf(““%d, %d, ””, current);, current);

Sample CodeSample Code

Page 22: Interview Question- Fibonacci Series

second_previous = 0second_previous = 0 previous = 1previous = 1 current = previous + second_previous;current = previous + second_previous; fprintf(fprintf(““%d, %d, ””, current);, current); second_previous = previous; second_previous = previous; previous = current; previous = current; current = previous + second_previous;current = previous + second_previous; fprintf(fprintf(““%d, %d, ””, current);, current); second_previous = previous; second_previous = previous; previous = current; previous = current; current = previous + second_previous;current = previous + second_previous; fprintf(fprintf(““%d, %d, ””, current);, current);

Sample CodeSample Code

Page 23: Interview Question- Fibonacci Series

second_previous = 0second_previous = 0 previous = 1previous = 1 current = previous + second_previous;current = previous + second_previous; fprintf(fprintf(““%d, %d, ””, current);, current); second_previous = previous; second_previous = previous; previous = current; previous = current; current = previous + second_previous;current = previous + second_previous; fprintf(fprintf(““%d, %d, ””, current);, current); second_previous = previous; second_previous = previous; previous = current; previous = current; current = previous + second_previous;current = previous + second_previous; fprintf(fprintf(““%d, %d, ””, current);, current); second_previous = previous; second_previous = previous; previous = current; previous = current; current = previous + second_previous;current = previous + second_previous; fprintf(fprintf(““%d, %d, ””, current);, current); second_previous = previous; second_previous = previous; previous = current; previous = current; current = previous + second_previous;current = previous + second_previous; fprintf(fprintf(““%d, %d, ””, current);, current); second_previous = previous; second_previous = previous; previous = current; previous = current; current = previous + second_previous;current = previous + second_previous; fprintf(fprintf(““%d, %d, ””, current);, current); second_previous = previous; second_previous = previous; previous = current; previous = current; current = previous + second_previous;current = previous + second_previous; fprintf(fprintf(““%d, %d, ””, current);, current);

Sample CodeSample Code

Page 24: Interview Question- Fibonacci Series

This implies that we want a loop!This implies that we want a loop!

Remember: A Loop lets the computer do things Remember: A Loop lets the computer do things over and over again so we donover and over again so we don’’t have to!t have to!

What loop to use?What loop to use? For loop or While loop?For loop or While loop? Give a valid reason to use either!Give a valid reason to use either!

Seems like the same old same Seems like the same old same old, over and over and overold, over and over and over

Page 25: Interview Question- Fibonacci Series

while ( current < some large number)while ( current < some large number)

Use a while loop because we want all Fibonacci Use a while loop because we want all Fibonacci numbers less than some numbernumbers less than some number

While LoopWhile Loop

Page 26: Interview Question- Fibonacci Series

for ith_fib_number = 3:1000for ith_fib_number = 3:1000

Use a for loop because we want the first 1000 Use a for loop because we want the first 1000 Fibonacci numbersFibonacci numbers

FOR loopFOR loop

Page 27: Interview Question- Fibonacci Series

Set second_previous to 0Set second_previous to 0 Set previous to 1Set previous to 1 Starting with 3, go until Starting with 3, go until ‘‘XX’’ (by ones) (by ones)

Current value is set to second_previous + Current value is set to second_previous + previousprevious

Print current valuePrint current value Set second_previous to previousSet second_previous to previous Set previous to currentSet previous to current

Pseudocode (4Pseudocode (4thth version) version)Very Close to CodeVery Close to Code

Page 28: Interview Question- Fibonacci Series

second_previous = 0;second_previous = 0;previous = 1;previous = 1;fprintf(fprintf(‘‘%d %d %d %d ‘‘, second_previous, , second_previous,

previous);previous);for I = 3:total_fib_numbersfor I = 3:total_fib_numbers

current = second_previous + previous;current = second_previous + previous;fprintf(fprintf(‘‘%d %d ‘‘, current);, current);second_previous = previous;second_previous = previous;previous = current;previous = current;

end end % the for loop% the for loop

CodeCode

Page 29: Interview Question- Fibonacci Series

Is the variable I Is the variable I usedused in the loop? in the loop? Nope! Its just a place holder.Nope! Its just a place holder.

for I = 3:total_fib_numbersfor I = 3:total_fib_numberscurrent = second_previous + previous;current = second_previous + previous;fprintf(fprintf(‘‘%d %d ‘‘, current);, current);second_previous = previous;second_previous = previous;previous = current;previous = current;

end end % the for loop% the for loop

ThoughtsThoughts

Page 30: Interview Question- Fibonacci Series

Are we calculating anything?Are we calculating anything? Sort of, but when the program is over, does the Sort of, but when the program is over, does the

computer have anything it can use?computer have anything it can use? NopeNope

How would we write code to save these How would we write code to save these values?values? What data type?What data type?

ThoughtsThoughts

Page 31: Interview Question- Fibonacci Series

What would we do if we needed to save the What would we do if we needed to save the values instead of simply printing them to the values instead of simply printing them to the screen?screen?

Answer:Answer: Use an ArrayUse an Array Note: now the variable I is importantNote: now the variable I is important

Saving the valuesSaving the values

Page 32: Interview Question- Fibonacci Series

New Code with ArrayNew Code with Array

% Pre-allocate (save buckets for)% enough space for all the numbersfib_numbers = zeros(1,total_fib_numbers);

% Set up the first two fib numbers from memory% (your memory)fib_numbers(1) = 0;

fib_numbers(2) = 1;

Page 33: Interview Question- Fibonacci Series

New Code with ArrayNew Code with Array

for i = 3:total_fib_numbers

fib_numbers(i) = fib_numbers(i-1) + …fib_numbers(i-2);

end % for

% where did the previous and % 2nd previous variables go?

Page 34: Interview Question- Fibonacci Series

What is wrong with this code?What is wrong with this code?

fib_numbers = fib_numbers(i-1) + …fib_numbers(i-2);

Corrected:

fib_numbers(i) = fib_numbers(i-1) + …fib_numbers(i-2);

Notice the Update of the Array uses the “(i)” next to the array variable

Page 35: Interview Question- Fibonacci Series

NEVER use:NEVER use:array = 5 + 6;array = 5 + 6;

ALWAYS use:ALWAYS use:array( position ) = 5 + 6;array( position ) = 5 + 6;

You must always You must always ““indexindex”” intointo an array! an array!

Let me Repeat!Let me Repeat!

Page 36: Interview Question- Fibonacci Series

How would we turn this code into a function?How would we turn this code into a function? What are the inputs?What are the inputs? What are the outputs?What are the outputs?

FunctionFunction

Page 37: Interview Question- Fibonacci Series

You have 1 minutes to draw a black box for You have 1 minutes to draw a black box for this functionthis function

Draw a Black BoxDraw a Black Box

Page 38: Interview Question- Fibonacci Series

Function as Black BoxFunction as Black Box

FunctionInput Output

Page 39: Interview Question- Fibonacci Series

Fibonacci as Black BoxFibonacci as Black Box

FibonacciCount

Fibonacci Numbers

Compute the first “count” fibonacci numbers

Array of Numbers

Integer

Page 40: Interview Question- Fibonacci Series

You have 1 minute to write a brief comment You have 1 minute to write a brief comment that would go at the top of your .m file for the that would go at the top of your .m file for the Fibonacci functionFibonacci function

Comment Your FunctionComment Your Function

Page 41: Interview Question- Fibonacci Series

% array_of_fib_numbers = compute_fib(…% array_of_fib_numbers = compute_fib(…% how_many)% how_many)%%% Author: H. James de St. Germain% Author: H. James de St. Germain% Date: Fall 2007% Date: Fall 2007%%% This function produces an array of the % This function produces an array of the % first % first ““how manyhow many”” Fibonacci Numbers Fibonacci Numbers

Function CommentFunction Comment

Page 42: Interview Question- Fibonacci Series

You have one minute to write the function You have one minute to write the function design pattern for this functiondesign pattern for this function

Function Design PatternFunction Design Pattern

Page 43: Interview Question- Fibonacci Series

function result_array = function result_array = compute_fib( how_many )compute_fib( how_many )

result_array(1) = 0;result_array(1) = 0;

end end %function%function

Function Design PatternFunction Design Pattern

Page 44: Interview Question- Fibonacci Series

From your memory and your notes write out From your memory and your notes write out the code for this function.the code for this function.

… … you have 1 minute….you have 1 minute…. Pseudocode:Pseudocode:

set up first two values in arrayset up first two values in array loop updating the loop updating the ““currentcurrent”” value based on the value based on the

previous two valuesprevious two values

Function CodeFunction Code

Page 45: Interview Question- Fibonacci Series

function result_array = function result_array = compute_fib( count )compute_fib( count )

result_array(1) = 0;result_array(1) = 0; result_array(2) = 1;result_array(2) = 1; for counter = 3 : countfor counter = 3 : count result_array(counter) = …result_array(counter) = …

result_array(counter-1) + result_array(counter-1) + ……

result_array(counter-2);result_array(counter-2); end end %for loop%for loopend end % function% function

Function CodeFunction Code

Page 46: Interview Question- Fibonacci Series

1.1. semicolons (;s) in the function? Where?semicolons (;s) in the function? Where?

function result_array = compute_fib( count )function result_array = compute_fib( count ) result_array(1) = 0;result_array(1) = 0; result_array(2) = 1;result_array(2) = 1; for counter = 3 : countfor counter = 3 : count result_array(counter) = …result_array(counter) = …

result_array(counter-1) + …result_array(counter-1) + …result_array(counter-2);result_array(counter-2);

end end %for loop%for loopend end % function% function

How many…How many…

Page 47: Interview Question- Fibonacci Series

1.1. fprintfs and input statements?fprintfs and input statements?

function result_array = compute_fib( count )function result_array = compute_fib( count ) result_array(1) = 0;result_array(1) = 0; result_array(2) = 1;result_array(2) = 1; for counter = 3 : countfor counter = 3 : count result_array(counter) = …result_array(counter) = …

result_array(counter-1) + …result_array(counter-1) + …result_array(counter-2);result_array(counter-2);

end end % for loop% for loopend end % function% function

How many…How many…

Page 48: Interview Question- Fibonacci Series

use fprintf in a functionuse fprintf in a function unless told that the function unless told that the function ““communicatescommunicates””

with the user of the programwith the user of the program use input in a functionuse input in a function

unless told that the function unless told that the function ““recievesrecieves”” input input from the user of the programfrom the user of the program

NEVER…NEVER…

Page 49: Interview Question- Fibonacci Series

1.1. variables? (parameters, return variables, variables? (parameters, return variables, local variables)local variables)

function result_array = compute_fib( count )function result_array = compute_fib( count ) result_array(1) = 0;result_array(1) = 0; result_array(2) = 1;result_array(2) = 1; for counter = 3 : countfor counter = 3 : count result_array(counter) = …result_array(counter) = …

result_array(counter-1) + …result_array(counter-1) + …result_array(counter-2);result_array(counter-2);

end end % for loop% for loopend end % function% function

How many…How many…

Page 50: Interview Question- Fibonacci Series

Questions?Questions?

End FibonacciEnd Fibonacci