Back-end Engineering Articles

I write and talk about backend stuff like Ruby, Ruby On Rails, Databases, Testing, Architecture / Infrastructure / System Design, Cloud, DevOps, Backgroud Jobs, some JS stuff and more...

Github:
/danielmoralesp
Twitter:
@danielmpbp

2024-12-10

Framework to Solve Coding Challenges with Ruby

Now we have a lot of knowledge to put into practice, so far we’ve seen these articles:about Ruby and Data Structures. 

Articles about Ruby

Articles about Data Structures

It’s important to have the basics of Ruby and Data Structures before doing coding challenges, because it can be hard if you don’t do that. All of the articles listed above are in order, which is important if you don’t know where to start or what’s the steps to learn all of these concepts

How to solve coding challenges
One of the main obstacles when you’re trying to solve coding challenges is to go directly to the code. That strategy can be hard, because if you don’t have the problem clear in your mind you’ll be trying to solve it blindly and it will become tough to solve the given problem. 

So I have a list of steps to solve coding challenges you should follow if you want to ace the coding challenges.

Step #1- Use an online stopwatch
Use an online stopwatch to measure the time it takes you to solve the problem. This could be the best measure of your enhancement over the weeks, because you’ll see that you can solve problems in less time. Or suddenly you can solve more complex problems in the same amount of time. 

You just need to type “stopwatch” in Google to see it. When should you start the stopwatch?. Once you start reading the problem. You should measure your entire process of solving coding challenges: from reading to solving the problem. 

Step #2- Read carefully the problem
Reading comprehension is a good skill to have here. However, you also need to know about the basics of Ruby and the data structures, because once you start reading you can start thinking about the different ways to solve the challenge. Try to find hidden keywords in the problem, because right there could be the solution

Step #3- Never try to see the result until you try it by yourself. 
This is one the most important steps. You have to keep yourself away from the solution. You should not cheat here. It's for your own growth as a developer. I know, the temptation is high, but actually it is more fun and addictive if you try to solve it by yourself, and then measure and compare your results with the official solution. Just believe me!

You’ll learn the double if you try to do it by yourself and then study the official solution. ACtually you’ll have at least 2 different points of views about the coding challenge and you’ll be better prepared to solve almost any problem. 

Step #4- What's expected output from the challenge? 
When you’re reading the problem, generally speaking, at the end of it you should find examples of the expected output. 

This output is critical, because you could find different gotchas in the output. For instance, a problem expects to return an array of characters between A to Z, but it can also expect to respond just in lowercase, so if you also allow the return of uppercase letters you could fail the challenge. 

So, pay careful attention to this examples

Step 5- Did I solve this same problem before? How?
If you start solving challenges, sure enough in some point (after 5-10 challenges) you’ll start collecting a lot of different ways to solve coding challenges and you could end up solving quite similar problems later. So it’s a good idea to start having your own cheat sheet or documentation about the problem you’ve solved in the past

Also is very important to remember how you solve it, because with that same logic you can apply that knowledge to the current challenge

Step #6- Start your own documentation or cheatsheet
As we mentioned in previous steps you should start your own documentation or cheatsheet from the beginning. Don’t trust your mind. It can be forgotten. 

You can start your documentation with the basics of the data structures and then start writing down the different algorithms you use to solve the problems. You don’t need to share that with anybody, you can keep it just for yourself. 

Step #7- Solve problem first in a paper or a whiteboard
Lot of people tend to avoid the usage of sheets of paper or whiteboards. Probably is far from you, or you just want to start writing code. Take your time. You're not in competition where you have to solve the challenge faster than others. You need your own time to solve the challenge, it doesn't matter your level. 

Once you have the paper or whiteboard subdivide it as follows:
  1. - Make 15% of the total space to write what’s the expedited output of the code. We saw this in step #4
  2. - Make another 15% of the total space to write the steps to follow. This is very important to have your mind organized until you start actually coding the solution. 
  3. - Use the other 70% of the space to actually start writing the solution. Try to complete the whole solution here, before starting coding the given solution. 

When you start dealing with coding challenges that space can be enough. But later you’ll see yourself doing next things
  • - You don’t have enough space to write the whole solution. Just take another sheet of paper and continue there
  • - Sometimes you don’t have a clear code output in your mind. So here it is allowed that you start using a tool like - IRB to test the output rapidly or even use the text editor if the solution is really complicated. But at least you have an idea about where to start now. 

Look at my whiteboard. It’s not a real coding challenge, but it is more or less that you have to divide the whiteboard and have the right amount of space to draw your thoughts. 


Step #8 - Start using the Ruby Interactive Console (IRB) or the text editor
This is the moment to start using the IRB or the text editor to write down your solution. Now you have a really good idea about the problem. If not, at least you have been studying the description more in detail and trying to figure out a possible solution. That’s something. Once you feel ready to start testing your assumption about the possible answer, is time to open the console or the text editor

Remember that the console helps you to test any problem rapidly, but if you think the problem will take a couple of lines of code, it is probably better to create the entire solution in the text editor and then execute it in the console. 

One of the things you should start doing is printing statements with command”puts” to see what you're returning on each step of the solution and try to catch any errors or insights about your solution and about the problem itself.

Step #9 - Solve it first via "brute force" 
At the beginning your solution could not seem pretty professional or advanced. You should not worry about it at this point. What you need now is to solve it and achieve the expected results. In step numero fourth we’ve been talking about the results expected by the solution, so once you have the code ready or at least you wanna to start printing things it is better to start using brute force in the solution. Later when we see the first examples we’ll be seeing what brute force in detail. 

Once you have the solution, and you understand the problem might be a good time to refactor and do a better solution with less code or other better performance algorithm.

Test all types of situations once you think you've solved the problem. This tactic can open your mind to other possible solutions. 

Step #10- Review the time & space complexity 
You have the solution ready, that's awesome! Now it's time to Review the time abd space complexity and put in comments what I think it is. For instance, if you think the time is O(n) and space is O(log(n)) just put that in comments inside your solution and try to explain for yourself why that is. It’s an amazon exercise to start testing your knowledge about time and space complexity

If you don’t know what time or space is for your answer, please refer to re-study the subject a little bit, or even go to the internet and try to figure out what's the right notation for your case. 

I’m pretty sure that with these 10 steps you can start solving coding challenges in a more structured and organized way. This is really good for you, because you don’t have to struggle as I was trying to figure out the best strategy to solve coding challenges. Obviously here we’re not talking about how to solve the algorithms itself, but the framework to ace and approach the solutions. It doesn't matter the complexity. 

Later we’ll have coding challenges to solve, so we can practice this framework. 

I hope you learned a lot

Thanks for reading
DanielM