5 commandments to avoid toxic situationships

I love how right off the bat I used two buzz words « toxic » and « situationship » in my title. In a way, I meant both but I also meant neither so let me explain. 8 months ago, I came to a point…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




How to Find an Anagram With JavaScript Using PEDAC

Ace your coding interviews with PEDAC

In a coding interview, you really want to show your problem-solving skills, without looking like the guy on the cover. By using the PEDAC problem-solving method we will have a plan to solve any potential coding interview problem. For those who don’t know what PEDAC is, it’s an acronym for:

Problem Examples Data Algorithm Code

I will be going through each one to help clarify each step.

Before any code is written, one must understand the problem. In this case, the problem is determining whether a list of given words contains an anagram of a word. Firstly, we must understand what an anagram is. According to our Google overlords, an anagram is a word or phrase whose letters can be rearranged to create another word or phrase. Now that we understand what an anagram is, we know that all of the letters from the original word must be found in the words of the given array. To better rephrase the problem, we can say that we will go through the array and check if any of the words have the same characters and number of characters as the original word. We have now explained the problem in our own words and can move on to the next step.

We must now create an example of how the program will work. We will provide inputs and outputs for this program. If we have the word `iceman` and we are provided with a list of words such as `anime, cinema, and google` we know that the output will be `cinema` because it is the only word that has the exact same letters as the word iceman just in a different order. Another example can be the word `listen` along with the list of words: `enlists, enlist, lists, inlets`. If those are the inputs, the outputs are `enlist, inlets`. Now that we’ve seen some examples of inputs and outputs, we can move to the next step which is Data.

E: (iceman, ["anime", "cinema", "google"]) => ["cinema"]
(listen, ["enlists", "enlist", "lists", "inlets", "words"]) => ["enlist", "inlets"]
(sushi, ["words", "youtube", "great", "hush"] => []

In this step, we will determine which data structures we will use. When we have a list of something we will most likely use an array. In this case, it is a good idea to use arrays because we can mutate the contents of the array making it easier to see if the words match one another. Data isn’t always going to be arrays, depending on the problem you might decide to use objects or strings or Booleans. By determining the data structure that you are using for the specific problem you can see which methods are available to be used. It will also be helpful if you had a specific method you wanted to use. This will help because it will give us a clue as to whether or not we will be converting the data type.

In this step, we will determine a step by step solution and write it in pseudo-code. For this specific problem we know we need to find a way to compare each item to the original. That’s because each word that is an anagram of the original will be returned. A good question to ask is “how can we compare two words?” We can’t necessarily say if (inlets === listen) return inlets. It would always be false because they are not the same thing. We can use .sort() to change the words in an order that is comparable and then return the original word. By using sort we are changing the first word to its alphabetical order, and by doing the same to each word in the array we now have comparable data.

An example of what the algorithm looks like is:

Algorithms may change as you code but the point is to have a plan that will make the coding part easier.

This is where we put all of the pieces together and code out our algorithm. If the algorithm is clear, the coding is a lot easier. There might be a couple of problems we hadn’t thought of but all of that can be fixed as long as we have a plan. Here is how we can code out the solution using the algorithm and what we know about sort.

Code without comments:

Conclusion

I hope this helped. We all know that coding interviews may be anxiety-inducing but with PEDAC you have a plan of attack. Using PEDAC will provide you with a structure to follow and a way to really break down each problem into smaller parts. The problem won’t seem so big when broken down. I hope that you’ll find PEDAC as useful as I have!

Add a comment

Related posts:

There is always much to hear from macaque leaders

These leaders will make you realize how conflicts and disruptions might affect your organization's leadership chain, averting reaction and gaining an edge