Exercise 1:

FizzBuzz

This is a problem used by lots of employers as a warm up exercise during an interview. Write some code in Ruby that prints numbers 1-100, BUT for multiples of three, print 'fizz', for multiples of 5 print 'buzz', and for multiples of both print 'fizzbuzz'.

The employer is testing that you know Ruby syntax.

  1. First you have to be able to print out messages in the console.
  2. You have to write a loop, or as Ruby people say, 'iterate'.
  3. You have to be able to figure out multiples using conditional statements.

Here are the tools you need to solve this one:

  1. puts will print your numbers and strings
  2. 100.times do |num|
      puts num
    end
  3. puts 'yes' if 2 > 1
    puts 'maybe' if 1 < 2
  4. Final tip: figure out the difference between 20 % 6
    and
    20 / 6
Try it yourself online!

Exercise 2:

Fibonacci Numbers

This is a problem has lots variations. The first Fibonacci numbers can start with 0 and 1 or 1 and 1. The next numver always equals the sum of the previous two. 0 1 1 2 3 5 8 etc. Can you write code that prints out the first 100 fibonacci numbers?

The employer is testing that you can effectively use variables and a loop. More than that they're looking to see that you're using a small amount of repeating code to do manual work.

  1. You have to be able to print out messages in the console.
  2. You need variables for the first two values.
  3. You have to iterate using a loop.
  4. You have to figure out how to have the last two values availble for addition.

Here are the tools you need to solve this one:

  1. puts will print your numbers and strings
  2. 100.times do |num|
      puts num
    end
  3. arr = [0,1]
    arr << 1
    puts arr[-1]
  4. Final tip: The array is a list of well, whatever you put into it. How can you use it?
Try it yourself online!

Exercise 3:

Collapse Array

Write a function that can add up the integers in an array that may contain characters and more arrays. [1,2, A'] should return 3. [3,[4,'H']] should return 7.

There's a method in ruby called flatten that you can run on an array to pull out elements from arrays in inside arrays to make one 'flat' array. Try using that method , each to iterate through, and then an if/else block to get the sum. If that's too easy, try solving this problem without .flatten (hint: google recrusion).

  1. You have to be able to define your own method.
  2. You need to save the result of .flatten in a new variable.
  3. You have to iterate using an each.
  4. You have to keep track of the total in a variable, adding to it when the if logic says so.

Here are the tools you need to solve this one:

  1. def flatten_sum(arr)
    end
    how you define a function
  2. arr.flatten returns a flattened array
  3. arr.each do |thing|
    end
    lets you loop through each thing in the array.
  4. Final tip: Remember you need to hold onto that total value.
Try it yourself online!