Interview 02

Generate the nth Fibonacci number, 2 different ways.

Specifications

Feature Tasks

Structure

Familiarize yourself with the grading rubric, so you know how to score the interview.

Look for effective problem solving, efficient use of time, and effective communication with the whiteboard space available.

Every solution might look a little different, but the candidate should be able to at least convince you that their code works to solve the problem.

Assign points for each item on the Rubric, according to how well the candidate executed on that skill.

Add up all the points at the end, and record the total at the bottom of the page.

Example

Input Output
0 0
1 1
2 1
3 2
4 3
5 5
6 8
7 13
8 21
... ...
14 377
... ...
45 1134903170
... ...

Documentation

Record detailed notes on the rubric, to share with the candidate when the interview is complete.

Solution

Algorithm The Fibonacci sequence is a sequence of numbers in which each number is the sum of the 2 numbers that precede it. The most straight forward solution starts at the beginning of the sequence and use iteration to calculate the preceding values until you reach the n(th) fibonacci value. Starting with 1 and 2 respectively, add those together to come up the with next value, and using loop along with a structure to track all values leading to the n(th) value. Another approach uses recursion to perform something similar, but using the callstack to count up to a given fibonacci value. Starting with the value n, we add n - 1, and n - 2, onto the callstack. For each number we pass into our recursive function, we potentially add 2 more function calls onto the stack, and only when the value of n is less than 2, do we return n and add it to all the function calls waiting to return a value. Using this approach we should add 1 together n2 times, giving us our fibonacci value at a given value of n.
PseudoCode
algorithm NTH_FIBONACCI_ITERATIVE:
  declare number N <- input
  if N is less than 2:
    return N
  declare array SUMS <- []
  append 0 to SUMS
  append 1 to SUMS
  for every number between 2 and N:
    declare i <- current number
    append SUMS[i - 1] + SUMS[i - 2] to SUMS array
  return number at SUMS position N
algorithm NTH_FIBONACCI_RECURSIVE:
  declare number N <- input
  if N is less than 2:
    return N
  return NTH_FIBONACCI_RECURSIVE(N - 1) + NTH_FIBONACCI_RECURSIVE(N - 2)
Big O The iterative solution will complete with 0(n) space and time complexity. Since we iterate between 2 and N times, our time complexity grows with the size of N. The same goes for space, since we use an array to store every value of the fibonacci sequence until we calculate the Nth value. The recursive solution is more elegant but it will take 0(2n) complexity for both space and time. Unless our value is less than 2, we call our recursive function 2 more times leaving us with exponential algorithmic complexity. This also applies to space complexity since we add function calls to our callstack taking up an exponential amount of space compared to our input size.