Now we have a lot of knowledge to put into practice. Even in the last blog post we talked about how to solve coding challenges. That post followed by the large amount of previous ones, you have the basis to solve current and next coding challenges. If you don’t know what’s our past blog post here you can see a list in order of each one of them
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 follow to have all of these concepts
Challenge #1
Write a function that takes in a non-empty array of integers that are sorted in ascending order and returns a new array of the same length with the squares of the original integers also sorted in ascending order.
Pay attention to this problem, because it might seem very simple, however you have to take into account that while integers in the input array are sorted in increasing order, their squares won’t necessarily be as well, because of the possible presence of negative numbers.
Expected Results
As you will see the expected results talk by themselves and can explain the expected behavior of your algorithm. When we see different inputs you have to return the output accordingly.
Sample Input
Array = [1, 2, 3, 5, 6, 8, 9]
Sample Output
[1, 4, 9, 25, 36, 64, 81]
----------------------------------
Sample Input
Array = [1]
Sample Output
[1]
----------------------------------
Sample Input
Array = [-2, -1]
Sample Output
[4, 1] ⇒ Initial result, you have to sort it
[1, 4]
----------------------------------
Sample Input
Array = [-50, -13, -2, -1, 0, 0, 1, 1, 2, 3, 19, 20]
Sample Output
[2500, 169, 4, 1, 0, 0, 1, 1, 4, 3, 361, 400] ⇒ Initial result, you have to sort it
[0, 0, 1, 1, 1, 3, 4, 4, 169, 361, 400, 2500]
Following the Framework to Solve Coding Challenges
- - Use an online stopwatch
- - Read carefully the problem
- - Never try to see the result until you try it by yourself.
- - What's the expected output from the challenge?
- - Did I solve a similar problem before? How?
- - Start your own documentation or cheatsheet
- - Solve problem first in a paper or a whiteboard
- - Start using the Ruby Interactive Console (IRB) or the text editor
- - Solve it first via "brute force"
- - Review the time & space complexity
Brute force solution
Remember that there are always different ways to solve the same problem. So it is probably that you solved it with a different approach and that's ok, even that what matters: “you solve it”. Now let’s check this solution
def sorted_squared_array(array)
## O(n) time | O(n) space
length = array.length - 1
current_position = 0
new_arr = []
if array.length == 1
p new_arr << array[current_position] ** 2
else
(1..length).each do |i|
if array[current_position] <= array[i]
p new_arr << array[current_position] ** 2
current_position += 1
if length == current_position
p new_arr << array[current_position] ** 2
end
else
return false
end
end
end
return new_arr.sort
end
p sorted_squared_array([1, 2, 3, 5, 6, 8, 9])
# p sorted_squared_array([1])
# p sorted_squared_array([1, 2])
# p sorted_squared_array([1, 2, 3, 4, 5])
# p sorted_squared_array([-2, -1])
# p sorted_squared_array([-5, -4, -3, -2, -1])
# p sorted_squared_array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
# p sorted_squared_array([-1, -1, 2, 3, 3, 3, 4])
Is a good moment to congratulate yourself, you’ve solved your first coding challenge!
You can start figuring out other kinds of solutions and that’s ok.
I hope you learned a lot with this excescrise and let’s keep our progress
Thanks for reading
DanielM