Problem Solving Code Challenges

Brett Cole
5 min readJun 11, 2018

--

Coding challenges are almost a right of passage to developers. Ask any developer you know if they have ever completed a code challenge. Most likely 100% of them will say yes and absolutely on more than one occasion. So that’s what we will do with this blog post.

We will discuss two challenges from a Udemy course. You can find this course here. The other challenge we will discuss I was asked through a technical review. Let’s get started.

The first question asked:

given an input of an array, make a function that sorts the values but also when theres equal values create separate included arrays. For example with an array equal to [1,2,4,591,392,391,2,5,10,2,1,1,1,20,20] should return [[1,1,1,1],[2,2],4,5,10,[20,20],391,392,591]. Oh yeah bonus if you separate string values from integers.

Let’s write a little psuedo-code and figure out our approach.

// first sort array of numbers and strings
// first check if number is string push into separate variable array
// second if check value equals next value in array and push into nestedArr if so
// third if value doesn't equal next value in array push that current value into nestedArr then push nestedArr into finalArr and reset nestedArr to empty array
// finally if no if statements pass just push value into finalArr

Now that we have what we want our code to do lets implement it.

So this is how I solved this challenge. First I separated sorted the array into a separate function. Inside my questionOne(arr) function I created 3 empty array variables. Sounds excessive I know. But they each served a purpose. finalArr is what it sounds like the final array to be returned. nestedArr is the array I used to create nested arrays with the same values and stringArr is the final array for the string values.

Now to the for loop. Inside first I check if the iterated value is a string and if it is then I push it to stringArr. Then I check to see if the value is equal to the next value. If that returns false then I check to see if the value does not equal the next value and also equal to the previous value. This stops from push values unnecessarily and also included the needed value in the created array. Finally if the value doesn’t equal any other value it’s just pushed into the finalArr. Finally after the for loop I returned the finalArr with all of it’s items. So if we did everything correct we should return an array value of [[1,1,1,1],[2,2,2],4,5,10[20,20],391,392,591,['2','3','10']]. Pretty cool.

Next question

Next challenge here we go.

Write a function that takes an array and a target number. This function should find two different numbers in that array that, when added together, give the target number.

Ok again let’s write some psuedo-code to get a game plan together.

// used array from question one
// just created random target value from array
// first for loop iterates value against each value of array
// second for loop compares first for loop value against each value in array
// if at end of loops answerArr is empty return 'Sorry' string

Time to implement our plan.

First things first I created an empty array to push my to values into. Then inside my for loop I created another for loop. This gave me the opportunity to initially take each value in array and then compare it iteratively to each value in the rest of the array. Now my if statement first checked if the two values equalled the target but then also checked to see if they were equal to each other. This would make sure that the two values were unique from each other. Finally I just created an if statement that checked if there were values. If not I would return the message 'Sorry no numbers combined equal that target value.'. So with a target value of 982 we return the array of [591, 391].

Best still to come

Ok it’s time for the worst question.

Write a function that takes a list of strings an prints them, one per line, in a rectangular frame. For example the list [‘Hello’, ‘World’, ‘in’, ‘a’, ‘frame’] gets printed as:

Sorry for the ESLint error squiggles. But you get the idea. When I first looked at this honestly I had never done anything like this before. I’ve never used JS to basically draw shapes with code inside. But after working with it here’s what I came up with.

So what’s the approach to this? Well first I had to find the longest word because of the lost space with the smaller words. So I separated that into it’s own function. Then inside myTextBox function I assigned a few variables. I think if you look at them they are kind of self explanatory. Then inside my for loop my if statement checked if each word was smaller then the longest word found in array. If it was then it first found the difference between that word and the longest word and assigned it to sub. Then I assigned a number of spaces based on that number difference. Finally I added that word with the necessary spaces to pick up the difference and keep the rectangle shape. Otherwise it had to be the longest word so it just added to the finalString variable. Finally at the end I added the final line of asterisks. So with the array of ['Hello', 'World', 'in', 'a', 'frame']. We create the rectangle

TA-DA! There you have it. Three coding challenges covered. Now there are so many coding challenges out there that these don’t even scratch the surface. But I figured I would just show you my approach to three different ones.

So time for the take away. I didn’t touch on it a whole lot but psuedo-code works great to visualize what you are trying to accomplish. Also keep up with coding challenges they can only benefit you. There’s so many sites specific to challenges. CodeWars, HackerRank and so many others.

Well that’s it for this blog. So as usual I guess until next time I’m out of here.

Goodbye! Till next time!

--

--

Brett Cole
Brett Cole

Written by Brett Cole

Full Stack Developer. Husband. Father.

Responses (1)