Data Structures & Algorithms: Practice #1

Photo by Markus Spiske on Unsplash

In technical interviews, it’s common to be given various problems that test your data structures and algorithmic skills. While the Flatiron School occasionally gave problems that tested these skills, the majority of the curriculum was focused on understanding a language’s syntax, use, and quirks for the purpose of applying it towards app development. Thus, upon the suggestion of fellow Flatiron alum Alison Quaglia I decided to invest in some classes at Udemy to practice solving common interview problems. Currently, I’m progressing through Stephen Grider’s ‘The Coding Interview Bootcamp: Algorithms + Data Structures’ which I highly recommend due to his clear, concise instruction and the progression of problems you are presented to solve, as well as the well-explained alternate solutions that expand your current abilities and approach.

One problem that initially stumped me was the popular Chunked problem. The directions are given below:

There are a couple of things to keep in mind here. One, the length size indicates the maximum amount of elements in a ‘chunk’ array. Even if the original array length does not neatly divide into ‘chunks’ of ’n’ size, the remaining elements must be pushed into a ‘chunk’ to return the right ‘chunked’ array.

Additionally, knowledge of array methods will be helpful here, like the ‘.push()’ method.

Possible Solution #1

So first let’s pseudo-code one possible solution.

Originally, I was trying to code a solution that went through the original array, tried to ‘chunk’ it, and push it into another overall ‘chunked’ array. The above approach is different, however, in that it’s first testing to see if a ‘chunk’ is completed, and then pushing it into a created array.

So how could this look like in code?

Possible Solution #2

Now of course there are many ways to solve this. Another possible answer uses the method .slice() instead. The slice method is very handy, as it allows you to go into an array, and modify elements at a desired index. Other methods (.pop(), .push(), .shift(), .unshift()) have a predetermined location, but slice gives much more freedom to the user. The only tricky thing about slice is that while the first number provided refers to the desired start index, the second optional number or ‘end’ . Thus, it can be a bit tricky to work with, but a very helpful tool once mastered. Here’s a possible pseudocode solution using .slice().

So what could this look like?

This solution is more in line with my original approach but uses .slice() to create the ‘chunks’ from the beginning to add to a created array. It’s neater but definitely harder to come up with on the spot in an interview. That’s why we practice!

Flatiron School Software Engineering program grad and a former English Language Instructor in South Korea. Roams between Boston & Seoul.