"I Know This is Crazy, but I Think the Tests Might Be Wrong"

jacobfromm

Jacob Fromm

Posted on March 7, 2021

"I Know This is Crazy, but I Think the Tests Might Be Wrong"

I recently received a coding challenge from a company I'd love to work for. I wanted to share the experience here because I think it might be valuable for other junior devs and bootcamp grads in the early stages of their job search.

I had been told that the challenge would test my Ruby skills, and that once I received it, I would have five days to complete it. Once I started, however, the assessment was supposed to take about forty-five minutes—fifteen for the first section, and thirty for the second section.

In preparation for the assessment, I opened up some old Rails projects, took Ruby and Rails assessments on teamtreehouse.com, and completed LinkedIn's Ruby on Rails skills assessment. On the day I had decided to complete the challenge, I was feeling prepared but anxious.

For the first section, I had the opportunity to choose which language I wanted to code in. I chose Javascript because I knew the second section was in Ruby and I wanted to demonstrate competency in a variety of languages. I completed the first section, but the time it took me to finish was considerably longer than was estimated. Because of that, I was even more anxious going into the second section than the first: even if my code was right, I didn't want to be seen as a slow and inefficient developer.

The instructions for the Section 2 presented a challenging but not impossible task. Given the input of a hash, I was tasked with sorting the hash such that the keys were sorted by length and their values sorted alphabetically and / or numerically. Unfortunately, I didn't save the actual instructions, so the above description is paraphrasing, but given the following input: {"category_names"=>["Sports", "Music", "Food"], "user_ids"=>[562, 332, 555, 311, 987], :videos=>["/videos/987569", "/videos/43222", "/videos/54322"]},
the expected output should be:
{ :videos =>["/videos/43222", "/videos/54322", "/videos/987569"], "user_ids"=> [311, 332, 555, 562, 987], "category_names"=>["Food", "Music", "Sports"] }

I tested my code in repl.it using the given input until I received the expected output. My solution looked like this:

def sort_hash(hsh)
      sorted_hsh = hsh.sort_by {|k, v| k.length}.to_h
      sorted_hsh.map {|k, v| [k, v.sort]}.to_h
end
Enter fullscreen mode Exit fullscreen mode

First, we sort the input hash, hsh, by the length of its keys using hsh.sort_by. We then convert that result back into a hash, since Hash.sorty_by returns an array of arrays (AoA).

Next, we map through sorted_hsh and sort each value on our way through. Finally, we transform the array returned by the Hash.map function into a hash. It's a simple solution that could probably be refactored and may not account for edge cases, but it does work. That is, given the example input, it returned the desired output as defined in the instructions.

I was excited to see my code producing the correct result in repl.it, but when I went back to the assessment to run my code through the assessment's tests, the tests failed! I was confused, disappointed, and very stressed. I told myself to take a breath and read the error messages, and when I did, I found a bit of a head-scratcher: as far as I could tell, the expected output (according to the tests) was identical to the example input.

To test my theory, I commented out my code and ran the function with only one line of code: return hsh. Sure enough, the tests passed. As far as I could tell, my hypothesis was confirmed, and two things were true: my code was right and the tests were wrong.

As my heart pumped with anxiety and sweat dampened my shirt collar, I took screenshots of my code, the instructions, and the error messages, and explained my thinking. I included edited versions of those notes to both the recruiter I'd been in touch with, as well as a senior engineer with whom I'd had an informational interview a few weeks prior. I did my best to humbly

💖 💪 🙅 🚩
jacobfromm
Jacob Fromm

Posted on March 7, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related