Problems Patterns
By: David Cha
Tags: Algorithms, Beginner, Patterns, JavaScript

Different pieces to help solve the puzzle. In our case as coders, its problems.

Hey friends hope you are all positive and testing negative! Today I want to talk about problem solving patterns for organizing your data. As a coder you will encounter so many of these patterns throughout your career. You will especially see them throughout Leet Code, Hacker Rank and technical interviews. The patterns that will be discussed are: Frequency Counter, Multiple Pointer, Sliding Window, and Divide and Conquer. Of course there are a lot more patterns, but I believe starting off with these four patterns will help beginners get the flow of creating such patterns. Getting the flow will allow you to go on to more complicated patterns and I am excited to tell you more about in the near future! Let’s get on to the first four patterns.

1. Frequency Counter Pattern

How many times/values occur during this runtime?

Our first pattern uses objects or sets to collect values/frequencies of values. This is useful for when you have multiple pieces of data inputs and you need to compare them to see if they consist the same values, hence frequencies. I will be using a LeetCode problem to help explain the process of this pattern. This problem is called validAnagrams and we are required to check and see if the two string inputs consist of the same amount of letters. We need to see if our pieces of data inputs (characters of both string) occur the same amount of times.

Figure 1.0 Lookup is our frequency counter and it is going to store the values of the first string.

In figure 1.0, we created a variable object called lookup. That variable is our frequency counter and if we take a look at lines 9 through 13, we are storing the elements into our variable. Look carefully on line 12, we are using a ternary statement to check whether or not our frequency counter has stored the same letter. If the letter is already stored in our lookup variable, increment it by one and if not we will set it to one. This part is very important because we are not just storing the elements from the string, we are also counting the amount of times it has occurred. Now if we take a look on lines 14 through 20, that is the frequency pattern at work. Just like how we were adding the letters into the variable lookup, we are now taking away the letters and the amount it has occurred. We are using conditional statements to check the second string’s letter and the variable lookup. If the letter from the second string does not match up with any of the letters that lookup has kept track of return false, otherwise decrement the occurrence by 1.

2. Multiple Pointer Pattern

Pointers will start from both ends of the spectrum.

In our second pattern, it utilizes pointers or values that correspond to an index or position and moves towards the beginning, middle, or end based on a certain condition. In the following problem, we are going to check and see if the sorted array has a sum of zero. We will need to iterate the array and see if the pointers add up to zero.

Figure 1.1 Our array for this input is [-15, -5, -3, -2, 1, 2, 3, 123]

Before we start adding the indexes we need to create pointers. In figure 1.1, we are creating a left and right variable to keep track of where we are in the array. Because we need to check every single index, we use a while loop that will stop when the left becomes greater than the right. As we iterate through the indexes, we will add up the index element of left and right and assign that value to the variable sum. We will then check to see if this variable sum is equal to 0. If it does equal to 0, great we will return the index element. What happens if the sum is greater than 0? We will then decrement the right by 1 and vice versa if the sum is less than 0.

3. Sliding Window