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…
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:
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!
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:
word
, remove that character from the characters
array, if you can’t remove all the characters then return false
word
& characters
, then subtract the countsHere’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.
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! 🙂