DEV Community

Cover image for Interview Programming Challenge
Raymundo Alva
Raymundo Alva

Posted on

Interview Programming Challenge

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.

def auto\_correct(str)  
  arr = str.split(' ')  
  nu\_arr = \[\]  
end
Enter fullscreen mode Exit fullscreen mode

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.

def auto\_correct(str)  
  arr = str.split(' ')  
  nu\_arr = \[\]

  arr.each\_with\_index do |word, index|  
    split\_word = word.split('')  
    nu\_word = ''  
  end  
end
Enter fullscreen mode Exit fullscreen mode

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.

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
Enter fullscreen mode Exit fullscreen mode

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

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!")
Enter fullscreen mode Exit fullscreen mode

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! 😎

Top comments (1)

Collapse
 
kalkwst profile image
Kostas Kalafatis

Hey there! I just read your post and I really liked it. However, I noticed that your code snippets seem a bit off. I thought I'd share a guide that might help you format them properly. Let me know if you have any questions or need further assistance. Keep up the great work!