Cassidoo’s Interview question of the week | 444

Hello folks! I got totally hooked up to Cassidy Interview QOTW, and I’m delighted that there is a place where we can share and compare and learn about the solutions in Ruby! :heart:

I never imagined that you could actually provide a one-liner answer to this one (via endless method), I like yours @fpsvogel! But I’m not sure why you and @charlie believe that your solutions are so inefficient? :snail:

@eayurt as I am starting to use AI in my learning process (more like a search on steroids), I would like to know how you used it for this solution? You prompted it with the question or somehow else?

@roasted-oolong you have one typo in your solution: you are using integer as a parameter for the factor in your method, and then inside the method you are using k as a factor. :eyes:
BTW, one way to put a code in markdown code block is to fence it with an open ```ruby and closing ``` tags, or just write ``` in a new line and a code block will appear and you can paste your code inside it. :blush:

So, back to my answer, which is BTW inspired by the recent exposure to Sandi Metz’s Smalltalkism—I really like the idea of sending messages to the objects! :upside_down_face: I was absolutely sure that my n00by, implement-the-first-obvious-pattern, 4-nested-loops (YUCK!!!) solution would be the most inefficient/slowest one, BUT no,@lpogic won that medal, although I’m not sure why, your solution looks so idiomatic and beautiful and technically advanced! :frowning:

So, here it is, my monkey-patched version (with tests), and below you have benchmarks for all provided solutions.

P.S.
Pardon my lengthy post, again, I can’t stress enough how great it is to be able to have a forum place to talk about Ruby! And I was a fan of em dashes before it was (AI) cool (drink). :stuck_out_tongue:

module ZoomifyArray
  def zoom_in_by(factor)
    raise ArgumentError, "Factor MUST be an Integer greater than or equal to 2!" unless factor.is_a?(Integer) && factor >= 2

    number_of_rows = size
    number_of_cols = first.size
    bigger_grid    = Array.new(number_of_rows * factor) { Array.new(number_of_cols * factor) }

    each_with_index do |row, row_index|
      row.each_with_index do |cell, col_index|
        0.upto(factor - 1) do |shift_row_index|
          0.upto(factor - 1) do |shift_col_index|
            bigger_grid[(row_index * factor) + shift_row_index][(col_index * factor) + shift_col_index] = cell
          end
        end
      end
    end

    bigger_grid
  end
end

module ArrayRefinements
  refine Array do
    import_methods ZoomifyArray
  end
end

BENCHMARKS:

==> Benchmarking 10x10 grid with random numbers from 10 to 99 and factor 10
ruby 4.0.1 (2026-01-13 revision e04267a14b) +YJIT +PRISM [x86_64-linux]
Warming up --------------------------------------
izkreny_zoom            364.000 i/100ms
charlie_zoom             5.106k i/100ms
lpogic_zoom             209.000 i/100ms
fpsvogel_zoom            5.167k i/100ms
eayurt_zoom              5.327k i/100ms
roasted_oolong_zoom      5.185k i/100ms
Calculating -------------------------------------
izkreny_zoom              3.463k (± 1.3%) i/s  (288.75 μs/i) -     17.472k in   5.045991s
charlie_zoom             51.352k (± 2.0%) i/s   (19.47 μs/i) -    260.406k in   5.073118s
lpogic_zoom               2.083k (± 1.8%) i/s  (479.98 μs/i) -     10.450k in   5.017523s
fpsvogel_zoom            51.973k (± 1.9%) i/s   (19.24 μs/i) -    263.517k in   5.072110s
eayurt_zoom              53.469k (± 2.5%) i/s   (18.70 μs/i) -    271.677k in   5.084349s
roasted_oolong_zoom      52.711k (± 1.7%) i/s   (18.97 μs/i) -    264.435k in   5.018322s

Comparison:
eayurt_zoom         :    53469.0 i/s
roasted_oolong_zoom :    52710.6 i/s - same-ish: difference falls within error
fpsvogel_zoom       :    51973.2 i/s - same-ish: difference falls within error
charlie_zoom        :    51352.4 i/s - same-ish: difference falls within error
izkreny_zoom        :     3463.2 i/s - 15.44x  slower
lpogic_zoom         :     2083.4 i/s - 25.66x  slower

==> Benchmarking 100x100 grid with random numbers from 100 to 999 and factor 100
ruby 4.0.1 (2026-01-13 revision e04267a14b) +YJIT +PRISM [x86_64-linux]
Warming up --------------------------------------
izkreny_zoom             1.000 i/100ms
charlie_zoom            30.000 i/100ms
lpogic_zoom              1.000 i/100ms
fpsvogel_zoom           31.000 i/100ms
eayurt_zoom             25.000 i/100ms
roasted_oolong_zoom     29.000 i/100ms
Calculating -------------------------------------
izkreny_zoom              0.347 (± 0.0%) i/s     (2.88 s/i) -       2.000 in   5.774075s
charlie_zoom            286.291 (± 4.2%) i/s    (3.49 ms/i) -      1.440k in   5.038385s
lpogic_zoom               0.255 (± 0.0%) i/s     (3.93 s/i) -       2.000 in   7.870985s
fpsvogel_zoom           275.924 (± 7.6%) i/s    (3.62 ms/i) -      1.395k in   5.089186s
eayurt_zoom             284.645 (± 4.6%) i/s    (3.51 ms/i) -      1.425k in   5.017416s
roasted_oolong_zoom     288.974 (± 4.2%) i/s    (3.46 ms/i) -      1.450k in   5.027366s

Comparison:
roasted_oolong_zoom :      289.0 i/s
charlie_zoom        :      286.3 i/s - same-ish: difference falls within error
eayurt_zoom         :      284.6 i/s - same-ish: difference falls within error
fpsvogel_zoom       :      275.9 i/s - same-ish: difference falls within error
izkreny_zoom        :        0.3 i/s - 832.38x  slower
lpogic_zoom         :        0.3 i/s - 1135.40x  slower

4 Likes