DEV Community

Discussion on: Why I Stopped Interviewing with Companies That Require a Coding Test

 
joshcheek profile image
Josh Cheek • Edited

NVM, I saw your other comment with the specific sorting question in it, and I get it. It's a question about "are you familiar with or can you come up with this 1 weird trick" rather than "can you implement a sorting algorithm". Yeah, I wouldn't really expect anyone I work with to come up with that solution, and while it's theoretically slower, I just tested them both, and the builtin sort was over twice as fast, and way more obvious.

ruby -e '
   def time
     start = Time.now
     yield
     Time.now - start
   end

   def sort(a)
     counts = Array.new 100, 0
     a.each { |n| counts[n] += 1 }
     sorted = Array.new a.size
     i = -1
     counts.each_with_index do |count, index|
       count.times { sorted[i += 1] = index }
     end
     sorted
   end

   a = 1_000_000.times.map { rand 0...100 }
   puts correct: a.sort == sort(a),
        builtin: time { a.sort },
        custom:  time { sort a }
   '
{:correct=>true, :builtin=>0.039097, :custom=>0.087166}
Enter fullscreen mode Exit fullscreen mode