Ruby Coding Practice
Tags: ruby, beginners, tutorial, challenge
Published: 2023-05-03 04:01:27

I always spend some part of my day practicing programming questions on CodeSignal. I make sure to never break the routine so that I can get better at solving problems. There were some easy problems at first but once in a while I stumble upon a problem that stumps me. I try and try and after a while I finally solve it. It is so satisfying and I end up feeling so good in the end. I wanted to share this coding problem because I thought it was interesting.

So like I mentioned this problem comes from CodeSignal and it goes like this.

_Given an array of integers, find the pair of adjacent elements that has the largest product and return that product._

Sounds pretty simple right? This was my process. First we start with an example array and an empty method that looks like this.
```ruby
array = \[3, 6, -2, -5, 7, 3\]

def adjacentElementsProduct(inputArray)

end
```
Since we are dealing with arrays I immediately thought about looping through the array and figure out which was the largest product that way. So I’m going to define a variable that is going to represent the array’s index and another variable that will represent the largest product. My method looks like this now.
```ruby
def adjacentElementsProduct(inputArray)  
  i = 0  
  largest\_product = 0  
end
```
So I’m going to implement a while loop. Each time we pass through every element in the array I am going to compare the largest\_product variable with the product of the current element and the next element. This will allow me to go through all of the elements and find out what the largest product is.
```ruby
def adjacentElementsProduct(inputArray)  
  i = 0  
  largest\_product = 0

  while i < inputArray.length  
    if inputArray\[i\] \* inputArray\[i + 1\] > largest\_product  
      largest\_product = inputArray\[i\] \* inputArray\[i + 1\]  
    end  
    i += 1  
  end  
  largest\_product  
end
```
This is the part where I got into my first problem. The output for my program looked like this.
```
nil can't be coerced into Integer  
main.rb on line 22:in \`\*'  
main.rb on line 22:in \`adjacentElementsProduct'  
main.rb in the pre-written template:in \`\_runqcfhs'  
main.rb in the pre-written template:in \`block in getUserOutputs'  
main.rb in the pre-written template:in \`times'  
main.rb in the pre-written template:in \`getUserOutputs'  
main.rb in the pre-written template:in \`<main>'
```
I figured out that when we loop through the array the very last element cannot be compared to another element because it doesn’t exist. I had to make sure that the code only ran if the element was not equal to nil. I added another if statement that would check for that. I also saved the product of the elements into a variable so that the program is easier to read. This is what that looks like.
```ruby
def adjacentElementsProduct(inputArray)  
  i = 0  
  largest\_product = 0

  while i < inputArray.length  
    if inputArray\[i + 1\] != nil  
      product = inputArray\[i\] \* inputArray\[i + 1\]  
      if product > largest\_product  
        largest\_product = product  
      end  
    end  
    i += 1  
  end  
  largest\_product  
end
```
With this code I finally thought I had solved the problem. When I run the test only one of them failed. I started to compare the passing tests to the failing test and realized that the only difference is that the largest product in the failing test is a negative number. Since I was initializing the largest\_product element at 0 the variable was never updated since the largest product is lower than that. I figured that I somehow had to initialize largest\_product to be the lowest number possible. After doing some research online I realized that Ruby can express the concept of infinity with the `Float::INFINITY` constant. Nothing can be lower than negative infinity so I figured this could work with any extreme examples. My final product looked like this.
```ruby
def adjacentElementsProduct(inputArray)  
  i = 0  
  largest\_product = -Float::INFINITY

  while i < inputArray.length  
    if inputArray\[i + 1\] != nil  
      product = inputArray\[i\] \* inputArray\[i + 1\]  
      if product > largest\_product  
        largest\_product = product  
      end  
    end  
    i += 1  
  end  
  largest\_product  
end
```
Maybe it was a really simple example but that was super fun! I’m sure there are some cleaner answers that make use of some built-in methods but I wanted to show my first attempt. After completing this problem and thinking about how I could make it better I thought about some methods I could use like .map or .each\_cons(). If anyone reading this would like to share their answers I would love to see them! Hope that this problem is as fun for you as it was for me. Happy coding! 😎