In this coding exercise, we walk through how to efficiently find the largest item in an array. Additionally, we examine how to implement performance tests to compare two different implementation options.
Summary
Implement a method that finds the largest integer in an array without using Ruby’s built-in
max
method. Additionally, ensure that the algorithm can work efficiently on large sets of data.
Exercise File
Exercise Description
Given the following array:
[1, 6, 3, 10, 5, 3]
Write a program that returns the largest integer in the array.
Example Output
10
Real World Usage
Finding the largest element in a collection is used in many algorithms. Additionally, you’ll discover that there are multiple solutions to this exercise, however, not all of the solutions are efficient. Working through this exercise should help you analyze the performance of each implementation option. And performance analysis is important in day to day development.
Solution
Can be found on the solutions branch on github.
This was a really great lesson! I learned a lot, and always feel that your videos provide useable skills and knowledge.
Thanks Zac, I’m glad that you found it helpful!
Thanks for these lessons.
But you say new_max becomes 5?
How can it? 1 is not greater than 5 is it? Oops!
I’m sorry. Not awake yet. Just looking at it didn’t make sense so I went and coded it for myself. I put a ‘p new_max’ in the loop. I’ve been using VSCode for all my Ruby and Python these days. So for my run of code note on the command line I did this bellow…
if __FILE__ == $PROGRAM_NAME
system ‘rspec 24.rb’
end
A better way with a one liner.
system ‘rspec 24.rb’ if __FILE__ == $PROGRAM_NAME
You mentioned you could improve the performance even more by caching. Could you explain how that works in this context?
I have noticed you don’t monetize crondose.com, don’t waste your traffic,
you can earn extra bucks every month with new monetization method.
This is the best adsense alternative for any type of website (they approve all websites),
for more details simply search in gooogle: murgrabia’s tools