Interview Programming Challenge
Tags: ruby, challenge, beginners
Published: 2023-05-03 04:10:47

A week ago I had my very first technical interview with a company I was very excited about. I had only participated in mock interviews before so to have a real interview coming up was both exciting and stressing. When the day came I realized that it was actually a very fun experience. I learned a lot about how the hiring process might go for other companies. Best of all, I was given a new programming challenge to have fun with! In this post I will show you what the challenge was and the solution I came up with.

The challenge was to create a function that auto corrected a string. The rules were simple.

*   Only the first letter of the first word was required to be capitalized.
*   The first letter of the second word could be capitalized or lower cased.
*   All letters after the first letter of the word had to be lower cased.

I decided to use the Ruby programming language to solve this problem. My thought process was to first split the input sentence into an array of words and create an empty array where the final product would be stored.
```ruby
def auto\_correct(str)  
  arr = str.split(' ')  
  nu\_arr = \[\]  
end
```
Ok, so now that I knew how I was going to process the original string I thought that I would need to iterate through each word. I needed a way to recognize the first word so that it was always capitalized. In order to do that I used .each\_with\_index. If the index was 0 then I can have that word’s first letter capitalized. Inside this block I also needed to split the word into individual letters and set a variable for the processed word. This is what my code looked like so far.
```ruby
def auto\_correct(str)  
  arr = str.split(' ')  
  nu\_arr = \[\]

  arr.each\_with\_index do |word, index|  
    split\_word = word.split('')  
    nu\_word = ''  
  end  
end
```
Now I need to go through every letter and figure out whether it needs to be a capital letter or lower case letter. I decided to do another .each\_with\_index block to go through every letter, change the casing, and concatenate it to the nu\_word. If the index for the word was 0 and the index for the letter was 0 then that letter had to be capitalized. Else, if the index of the letter was zero but not the first word then that letter could be left as is and then concatenated to the nu\_word. Else the letter is lower case. I wrote and if statement that would do just that. After the if statement I would shovel the nu\_word to the nu\_arr. This is what that looked like.
```ruby
def auto\_correct(str)  
  arr = str.split(' ')  
  nu\_arr = \[\]

  arr.each\_with\_index do |word, index|  
    split\_word = word.split('')  
    nu\_word = ''  
    split\_word.each\_with\_index do |letter, i|  
      if index == 0 && i == 0  
        nu\_word += letter.upcase  
      elsif i == 0  
        nu\_word += letter  
      else  
        nu\_word += letter.downcase  
      end  
    end  
    nu\_arr << nu\_word  
  end  
end
```
Great so now our nu\_arr should contain all the words processed correctly according to the rules. Now the only thing left to do is to join the words into a string and ouput the result to the console. Let’s call the method and see our result
```ruby
def auto\_correct(str)  
  arr = str.split(' ')  
  nu\_arr = \[\]

  arr.each\_with\_index do |word, index|  
    split\_word = word.split('')  
    nu\_word = ''  
    split\_word.each\_with\_index do |letter, i|  
      if index == 0 && i == 0  
        nu\_word += letter.upcase  
      elsif i == 0  
        nu\_word += letter  
      else  
        nu\_word += letter.downcase  
      end  
    end  
    nu\_arr << nu\_word  
  end  
  puts nu\_arr.join(' ')  
end

auto\_correct("HeLlO WoRLD!")
```
After we run it our code the result looks like this “Hello World!”. The code is working perfectly!

The problem was fun and it was the very first time I had interviewed so it was great experience. Maybe this wasn’t the best or most efficient answer but I was proud that I was able to solve the problem. In reality trying to solve this problem wasn’t so straightforward. I went through periods of trial and error and it took me some time to get it working. Either way I feel like the interview went well and I really enjoyed getting some interviewing experience. Let me know what your solution looks like. I would love to see what other people come up with. Thanks for reading and happy coding! 😎