Doing coding challenges is an excellent way to improve your Ruby & problem-solving skills. And to prepare for coding interviews!
Why?
Because during a challenge you put all your focus on solving 1 specific problem.
You don’t have to worry about anything else.
This kind of practice will expand your thinking skills, allow you to explore interesting language features & become a better Ruby developer.
Some of these challenges require specialized knowledge about a computer algorithm, data structure or concept, like some math trick.
It’s ok if you can’t solve most of these.
Don’t worry!
Now:
I think it can be helpful to read a few solutions to get a feeling for the process of solving these challenges.
Starting with…
Challenge 1: Finding Duplicates
This first challenge is that given an array with Integer
values you need to find all the duplicated numbers.
Here’s an example:
numbers = [1,2,2,3,4,5] find_duplicates(numbers) # [2]
Let’s start with a question…
“How can I know if a particular number is duplicated?”
I’m not trying to get the solution in one step.
At this stage, I just want to ask & answer a question that will get me closer to the solution.
Write down a few ideas:
- I can count how many of each number we have in the array, then count = 2 means duplicate.
- I can go over every element & keep a list of “seen” elements, if I see an element twice then we found a duplicate.
- I can join the numbers into a string & try to match the duplicated numbers with a regular expression.
It doesn’t matter “which is best” at this point.
Don’t worry about that!
Right now, what’s important is to get a working solution. Pick one that sounds good to you & write the code for it.
Example:
seen = [] numbers.each_with_object([]) do |n, dups| dups << n if seen.include?(n) seen << n end # [2]
This seems like the correct solution.
Now:
You want to try other inputs (different arrays) to make sure this really works, writing unit tests is great for this.
If the solution is 100% working then you can try the other solutions & use the one that’s easier to understand.
Don’t look for perfection.
Look for learning, understanding & making progress every day!
Challenge 2: Valid Words
Given an array of characters & one word, find out if the word can be made from these characters.
Example:
word = "orange" characters = %w(e n g a r o) valid_word?(characters, word) # true
Again we start with a question to direct our thinking process.
“How can we make sure that the word can be made with these characters?”
Come up with ideas.
Put away the distractions & think about it.
It may help to do this on paper.
My ideas:
- For every character in
word
, remove that character from thecharacters
array, if you can’t remove all the characters then returnfalse
- Count all the characters in
word
&characters
, then subtract the counts
Here’s a possible solution:
word .each_char .all? { |ch| characters.delete_at(characters.index(ch)) rescue nil }
What’s your solution?
Share it in the comments section.
Summary
You have learned about coding challenges & how to use them to improve your Ruby skills!
Remember:
It’s perfectly ok to not be able to solve a specific challenge. Think of it like a video game boss, if the boss is level 100 & you’re level 70 it’s going to be really hard to beat this boss.
What do you do in that case?
You go level up & get experience with easier challenges (lower level bosses), and you get better gear (learning more about Ruby & programming).
Good luck & have fun! 🙂