JS

Message Decoder by Zach Mayer

I can't remember where I first heard about this one but I like it because it's almost exactly as simple as it sounds, but if you insist on working through the whole solution it's a great question for junior engineers.

Given a string representing an encoded matrix and a decryption key, write a function to decode the message.

This one requires a bit more explanation. First, our "encoded" string looks like this...

"I B C A R L A M T A R G C E S T A C M H O W A A"

Now, imagine this string as a matrix the length of which matches the given integer key, say 3, and the message is in the highlighted characters below...

[
  ['-I- ', 'B',  'C',  'A', '-R-', 'L',  'A',  'M'],
  [ 'T',  '-A-', 'R', '-G-', 'C', '-E-', 'S', '-T-'],
  [ 'A',   'C', '-M-', 'H',  'O',  'W', '-A-', 'A']
]

Can you see the pattern? Starting from the top left, move down and right, then up and right, and so on until you reach the end of the matrix.

Our task is to first munge the input into the desired shape, and then scrape through the matrix for the message.

Here's the code that does just that, with the slight twist that I chose to arrange the matrix by column, but either way the core is the same.

Fun fact: the message in the code above can also be decoded with a key of 4 and a different message :-p