# Code Challenges In Review

Everyone’s first step to landing that dev role: becoming decent(or awesome!) at code challenges

When I first started to do code challenges on Code Wars, I really struggled. I found it difficult to solve problems when I was still in the process of nailing down basic syntax structure of languages. Now that I have some more of the basic functionality of programming secured, code challenges can still be challenging when you see them at first but seem a little more manageable. Below are some of the problems I’ve worked on lately, with some commentary.

# Sum a number

The first one I’ll discuss is a simple sum a number. The reason I’m choosing this problem first as I think it has great commentary on Big O. Big O if you aren’t familiar is a practice used to mathematically calculate how long and how much space your algorithm takes to return a value. Since so much of programming is about efficiency, calculating how much time it takes for your function to run should always be on-top-of-mind for any programmer. It may be okay if it only takes 1-second for your computer to sort an array with 100 values, but wouldn’t it be a problem for the end-user if all of sudden it started to take 10+ seconds because the array grew to 1 million values?

The first solution above is how most people would solve this problem. However, for every increase in n, your code will require to run an extra time (if n was either 10 || 11, running for n === 11 would require 1 additional calculation when it comparing to n === 10). This is called having an o(n) runtime. The second solution, however, is said to have o(1) runtime. n could be 1 or it could be 1,000,000, it doesn’t matter, the code will only need to run a single time no matter the variable. This is rare, but o(1) is the fastest runtime that can be achieved.

# Anagram Challenge

A common junior-dev question is to determine if two words are anagrams, meaning all the characters in the first word === the second word (listen and silent are anagrams, but pencil and pen are not). The most intuitive way to solve this problem is to make a hash map: take all of the characters and assign them as keys, and determine how many times each letter appears in the hash (or object):

In the above, I declared two empty object as variables. I then iterated through both of these objects with for loops. The syntax here is ES6, which was introduced as an alternative to: `for(let i=0; i < string1.length; i++)`. I try to use the ES6 syntax whenever it is available and I don’t require to iterate over every-other character or something similar. The ternary operations are saying if you have seen that key before, increment its value by 1, and if not, set its value equal to 1. Finally, I then needed to iterate over the objects, which required a `For In` loop as opposed to what I used previously, a `For Of` loop. If all the key/value pairs of the first object are not equal to the second object return false, but if they are, return true.

# Fibonacci Sequence

Determining the Fibonacci sequence in an interview seems to be a common question, as again it has good discussion on time complexity (Big O).

First, the iterative solution:

Next, using recursion:

The recursive solution involves less code, and may even be easier to read for most people. However, its time complexity is terrible O(2^n), which is worse than O(n²). For every increase in n, the calculation increases exponentially as it has to call on significantly more sequences in its tree. To avoid this, you can use a trick called memoization, which stores already calculated functions in a cache:

# Square and Sort

This may seem like another easy problem, but the first time I tried it I returned an unexpected result, so I thought I would include it:

The first time running this function I did not have a callback function on lines 10 and 11, leaving me with an unexpected return value when array was set to [-4, -1, 0, 3, 10]. When running this function without the callback, you would log: [0, 1, 100, 16, 9]. Huh?? This is because JavaScript is only evaluating the first digit in each integer (0, then 1, then 9). This is one of the many odd quirks JavaScript has in its codebase, I hope to find more of them over the years. Including the callback function returns an array of [0, 1, 9, 16, 100], the intended return value. The above solution has an O(n log n) time complexity, which is okay but not great. In my next blog, I’ll try to improve this solution with a double-pointer example.

# Commonly Used Functions

I’ve heard from many people your coding challenges aren’t intended to be a memorization contest, I still find that hard to believe. It has been difficult to articulate what function you need to pull from when you don’t know the name of the function. Here are some commonly used functions that continue to pop-up in these situations:

.join(): returns the array as a string.

.split(): Splits a string into an array; returns a new array. Using the (‘, ‘) syntax will split the string every location there is a comma. Using (‘ ‘) will split everywhere there is a space, and insert a comma. () will split every letter into its own element. Cannot be used on arrays.

.splice(): Changes an array by adding or removing elements from it. Does not work on strings.

.slice(): Returns selected elements in an array, as a new array. Original array is unchanged. Does not change original array. Also works on strings.

.fill(): fills specified elements in an array with a static value. Overwrites the original array. array.fill(value, start, end).

.push() / .pop(): Add new item to end of an array, return new length / remove last item, return new length

.shift() / .unshift(): Remove first item / Add item to beginning of array

.reduce(): Sums values using acc + item:

I plan to continue to more of these with additional methods as I continue working through other Data Structures & Algorithms. In the meantime, happy coding!