The Ruby Array class offers some of the most-used methods in the ruby ecosystem. A few months back we published a quiz, testing knowledge of ruby arrays and their associated methods. In this short post we review average performance against this quiz and identify which questions posed the biggest challenge.

Introduction

A few months ago we published a ruby arrays quiz. This was a 10-question quiz, allowing you to self-test your knowledge of this fundamental component of the ruby language.

In this post we will use the responses collected to see how people fared on average, and to identify which question posed the biggest challenge.

Breakdown by score

At the time of writing the quiz has a total of 324 attempts registered, with an average score of just 29%. The distribution of scores is summarised in the barchart below:

Frequency plot showing number of respondants registering a score in different buckets, 0-10, 10-20 up to 90-100
Distribution of scores for all attempts against the Ruby Array quiz

The mean score across all responses was 29%, whilst the modal scoring bracket was 30-39. Responses reaching the higher scoring brackets were rare. Indeed, at the time of writing, if you scored over 70% you were in the top 5% and only three responses exceeded the 80% mark!

Breakdown by question

We can look at how users performed against the individual questions. The bar chart below summarizes the percentage of correct responses for each question, you can click on the individual bars to see a screenshot of the question in each case.

The Hardest Question

From the graph, question 6 immediately jumps out as the hardest question, with only 2.07% of respondants getting this question correct. The question considered array (and string) manipulations using combinations of some fairly common operators including split, join, reverse, map and sum. You can learn more about the specifics of these different methods from the offical Ruby docs for Array and String. The actual question posed was as follows:

Screenshot of Qu 6 from Ruby Arrays quiz: Which of the following will result in the string \
The hardest question on the Ruby Arrays quizs

The question proposed a number of ways to generate the string "12345". Some of these techniques will give us what we want and some will not. Our challenge is to select one or more options that do the job. One of the key pieces of knowledge required to answer this question pertains to the String#split method:

    
"once,twice,three times a lady".split(",") 
=> ["once", "twice", "three times a lady"]
    
  

The method splits the string into an array, based on the delimiter supplied, in this case we have passed a comma. If we refer to the docs for the method, we see that when we don't specify a pattern to split on then it will, effectively, split on whitespace:

If pattern is nil, the value of $; is used. If $; is nil (which is the default), str is split on whitespace as if ' ' were specified.
Splitting a string with no whitespace, e.g. '12345', will just give an array with a single element, i.e. ['12345']. We will see that this understanding is crucial to understanding two of our proposed solutions below. Let's now consider each option in turn:

  • (1...5).map(&:to_s).join(''): The range (1...5) has three dots which means it will not include 5, so this expression results in a string of "1234". If we had used the two-dot version of the range, i.e. (1..5), then we would have obtained the desired result.
  • "54321".split.reverse.join: Applying split to the string "54321" leads to a single-element array (splitting on whitespace by default). The Array#reverse will have no effect on the single-element array and the output after join will just be "54321". So this is not what we want. Note, had we used split('') in this example it would have produced the desired output.
  • "54321".reverse: Applying reverse to a string will do exactly what we expect, so this is a valid answer.
  • 12345.to_s.split.sum(''): Converting the number 12345 to a string, then running split and sum is logically sound, but note that split will generate a single-element array, as discussed above. So the split.sum('') operations have not net impact on the string, and the result is "12345". But this is what we want, so it is a valid answer..

Summary

We have presented a short analysis of the results registered against our recently published Ruby Array Quiz. The most difficult question was identified to be question 6, which challenged our understanding of Array and String methods, when used in concert. We looked at the correct response for this question, identifying a couple of learning points along the way. In particular, the default behaviour for String#split when called with no arguments is to split on whitespace.

I hope you found this short article useful. If you have any questions or feedback we would love to hear from you on the comments. If you enjoyed this content please consider subscribing to be kept updated when new content is published. Thanks for reading!

References

  1. The original quiz on ruby arrays
  2. Blog post about Set operations on Ruby arrays
  3. Official Ruby Array docs
  4. Official Ruby String docs

Comments

  • Submitted by Dale
    over 1 year ago

    Do you think this was the least answered correctly question because of the time to solve i.e. multiple options, each with multiple methods to recall, but the same time limit resulting in a speed test instead of a knowledge test? This might be indicated by a higher number of timeouts with no answer at all for this question. This is not sour grapes on my part, I know I only got one of the options for this question correct. Thanks for the fun quizzes!

    • Submitted by Domhnall Murphy
      over 1 year ago

      Many thanks for giving this a try, and for your feedback. I think this is a fair point, although I do not have access to the timings for different question responses to test the assumption properly. For the few quizzes created I have received similar feedback about the aggressive time limit preventing a fair attempt at the question. This question would definitely require a bit of time to work through mentally, so perhaps the 45 second time limit is unrealistic. I'm a bit reluctant to change the terms of this quiz, after it has been published, just for consistency in experience. But I will definitely take this feedback on board when designing future quizzes. Many thanks.

Got your own view or feedback? Share it with us below …

×

Subscribe

Join our mailing list to hear when new content is published to the VectorLogic blog.
We promise not to spam you, and you can unsubscribe at any time.