Comparing Sort Methods as Interview Practice
Last month I attended a very informational Philly.rb meetup where I learned some Ruby best practices for code. Different methods were tested using the ‘benchmark-ips’ gem to compare run times. For example:
#slower
Post.select(:id).map(&:id)#faster
Post.pluck(:id)
I wanted to try out this gem to test performance of my own code but was at the point in the Flatiron School curriculum where I was focused on Javascript and React/Redux and taking a break from Ruby. I needed to keep pushing forward to finish those projects so that I could stop paying tuition and then find a developer job.
As I’m prepping for my first developer interview, I’m cognizant that I need to study CS concepts for technical questions. Bootcamp programs are notoriously known for not focusing on algorithms (I knew this and I don’t necessarily think it’s a huge problem), so I’ve been actively solving problems on CodeWars, watching Youtube videos, and reading articles about concepts I feel I’m lacking knowledge about.
Sorting is a no-brainer whiteboard problem. For someone with my skill level, I’m expecting to encounter having to explain how to sort an array of items. This is something I haven’t had to consider since the languages I have been using have a sort method built in. Today I realized that I have no clue what type of sorting algorithm is used in the Ruby sort method. After a quick Google search, I found the answer: Quicksort.

I follow Vaidehi Joshi, a fellow Flatiron grad, on Twitter and knew that she was writing a weekly blog that covers basic CS concepts as a year-long project. I liked the way she illustrated concepts on paper and spoke in language easy to understand for beginners like me. Luckily, she had just done a 2-part series on Quicksort. (See here and here). I read the posts and felt I understood the algorithm but knew that in order for it to fully sink in, I would need to code quicksort on my own. I could then compare it to the Ruby built-in sort method using the ‘benchmark-ips’ gem.

My custom quicksort consistently performed slower than the Ruby sort method on arrays of various sizes of random numbers. I added a bubble sort method to the comparison to give me some satisfaction that my method performed well. This was a fun exercise for interview practice and I plan to keep experimenting with sort methods. I also need to research how the Ruby quicksort is optimized so that I can attempt to close the performance gap on my custom quicksort. I think the knowledge I could learn doing that would help me to become a more efficient developer. I’ll be saving my work on Github here.
