Skip to content

Instantly share code, notes, and snippets.

@jaypeasee
Last active February 18, 2021 18:54
Show Gist options
  • Save jaypeasee/928ccf50fcd51c3092a2b7a588b900ec to your computer and use it in GitHub Desktop.
Save jaypeasee/928ccf50fcd51c3092a2b7a588b900ec to your computer and use it in GitHub Desktop.

The Snail Tech Challenge

Rewrite the question in your own words:

You are given an array with a length of a square number. Return an array that is rearranged as if the array was an array of arrays in rows.

What assumptions will you make about this problem if you cannot ask any more clarifying questions? What are your reasons for making those assumptions?

  • I will assume that this argument in function invocation can be an array with a length of any square root or square number.
  • I will assume I must return a single array
  • The innermost piece of data will likely be a number, string, or object or boolean - not another array
  • I should assume I dont need to make edge cases if the array length is not a square number

What are your initial thoughts about this problem? (high level design, 2-3 sentences)

  • my initial thoughts are that I will need to iterate through the array and ultimatelt turn it into a single array
  • I will start at the top, move right, then down, then at the bottom move right to left and then back in.

How would you identify the elements of this problem?

  • Sorting of Data
  • Pattern Recognition
  • Build/Navigate a Grid

Which data structure(s) do you think you'll use? What pros/cons do you see with that choice?

I am given an array which may contain arrays, but ultimately I will need to return a single array with consistent data on the inside. Best way to know at first if it's working is to use inputs of numbers.

Write out a few lines of initial pseudocode: (mid-level design, NOT REAL CODE)

  1. create a new variable assigned to an empty array
  2. iterate through the parameter (for loop)
  3. For index 0, iterate through (forEach) and push into new variable array
  4. For the last index in the array, iterate through backwares (reverse forEach) and push into new variable array
  5. Otherwise, push the last element in each middle array
  6. Start a new iteration (backwards for loop) to work back up in the perimeter
  7. iterate through each mini array (forEach)
  8. if the number in the mini array is not contained inside of the new variable array, push it in.

Write out any implementation code OR link to repl

const arrayMatrix = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9]
]

function snail(matrix) {
  let newOrder = []
  for (let i = 0; i < matrix.length; i++) {
    if (i === 0) {
      matrix[i].forEach(num => newOrder.push(num))
    } else if (i === matrix.length - 1) {
      matrix[i].reverse().forEach(num => newOrder.push(num))
    } else {
      newOrder.push(matrix[i][matrix[i].length -1])
      }
  }
  for (let i = matrix.length -1; i >= 0; i--) {
    matrix[i].forEach(num => {
      !newOrder.includes(num) ? newOrder.push(num) : null
    })
  }
  return newOrder
}

snail(arrayMatrix)

What is the Big O complexity of your solution?

I am not entirely sure besides that I know it will be complex because it deals with nested loops at least thrice. so thats 2 + 2 + 2 + 1?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment