Interview Programming Challenge

Ray Alva
Geek Culture
Published in
3 min readMar 1, 2021

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

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

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

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!")

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

--

--

Ray Alva
Geek Culture

Software engineer with a passion for building applications. I love nature, gaming, cooking and of course coding!