LetMyPeopleCode.com

A blog about software, schmaltz, and monkey-patches for the soul

Menu
  • About Greg Bulmash
  • Greg Talks Tech
  • Privacy Policy
Menu
"The Coding Interview"

Interview Questions: Even Fibonacci Numbers

Posted on February 3, 2022 by Greg Bulmash

This series answers coding interview questions from the Coding Interview Prep collection at freeCodeCamp. You can find more with the interview questions tag.

This is an algorithmic problem from the Project Euler collection.

Create a set of fibonacci numbers that are less than or equal to n and return the sum of only the even numbers. Fibonacci adds the current number to the prior number in the sequence. So a sequence would be 1, 1, 2, 3, 5, 8, 13.... In that sequence, only 2 and 8 are even, so the answer would be 10.

Solution Explained

To derive the fibonacci numbers below n requires 4 variables, 3 that need to be function-scoped (existing outside the loop iteration scope), one that can be loop-iteration-scoped.

First declare 3 variables, one to hold the result, one equal to one, one equal to 0.

Then start a while loop that will keep running until the next fibonacci number will be greater than n.

At the end of the iteration, last must equal the starting value (fib) that started the iteration, so the starting value is assigned to hold. Add fib and last to get the next fibonacci value, then assign the initial value to last.

Finally, use fib mod 2 === 0 to test for it being an even number, and if so, add it to the result.

When done, return the result.

Notes

The initial thought hurdle I had to overcome was where to make last directly equal fib. Two iterations of the code suffered from that logic error and failed the tests.

I realized there was nowhere in this that wouldn’t cause a binary progression (1, 2, 4, 8, 16…) instead of a fibonacci one (1, 1, 2, 3, 5, 8…). Simply put, last had to equal fib, but only the value of fib before last had been added to it. Adding hold solved that.

iteration 1: 
  start: hold = 1, fib = 1, last = 0 
  end: fib = 1, last = 1 

iteration 2: 
  start: hold = 1, fib = 1, last = 1 
  end: fib = 2, last = 1 

iteration 3: 
  start: hold = 2, fib = 2, last = 1 
  end: fib = 3, last = 2 

iteration 4: 
  start: hold = 3, fib = 3, last = 2 
  end: fib = 5, last = 3 
...

Solution

function fiboEvenSum(n) {
  let result = 0, fib = 1, last = 0;
  while (fib + last <= n) {
    let hold = fib;
    fib = fib + last;
    last = hold;
    if(fib % 2 === 0) result += fib;
  }
    return result;
}

Related

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Recent Posts

  • Job Hunt Diary 2023: Verizon Is Passive-Aggressive?
  • Job Hunt Diary 2023: Eliciting Responses
  • Job Hunt Diary 2023: And so on, and so on…
  • Job Hunt Diary 2023: Freelancing and warning words
  • Job Hunt Diary 2023: Not all job fairs are created equal

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • September 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • November 2021
  • May 2021
  • April 2020
  • March 2020

Categories

  • Apps
  • Computing
  • Frameworks and Libraries
  • Games
  • Hacks
  • Hardware
  • JavaScript
  • Mac
  • Mobile
  • Other Art Projects
  • Productivity
  • Programming
  • Projects
  • Python
  • Society & Culture
  • Teaching Code
  • Tech Career
  • Uncategorized
  • WebTech
  • Writing

Tag Cloud

Adobe advertising bad ads browser hunt clown car code sample comcast commute copy writing CX design Developer Relations dev rel evernote game demo google drive hardware review health home office home studio internet interview questions j job hunt job hunt diary Mac magento meta-coding OneNote PEO productivity review speedtest t-mobile Windows xfinity

©2023 LetMyPeopleCode.com | Theme by SuperbThemes