<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Donny</title>
    <description>The latest articles on DEV Community by Donny (@kuddleman).</description>
    <link>https://dev.to/kuddleman</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F188151%2F5b9e6086-06ed-4e56-b5f6-492522107548.jpg</url>
      <title>DEV Community: Donny</title>
      <link>https://dev.to/kuddleman</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kuddleman"/>
    <language>en</language>
    <item>
      <title>Array Method in JavaScript:  Reduce</title>
      <dc:creator>Donny</dc:creator>
      <pubDate>Mon, 30 Nov 2020 20:36:14 +0000</pubDate>
      <link>https://dev.to/kuddleman/array-method-in-javascript-reduce-2k03</link>
      <guid>https://dev.to/kuddleman/array-method-in-javascript-reduce-2k03</guid>
      <description>&lt;p&gt;Let’s review one of my favorite advanced array methods in JavaScript: Reduce.&lt;/p&gt;

&lt;h3&gt;
  
  
  What Does Reduce Do?
&lt;/h3&gt;

&lt;p&gt;Imagine taking a bunch of ingredients from a recipe and tossing them one by one into a pot.  Apply heat to that pot.  After the pot cooks for a bit, you’ll be left with a marvelous stew.&lt;/p&gt;

&lt;p&gt;That’s basically what happens in the reduce method.  We take a bunch of items (elements of an array).  Throw them into a pot (an accumulator) and apply heat (logic) .  Finally, we are left with our result--a yummy dish.&lt;/p&gt;

&lt;p&gt;Let’s take a simple example:&lt;/p&gt;

&lt;p&gt;We start with our dish’s ingredients: an array of 4 integers.  Let’s call our array  “ourIngredients.”&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ourIngredients&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our recipe calls for us to take our ingredients, add them all together and come out with a total.  So given our ingredients: 1, 2, 3, 4;  we’re hoping to use our reduce recipe to end up with 10  (1 + 2 + 3 + 4).&lt;/p&gt;

&lt;p&gt;So let’s set it up.&lt;/p&gt;

&lt;p&gt;We’ll need a variable “stove”  to be assigned to the cooking instructions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ourIngredients&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;stove&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The stove will need a pot, then need to take each “ingredient” in turn:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ourIngredients&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; 

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;stove&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nx"&gt;pot&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ingredient&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we have to tell the stove what to do with the pot and the ingredient.  In our case, we just want to add each ingredient to the pot in order to get our cumulative total:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ourIngredients&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; 

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;stove&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nx"&gt;pot&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ingredient&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;pot&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;ingredient&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s put it all together.  All that remains is to “call” our stove on the list of ingredients.  The magic word we’ll use is “reduce”: (We’ll console.log our result)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ourIngredients&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; 
&lt;span class="c1"&gt;// In techno talk, we have:&lt;/span&gt;
&lt;span class="c1"&gt;// 1) declared an arrow function and named it ‘stove’&lt;/span&gt;
&lt;span class="c1"&gt;// 2) passed in 2 parameters: ‘pot’ and ‘ingredient’ &lt;/span&gt;
       &lt;span class="c1"&gt;// (pot, ingredient)&lt;/span&gt;
&lt;span class="c1"&gt;// 3)  told pot to just keep adding the given ingredient&lt;/span&gt;
&lt;span class="c1"&gt;//    to the pot:   pot + ingredient&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;stove&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nx"&gt;pot&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ingredient&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;pot&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;ingredient&lt;/span&gt;

&lt;span class="c1"&gt;//take the ingredients, call the reduce method&lt;/span&gt;
&lt;span class="c1"&gt;// and pass in our instructions (stove)&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ourIngredients&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stove&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;  &lt;span class="c1"&gt;// 10&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is the basic.  Now let’s get a little fancier.&lt;/p&gt;

&lt;p&gt;Our current example assumes we’re starting with an empty pot, that is, at 0.  But what if our pot already had something in it--let’s say an integer “5” was already in the pot before we started cooking.  In that case, we would expect to get 15 at the end of our session (10 + 5)&lt;/p&gt;

&lt;p&gt;Let’s see how to add something to the pot before cooking with “ourIngredients:”&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ourIngredients&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; 

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;stove&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nx"&gt;pot&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ingredient&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;pot&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;ingredient&lt;/span&gt;

&lt;span class="c1"&gt;//But there is a 5 already in the pot:&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ourIngredients&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stove&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;  &lt;span class="c1"&gt;// 15&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Before I leave you for today, let’s ratchet it up a couple of notches with a more challenging example.  This next example comes up from time to time in coding challenges:&lt;/p&gt;

&lt;p&gt;Say we’re given an array of names:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;names&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;Desmond&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;Orlando&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;Leticia&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;Desmond&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;Shane&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;Desmond&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;Madison&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;Leticia&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We need to find out how many times each name occurs and end up with a object like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;names&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;Desmond&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;Orlando&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;Leticia&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;Desmond&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="c1"&gt;// convert the array into an object and count&lt;/span&gt;
&lt;span class="c1"&gt;// the occurrence of each element:&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;

&lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;Desmond&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;Orlando&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;Shane&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;Madison&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;Leticia&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s do it by first setting up our stove.  We know we’ll need a pot and an ingredient as parameters.  When it’s all done, we’ll want to pop the pot off the stove by using a “return” statement:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;stove&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nx"&gt;pot&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ingredient&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;



&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;pot&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now comes the logic.  If the ingredient is already in the pot, all we have to do is increment its count (Its count is the ingredient’s value)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;stove&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nx"&gt;pot&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ingredient&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pot&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="nx"&gt;ingredient&lt;/span&gt; &lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;pot&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="nx"&gt;ingredient&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;pot&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Else, we know the ingredient is not in the pot, so we can set its value or count to 1:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;stove&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nx"&gt;pot&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ingredient&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pot&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="nx"&gt;ingredient&lt;/span&gt; &lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;pot&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="nx"&gt;ingredient&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;pot&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="nx"&gt;ingredient&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;pot&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let’s bring it all together!&lt;/p&gt;

&lt;p&gt;let’s take our “names” array, call our magic “reduce” method and tell it to use our stove method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="c1"&gt;//almost done…..&lt;/span&gt;
&lt;span class="nx"&gt;names&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stove&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Oops!  Almost done.  We forgot two things.  Firstly, we want to console.log the results:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="c1"&gt;// REALLY almost done…..&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;names&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stove&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;AND, we need to tell our reduce method that there was something already in the pot when we began cooking.  What was that?  An empty object, {} !!!!  We’ll need that empty object to hold the transformed array:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="c1"&gt;// Now we’re done.  Notice the empty array after “stove”&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;names&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stove&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And there you are: a primer to the “reduce” method in JavaScript.  Have a look at the MDN with the full treatise on “reduce”  there are some even wilder things you can do with it!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce"&gt;Here’s the link to the MDN article on “reduce”&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Keep on cooking up your dreams with code!&lt;/p&gt;

&lt;p&gt;Namaste!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>reduce</category>
    </item>
    <item>
      <title>Interview Prep:  Implementing A Binary Tree</title>
      <dc:creator>Donny</dc:creator>
      <pubDate>Wed, 25 Nov 2020 20:14:46 +0000</pubDate>
      <link>https://dev.to/kuddleman/interview-prep-implementing-a-binary-tree-365p</link>
      <guid>https://dev.to/kuddleman/interview-prep-implementing-a-binary-tree-365p</guid>
      <description>&lt;p&gt;Welcome back to interview prep.  This week, we’ll build on last week’s intro to tree data structures and we’ll actually implement or build a binary tree in Java.&lt;/p&gt;

&lt;p&gt;If you’ve never heard of the tree data structure, check out last week’s article to get up to speed:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/kuddleman/interview-prep-binary-and-general-trees-2mf4"&gt;Read about the Basics Here&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Our Problem
&lt;/h3&gt;

&lt;p&gt;We’re given a picture of a tree data structure the looks like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fgm67b5zr91edjysyprm0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fgm67b5zr91edjysyprm0.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We are asked to code this tree out in Java.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Left and Right&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We’ll just need a bit of additional terminology to accomplish our task.  Note that each parent has 2 children.  We can say that one child is on the “left” side of the parent and one is on the “right” side.&lt;/p&gt;

&lt;p&gt;For example, in our image of a tree above, we see that the root is the node containing the value “1”.  Its left child is “2” and its right child is “3”.&lt;/p&gt;

&lt;p&gt;To simplify our language, instead of referring to the child nodes, we can refer just to the edges (lines) that lead  from the parent to the child.  Thus, we can say node 1’s right edge (or just “right) leads to node 3.  Node 1’s left edge (or just “left”) leads to node 2.&lt;/p&gt;

&lt;p&gt;With that knowledge, let’s begin to code&lt;/p&gt;

&lt;p&gt;Let’s start by declaring a BinaryTree class and a main class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BinaryTree&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then we’ll need a private field to hold our entry point to our tree.  That entry point is called the “root”.  Let’s declare that field as a TreeNode type (we’ll write the TreeNode class next).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BinaryTree&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;TreeNode&lt;/span&gt; &lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We’ve declared our root note as a TreeNode class type, so we better write that TreeNode class.   &lt;/p&gt;

&lt;p&gt;TreeNode is the class that will produce all the individual nodes we need to construct our binary tree.&lt;/p&gt;

&lt;p&gt;We’ll have to supply each TreeNode with three items: 1) a left edge (we’ll just call it “left”); 2) a right edge (named “right”) and a variable we’ll call “variable” to store the node’s data.   We’ll assume our binary tree can only contain integers as data for simplicity’s sake.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BinaryTree&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;TreeNode&lt;/span&gt; &lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TreeNode&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;TreeNode&lt;/span&gt; &lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// left edge&lt;/span&gt;
        &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;TreeNode&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// right edge&lt;/span&gt;
        &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;    &lt;span class="c1"&gt;//  here is where the data goes&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s add a constructor method below our field declarations in our TreeNode class.  This constructor will just take as a parameter the data we want to insert into the node we’re creating.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BinaryTree&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;TreeNode&lt;/span&gt; &lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TreeNode&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;TreeNode&lt;/span&gt; &lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// left edge&lt;/span&gt;
        &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;TreeNode&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// right edge&lt;/span&gt;
        &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;    &lt;span class="c1"&gt;//  here is where the data goes&lt;/span&gt;

        &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;TreeNode&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
             &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We’re ready now to create our tree.   Below the TreeNode class, still within our BinaryTree class, let’s make a createBinaryTree method and within that method, let’s create a few nodes.  We’ll create those nodes by creating instances of our TreeNode class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BinaryTree&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;TreeNode&lt;/span&gt; &lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TreeNode&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;TreeNode&lt;/span&gt; &lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// left edge&lt;/span&gt;
        &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;TreeNode&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// right edge&lt;/span&gt;
        &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;    &lt;span class="c1"&gt;//  here is where the data goes&lt;/span&gt;

        &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;TreeNode&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
             &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

   &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;createBinaryTree&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

       &lt;span class="nc"&gt;TreeNode&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;newTreeNode&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
     &lt;span class="c1"&gt;//I’m create a TreeNode and calling it “first”&lt;/span&gt;
           &lt;span class="c1"&gt;// then I’m assigning the integer “1” as its value&lt;/span&gt;

    &lt;span class="c1"&gt;// Below: create more TreeNodes:&lt;/span&gt;
     &lt;span class="nc"&gt;TreeNode&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;newTreeNode&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
     &lt;span class="nc"&gt;TreeNode&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;newTreeNode&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
     &lt;span class="nc"&gt;TreeNode&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;newTreeNode&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
     &lt;span class="nc"&gt;TreeNode&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;newTreeNode&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
     &lt;span class="nc"&gt;TreeNode&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;newTreeNode&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
     &lt;span class="nc"&gt;TreeNode&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;newTreeNode&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

   &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s start organizing these nodes we’ve just created.&lt;br&gt;
We have a field we already declared as “root”.  By looking at the diagram of our tree(scroll to the top of this article to see it) we see we want the “1” node as the root.  We also see from the diagram that the root node’s “left” edge will have the 2 node.  Root’s right edge will have the 3 node.  Let’s code that.  (For simplicity’s sake, I’ll just show the createBinaryTree method here instead of the whole class)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;createBinaryTree&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

       &lt;span class="nc"&gt;TreeNode&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;newTreeNode&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
     &lt;span class="c1"&gt;//I’m create a TreeNode and calling it “first”&lt;/span&gt;
           &lt;span class="c1"&gt;// then I’m assigning the integer “1” as its value&lt;/span&gt;

    &lt;span class="c1"&gt;// Below: create more TreeNodes:&lt;/span&gt;
     &lt;span class="nc"&gt;TreeNode&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;newTreeNode&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
     &lt;span class="nc"&gt;TreeNode&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;newTreeNode&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
     &lt;span class="nc"&gt;TreeNode&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;newTreeNode&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
     &lt;span class="nc"&gt;TreeNode&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;newTreeNode&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
     &lt;span class="nc"&gt;TreeNode&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;newTreeNode&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
     &lt;span class="nc"&gt;TreeNode&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;newTreeNode&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

     &lt;span class="c1"&gt;// we want our root to be assigned to “first”&lt;/span&gt;
     &lt;span class="n"&gt;root&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

     &lt;span class="c1"&gt;//we know root’s left edge is the 2 node:&lt;/span&gt;
     &lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;left&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;second&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
   &lt;span class="c1"&gt;// root’s right edge is the 3 node:&lt;/span&gt;
   &lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;right&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;third&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

   &lt;span class="o"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s finish it up.  We see from our original diagram that the 2 node’s (AKA second) left edge points to the  4 node.  It’s right edge points to the 5 node.&lt;/p&gt;

&lt;p&gt;It’s similar for our 3 node (AKA third): third’s left is 6.  Third’s right is 7:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;createBinaryTree&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

       &lt;span class="nc"&gt;TreeNode&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;newTreeNode&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
     &lt;span class="c1"&gt;//I’m create a TreeNode and calling it “first”&lt;/span&gt;
           &lt;span class="c1"&gt;// then I’m assigning the integer “1” as its value&lt;/span&gt;

    &lt;span class="c1"&gt;// Below: create more TreeNodes:&lt;/span&gt;
     &lt;span class="nc"&gt;TreeNode&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;newTreeNode&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
     &lt;span class="nc"&gt;TreeNode&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;newTreeNode&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
     &lt;span class="nc"&gt;TreeNode&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;newTreeNode&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
     &lt;span class="nc"&gt;TreeNode&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;newTreeNode&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
     &lt;span class="nc"&gt;TreeNode&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;newTreeNode&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
     &lt;span class="nc"&gt;TreeNode&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;newTreeNode&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;


     &lt;span class="n"&gt;root&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;


     &lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;left&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;second&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

   &lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;right&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;third&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

   &lt;span class="c1"&gt;//second’s left node is 4&lt;/span&gt;
   &lt;span class="n"&gt;second&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;left&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fourth&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

   &lt;span class="c1"&gt;//second’s right node is 5&lt;/span&gt;
   &lt;span class="n"&gt;second&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;right&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fifth&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

   &lt;span class="c1"&gt;//Last two nodes to place:&lt;/span&gt;
   &lt;span class="n"&gt;third&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;left&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sixth&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
   &lt;span class="n"&gt;third&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;right&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;seventh&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

   &lt;span class="o"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And there is your binary tree implementation in Java.   Admittedly, this is a simple implementation and there are fancier ways of doing this.  For example, you could start with an array of data and then implement the tree using that array.  However, as my dance teacher told me, “You have to learn the basic step first before you try any variations.”&lt;/p&gt;

&lt;p&gt;By the way, if you look at our original diagram one last time, what if your interviewer asks you, “Ok, but what do the nodes containing the values 4, 5, 6, 7 point to?”  Be sure to use the technical word to answer this question.  So instead of saying, “Well, they point to ‘zip’”, you can say, “They point to &lt;strong&gt;null&lt;/strong&gt;.”  &lt;/p&gt;

&lt;p&gt;Enjoy the adventure,&lt;/p&gt;

&lt;p&gt;and keep coding out your dreams!&lt;/p&gt;

&lt;p&gt;Namaste!&lt;/p&gt;

</description>
      <category>binarytrees</category>
      <category>java</category>
    </item>
    <item>
      <title>Interview Prep:  Binary and General Trees
</title>
      <dc:creator>Donny</dc:creator>
      <pubDate>Wed, 18 Nov 2020 23:08:18 +0000</pubDate>
      <link>https://dev.to/kuddleman/interview-prep-binary-and-general-trees-2mf4</link>
      <guid>https://dev.to/kuddleman/interview-prep-binary-and-general-trees-2mf4</guid>
      <description>&lt;p&gt;In your study of data structures, perhaps you’ve only gotten to the linear structures, e.g. stacks, queues, linked lists.  Think of these as one dimensional like a street-- you can only go up or down that street.  Now let’s consider a multi-dimensional data structure: trees.  A tree has one main trunk, but it can have branches.  And those branches can have their own branches, like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fmhk6x37xw2v73o2ancry.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fmhk6x37xw2v73o2ancry.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So what does a tree look like as a data structure?&lt;/p&gt;

&lt;p&gt;First, let’s remember from our linear data structures that those structures consisted of  nodes of data.  Each node pointed to the next node:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fhbw3lltk7q3ngt30chth.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fhbw3lltk7q3ngt30chth.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In trees, the difference is that each node can point to more than one node:&lt;/p&gt;

&lt;p&gt;In the above image, we have 4 nodes: A, B, C and D.&lt;/p&gt;

&lt;p&gt;The A node is called the &lt;strong&gt;root&lt;/strong&gt;, because all other nodes branch off from A.  We can even say, “A is the &lt;strong&gt;parent&lt;/strong&gt; of B, C and D.   B, C, and D are the &lt;strong&gt;children&lt;/strong&gt; of A&lt;/p&gt;

&lt;h2&gt;
  
  
  Siblings
&lt;/h2&gt;

&lt;p&gt;Let’s learn some more terminology with this tree below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F4n18ekx3kyojjo0yq3vi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F4n18ekx3kyojjo0yq3vi.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Above, we see node A is the root and B, C , D are A’s children.  However, because B, C, D are on the same level (“Same level” means you can draw a horizontal line connecting the lengths of B, C, D) these nodes are also &lt;strong&gt;siblings&lt;/strong&gt; of each other.&lt;/p&gt;

&lt;h2&gt;
  
  
  Ancestors and Descendants
&lt;/h2&gt;

&lt;p&gt;See the tree below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ffl2zu1i956ynvgeq5n6z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ffl2zu1i956ynvgeq5n6z.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the image above, we see three nodes: A, B and E circled in pink.  Just like in a family tree diagram,  E’s “parent” or &lt;strong&gt;ancestor&lt;/strong&gt; is B.  But the “family line” extends further back to A.  A is also an ancestor of E.&lt;/p&gt;

&lt;p&gt;We can also look at it in the opposite direction with the nodes circled in blue highlighting nodes D, G, H, and I.  D has two “children” or &lt;strong&gt;descendants&lt;/strong&gt;: G and H.  However, D’s “family line” extends further through H to I.  I is also a descendant of D.&lt;/p&gt;

&lt;h2&gt;
  
  
  Leaves
&lt;/h2&gt;

&lt;p&gt;Here’s another tree:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Feamafuhsqicmg0lmjckk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Feamafuhsqicmg0lmjckk.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Sometimes nodes don’t have children.  In that case, these child-less nodes are called &lt;strong&gt;leaves&lt;/strong&gt; (think of a leaf on a tree blowing around in the wind untethered by any branches beyond it).  A leaf can also be called &lt;strong&gt;external&lt;/strong&gt; (Think of the leaf blowing in the wind as a conduit to the outside world; no further branches are blocking it.&lt;/p&gt;

&lt;p&gt;If leafs can be termed external, what would we call those nodes who do have at least 1 child?  Yep, those nodes are &lt;strong&gt;internal&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Path and Path Length
&lt;/h3&gt;

&lt;p&gt;Given a tree where we select two different nodes of that tree, a &lt;strong&gt;path&lt;/strong&gt; is the set of edges from one node to the other.  The &lt;strong&gt;path length&lt;/strong&gt; is the number of edges in that path:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fewxmiixuxhu59seg6br6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fewxmiixuxhu59seg6br6.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above diagram, we want to get from the root, A, to H.   You see the path was traced out for us in blue.  Each of those arrows that point from a parent node to a child node is called an &lt;strong&gt;edge&lt;/strong&gt;.  We see it took 3 edges to get from A to H.  &lt;/p&gt;

&lt;h3&gt;
  
  
  Node Level and Node Depth
&lt;/h3&gt;

&lt;p&gt;Let’s describe a way to tell how deep down a given node is nested:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ftbhg8uvwzoqiky0oibh4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ftbhg8uvwzoqiky0oibh4.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the image above, we define the root level to be “0”.  Each succeeding level increases by 1. The terms “level” and “depth” are used interchangeably.&lt;/p&gt;

&lt;p&gt;For example, we see that Node C is at a depth (or level) of 1.  Node H is at a depth of 3.&lt;/p&gt;

&lt;p&gt;Another related concept is the height of a tree:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F9ug6emkq7u02ebqh7iks.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F9ug6emkq7u02ebqh7iks.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this example, we can determine the height of a tree by just counting levels--the trick is that we have to start with “0”.  So node A is at a height of 0; B, C, and D are at a height of 1.  E, F, and G are at a height of 2 and finally node H is at a height of 3.  Our tree’s height is then 3.&lt;/p&gt;

&lt;h3&gt;
  
  
  Degree
&lt;/h3&gt;

&lt;p&gt;Look at this example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fi1itdpl7orddwfho3l9c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fi1itdpl7orddwfho3l9c.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;First of all its height is 2.  Can you tell?&lt;/p&gt;

&lt;p&gt;Then, we look at the maximum number of possible children for every node in the tree.  We see that the maximum number is 3. &lt;/p&gt;

&lt;p&gt;We can say that our tree has a height of 2 and a degree of 3.&lt;/p&gt;

&lt;h2&gt;
  
  
  Types of Trees
&lt;/h2&gt;

&lt;p&gt;The last thing we’ll look at today are types of trees.&lt;/p&gt;

&lt;p&gt;You can have a tree where each node has any number of children from 0 to n.  The number of children each node has can be different from parent node to parent node.  In this case, we have a &lt;strong&gt;general tree&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;On the other hand, we can have a tree where every parent has at most &lt;strong&gt;&lt;em&gt;n&lt;/em&gt;&lt;/strong&gt; children.   The most popular version of this kind of tree is where n = 2.  In this case we have a &lt;strong&gt;binary tree&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Here’s an example of a binary tree:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fbz2zfaipcefzer4crns9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fbz2zfaipcefzer4crns9.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Notice how we have the root at node 1.  Then each child or descendant of node 1 has at either 0, 1 or 2 children.&lt;/p&gt;

&lt;p&gt;So there you have most of the basic terminology to be able to work with trees.  Most of our work going forward will be with binary trees--as my tutor told me, binary trees represent about 95% of the work with trees.&lt;/p&gt;

&lt;p&gt;More to come!&lt;/p&gt;

&lt;p&gt;Keep coding out your dreams!&lt;/p&gt;

&lt;p&gt;Namaste!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Interview Prep:
Bit Manipulation: Part III
</title>
      <dc:creator>Donny</dc:creator>
      <pubDate>Thu, 12 Nov 2020 04:07:48 +0000</pubDate>
      <link>https://dev.to/kuddleman/interview-prep-bit-manipulation-part-iii-leo</link>
      <guid>https://dev.to/kuddleman/interview-prep-bit-manipulation-part-iii-leo</guid>
      <description>&lt;p&gt;Welcome back to more work on Bit Manipulation in preparation for that big job interview you’re going for.&lt;/p&gt;

&lt;p&gt;We’ll be pushing forward based on what we’ve talked about in my last 2 articles on bit manipulation.  If this subject is new to you,  go ahead and read at least the first one here:&lt;/p&gt;

&lt;p&gt;Article I&lt;br&gt;
&lt;a href="https://dev.to/kuddleman/interview-prep-bitwise-manipulation-1o1"&gt;Bitwise Manipulation Part 1&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/kuddleman/interview-prep-more-on-bit-manipulation-25mm"&gt;Bitwise Manipulation Part 2&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Today, we’ll look at some more interview questions that can be solved by using bit manipulation.  But before we do that, we’ll just need to learn one more way of manipulating bits in addition to the ones we’ve already covered in my previous articles.&lt;/p&gt;
&lt;h3&gt;
  
  
  Left and Right Shift Operator in Java
&lt;/h3&gt;

&lt;p&gt;The Java API gives us a left shift operator ( &amp;lt;&amp;lt; ) as well as a right shift operator( &amp;gt;&amp;gt; )&lt;/p&gt;

&lt;p&gt;Let’s see how they work:&lt;/p&gt;

&lt;p&gt;Let’s make up a base two integer.  The letters above the number are just a way to identify each component of the integer.  Let's set our integer to the variable “a”:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;          A   B   C   D   E   F  
a =       1   0   1   0   1   1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let’s say we want to &lt;strong&gt;&lt;em&gt;right shift&lt;/em&gt;&lt;/strong&gt; this number by two places.  We would write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;               a &amp;gt;&amp;gt; 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So let’s take our “hand”, place it just to the left of column “A” and shove the whole integer over to the right by 2 places.  But remember, any number that gets shoved to the right of column “F” just disappears!&lt;/p&gt;

&lt;p&gt;So here’s what we get after right shifting by 2  &lt;/p&gt;

&lt;p&gt;( a &amp;gt; &amp;gt; 2)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; A   B   C   D   E   F  
         1   0   1   0    
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That’s all there is to right shifting.&lt;/p&gt;

&lt;p&gt;Now let’s retrieve our original number:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;          A   B   C   D   E   F 
a =       1   0   1    0  1   1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s now shift the integer contained in variable “a” 2 places in the opposite direction to the left.  We’ll write it like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;               a &amp;lt;&amp;lt; 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So again we’ll take our “hand” and place it this time just to the right of column “F” and shove the whole integer two places over to the left:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                  A   B   C   D   E   F  
          a =     1   0    1   0   1   1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But this gives us empty bits in columns “E”  and “F” as seen above.  We’ll have to fill those columns in with zeros to get our final result below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;             A   B    C   D    E   F  
     a =     1   0    1   0    1   1    0   0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  And Now for Some Cool Tricks You Can Use In Your Interview:
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Right shift ( &amp;gt; &amp;gt;) is the same as dividing by 2
&lt;/h3&gt;

&lt;p&gt;Let’s take a decimal number , 10.   Let’s convert to to base 2:&lt;br&gt;
11000&lt;/p&gt;

&lt;p&gt;We’ll right shift binary 11000 by 1 place:&lt;/p&gt;

&lt;p&gt;1100&lt;/p&gt;

&lt;p&gt;Binary 1100 is decimal 12 which is half of decimal 24.&lt;/p&gt;

&lt;p&gt;You can keep shifting one place to the right and keep on halving our number to check it out!&lt;/p&gt;

&lt;h3&gt;
  
  
  Set the 2 to the nth bit of integer X
&lt;/h3&gt;

&lt;p&gt;This is a good one.&lt;/p&gt;

&lt;p&gt;We’re given an binary integer, say it’s 1010;&lt;/p&gt;

&lt;p&gt;A   B    C    D&lt;br&gt;
1   0    1    0&lt;/p&gt;

&lt;p&gt;We’re also told our “n” is 2.  That means we have to set (put a one) in column B.  Why column B?  In short, column B is the 2 to the “n” column.  Our “n” was given as “2”.  Column B is our 2 to the 2nd column:&lt;/p&gt;

&lt;p&gt;Column D is the 2 to the 0th column or the “ones” column&lt;br&gt;
Column C is the 2 to the 1st column or the “twos” column&lt;br&gt;
Column B is the 2 to the 2nd column or the “fours” column&lt;/p&gt;

&lt;p&gt;The way we’ll set our column B is by using this simple formula:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        x | ( 1 &amp;lt;&amp;lt; n)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Let’s work it out.  First let’s start with what’s inside the parens on the right side of the bitwise “OR” operator ( | ):   1 &amp;lt;&amp;lt; n&lt;/p&gt;

&lt;p&gt;We want to shift the integer “1” to the left by n places.  We were given n as “2”.  Let’s do it:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;              1    //original integer

          1 0 0   // shift the original 
                     over to the left by 
                     2 places
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now let compare our x, 1010, to 100 using bitwise OR ( | )&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;      A   B   C  D
      1   0   1   0
      0   1   0   0
      _____________
      1   1   1   0      // 1010 | 0100
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;After applying bitwise OR, we see that in our result ( 1 1 1 0 ), in column B is now set as the problem required.&lt;/p&gt;

&lt;p&gt;These are just two of the very fun things you can do with bit manipulation.  I hope it’s whetted your appetite for more.  Using base 2 and bit manipulation allows for a fine tuning of our integers that would be clunky in base ten.   &lt;/p&gt;

&lt;p&gt;Keep on coding out your dreams!&lt;/p&gt;

&lt;p&gt;Namaste&lt;/p&gt;

</description>
      <category>java</category>
      <category>bitmanipulation</category>
    </item>
    <item>
      <title>Interview Prep:  More on Bit Manipulation</title>
      <dc:creator>Donny</dc:creator>
      <pubDate>Tue, 03 Nov 2020 01:37:33 +0000</pubDate>
      <link>https://dev.to/kuddleman/interview-prep-more-on-bit-manipulation-25mm</link>
      <guid>https://dev.to/kuddleman/interview-prep-more-on-bit-manipulation-25mm</guid>
      <description>&lt;p&gt;Welcome back again to another episode of Interview Prep.  We’ve been playing around with Bit Manipulation lately. If you didn’t catch my first article the gives the basics, give it a quick read-through now as we’ll be continuing on from where it left off:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/kuddleman/interview-prep-bitwise-manipulation-1o1"&gt;Bitwise Manipulation, Part I&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As a quick overview, remember that “bit manipulation” means we’re working in base-2 instead of base-10. The reason why we care is that working in base-2 gives us a finer level of control over our data.  Think of bit manipulation as working with a nail file instead of a hammer.&lt;/p&gt;

&lt;p&gt;So now that you know some basic operations of bit manipulation from my first article, let’s use those operations to do some cool stuff and also to help us answer some possible interview questions.&lt;/p&gt;

&lt;h3&gt;
  
  
  Clear the Least Significant Bit that is Set
&lt;/h3&gt;

&lt;p&gt;The question you might get goes like this:&lt;br&gt;&lt;br&gt;
   “Clear the least significant set bit in a given &lt;br&gt;
sequence of bits”&lt;/p&gt;

&lt;p&gt;First, let’s define the terms.  What do they mean by “significant” bit?&lt;/p&gt;

&lt;p&gt;Simple.  Look at this sequence of bits:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                                          X
1010001110000001000000010000111100000101010
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now put your finger on the screen (go ahead, I won’t tell!) on the extreme right side of the sequence(Marked with an “X”).  Now move your finger along the sequence to the left.  &lt;/p&gt;

&lt;p&gt;You are now moving your finger from the least significant bit toward the more significant bits.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;&amp;lt;---&amp;lt;&amp;lt;&amp;lt;&amp;lt;---&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;go to the left to get more significant
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                                          X
1010001110000001000000010000111100000101010
Y
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In our example, the bit marked with an “X” is the least significant and “Y” is the most significant bit.  The degree&lt;br&gt;
of “significance” increases as we move our finger along the bit sequence from right to left.&lt;/p&gt;

&lt;p&gt;Now back to our original question.  Let’s take a fresh bit sequence:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KhfMckv---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/bg79qtobn2gzr9z6uykc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KhfMckv---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/bg79qtobn2gzr9z6uykc.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We are being asked to create an algorithm that clears the least significant set bit.&lt;/p&gt;

&lt;p&gt;If we put our finger on the extreme right side of the sequence( cell E3), we move our finger to the left along the sequence and there move from least “significant” bit to more significant bit.  The first set bit we come to is the “1” in cell D1 marked in red.   This is the bit we need to clear and turn into a zero.&lt;/p&gt;

&lt;p&gt;In our algorithm, we have two basic tasks to perform: 1) find the least significant set bit and then “clear” it, meaning set it to zero.&lt;/p&gt;

&lt;p&gt;Now comes the cool part.   The easy way we can find and clear the least significant bit is to use bit manipulation.  And here’s the formula that works every time for this problem:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; x &amp;amp; ( x - 1)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s just work out the above formula!&lt;/p&gt;

&lt;p&gt;We were given “x”  :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wS9PYo6R--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/9nahfvo4uqwe2l3pvunr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wS9PYo6R--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/9nahfvo4uqwe2l3pvunr.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now let’s type in x-1 on line 5:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5sy7b40q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/zcxqm7suphnl4mhmx54q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5sy7b40q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/zcxqm7suphnl4mhmx54q.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Lastly, let’s find out what the expression  x &amp;amp; (x-1) resolves to:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--t5IMnEGI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/lac5jwao53d7jhr00czs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--t5IMnEGI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/lac5jwao53d7jhr00czs.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Just to review our “&amp;amp;” operation:  &lt;/p&gt;

&lt;p&gt;In Column A: “1” on line 3 and “1” on line 5 resolve to “1” on line 8.&lt;/p&gt;

&lt;p&gt;Column B: 0 and 0 resolve to 0&lt;/p&gt;

&lt;p&gt;Column C: 1 and 1 resolve to 1&lt;/p&gt;

&lt;p&gt;Column D: 1 and 0 resolve to 0&lt;/p&gt;

&lt;p&gt;Column E: 0 and 1 resolve to 0&lt;/p&gt;

&lt;p&gt;Remember we wanted to find and clear the least significant bit in line 3, the bit marked in red.  And that’s what we did!  In line 8, we see Cell D8 marked in blue where the cell was cleared.&lt;/p&gt;

&lt;p&gt;Our secret formula:  x &amp;amp; ( x-1)  cleared the least significant bit.&lt;/p&gt;

&lt;h3&gt;
  
  
  One More
&lt;/h3&gt;

&lt;p&gt;Let’s look at one more problem.  Given a sequence of &lt;/p&gt;

&lt;p&gt;bits(we’ll start with the same one we’ve been using)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Z1mrq5h---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/4cizjfc7a5g6kflrxlew.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Z1mrq5h---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/4cizjfc7a5g6kflrxlew.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We’re asked to write an algorithm that sets the least significant cleared bit.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qPiopyUA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/lep97eyyso29qn8ose4c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qPiopyUA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/lep97eyyso29qn8ose4c.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the image above, we’re given the sequence on line 3. &lt;br&gt;
The least significant cleared bit is in cell E3.  We want our algorithm to choose this cell and set it so that we’re left with the bit sequence on line 8.&lt;/p&gt;

&lt;p&gt;How do we do it?&lt;/p&gt;

&lt;p&gt;Simple!  We’ll use another magic bit manipulation formula.  This time, it’s:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;          x | (x + 1)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Let’s do it!&lt;/p&gt;

&lt;p&gt;We already have “x”, so let’s fill in (x + 1) on line 5 below and then let’s check to see that it resolves to our desired result on line 8:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--HYzUlHIV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/d0kkjtzt20o0prlkssmb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HYzUlHIV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/d0kkjtzt20o0prlkssmb.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We see that we successfully found the least significant cleared bit in our given sequence (Cell E3) and set it in our result in Cell E8 (in red) on line 8.  Voilà!&lt;/p&gt;

&lt;p&gt;Those are just a couple of tricks.  There are more, so I hope that whets your appetite and increases your appreciation for bit manipulation.  This way of looking at numbers allows us to make some subtle changes in our values that would otherwise not be possible.&lt;/p&gt;

&lt;p&gt;Keep coding out your dreams!&lt;/p&gt;

</description>
      <category>java</category>
      <category>bitmanipulation</category>
    </item>
    <item>
      <title>Interview Prep:  Bitwise Manipulation</title>
      <dc:creator>Donny</dc:creator>
      <pubDate>Thu, 29 Oct 2020 01:22:11 +0000</pubDate>
      <link>https://dev.to/kuddleman/interview-prep-bitwise-manipulation-1o1</link>
      <guid>https://dev.to/kuddleman/interview-prep-bitwise-manipulation-1o1</guid>
      <description>&lt;p&gt;Welcome back to interview prep.  Today we’ll tackle a new subject to help you get ready for that big technical interview: bitwise manipulation.&lt;/p&gt;

&lt;h3&gt;
  
  
  First the punchline to get you motivated:  what is it and why do I need to know this?
&lt;/h3&gt;

&lt;p&gt;In bitwise manipulation, we’re looking at numbers not from a base-10 perspective but from a base-2 perspective.  For example, the number “10” in base-10 would be expressed as “1010” in base-2.  &lt;/p&gt;

&lt;h3&gt;
  
  
  Why is this important?
&lt;/h3&gt;

&lt;p&gt;For now, I’m afraid you’ll have to take it on faith that by examining numbers in this bitwise, base-2 way, we can make subtle manipulations to those numbers which makes solving certain algorithms really easy.  If we tried to use base-10 for those certain algorithms, it would be a bloody mess!&lt;/p&gt;

&lt;h3&gt;
  
  
  Are we Onboard re: base 2 and what a bit is?
&lt;/h3&gt;

&lt;p&gt;You can skip this section if you already know about base 2 and bits, otherwise read on.&lt;/p&gt;

&lt;p&gt;Take a regular base-10 number like 156.  You know from elementary school show how to read this number since you learned about &lt;strong&gt;&lt;em&gt;place value&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--SWbZ29CA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/cbg8opbjutgg8x3ze9r8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SWbZ29CA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/cbg8opbjutgg8x3ze9r8.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let me make a slight alteration to the image above.  Let us use a more computer science-like expression for the values of the 3 columns.  Instead of “100s” we’ll call it “10 to the power of 2” or 10^2.  Instead of “10s”, it’s 10 to the power of 1: 10^1.  Lastly the “1s” column is 10 to the power of 0: 10^0 (Remember any number to the power of 0a  power calculates to “1”&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ArkQH0oC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/w7peo8hdnzg367ye9qvk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ArkQH0oC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/w7peo8hdnzg367ye9qvk.png" alt="Alt Text"&gt;&lt;/a&gt;  &lt;/p&gt;

&lt;p&gt;I bet you got it.  So here’s a quick check.  Can you figure out what “10101” in base-2 works out to be in base-10?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6hJHuDvG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/tr0cmmlxwjwoofxv7iqi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6hJHuDvG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/tr0cmmlxwjwoofxv7iqi.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Right! It’s 21.   Starting from the right of the number, we have 1*2^4  +  0* 2^3  +  1* 2^2  +  0* 2^1 +  1 * 2^0 = 21&lt;/p&gt;

&lt;h3&gt;
  
  
  Let’s Learn Some Terminology
&lt;/h3&gt;

&lt;p&gt;Now that we know what base 2 is, we can start using more fancy computer science terms.  &lt;/p&gt;

&lt;p&gt;Let’s just look at at base two number:  11100&lt;/p&gt;

&lt;p&gt;Think of each number, the 1 or the 0, occupying a separate cell in a spreadsheet. In the illustration below, I’ve given each column a letter to identify it:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JQ0RbvJF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/o7da7zptneh02wg7fgtd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JQ0RbvJF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/o7da7zptneh02wg7fgtd.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The fancy word for each cell of the spreadsheet is: &lt;strong&gt;byte&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In each cell or byte, the only two numbers we can put in them is 1 or 0.  &lt;/p&gt;

&lt;p&gt;If we put a 1 in the cell, that cell or byte is said to be &lt;strong&gt;set&lt;/strong&gt; or &lt;strong&gt;on&lt;/strong&gt;.  &lt;/p&gt;

&lt;p&gt;In contrast, if there is a 0 stored&lt;br&gt;
in the byte, that byte is said to be &lt;strong&gt;clear&lt;/strong&gt; or &lt;strong&gt;off&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;That means in our example above, we see that columns A, B and C contain bytes that are “set” or “on”, while columns D and E contain bytes that are “clear” or “off”.*&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Fun aside: the bytes are represented to us humans as the integer 1 or the integer 0.  However, the computer does not “see” these symbols.  Just recognizes intensity of light.  This means the byte containing the “1” is seen by the computer as a brighter light.  The “0” is seen by the computer as a dimmer light.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Let’s Start Manipulating Those Bits!
&lt;/h3&gt;

&lt;p&gt;So finally we know enough to start making some bit-wise waves!&lt;/p&gt;

&lt;p&gt;Let’s learn some basic operations of bits:&lt;/p&gt;

&lt;p&gt;Bitwise NOT ( ~ )&lt;/p&gt;

&lt;p&gt;Bitwise AND ( &amp;amp; )&lt;/p&gt;

&lt;p&gt;Bitwise OR ( | )&lt;/p&gt;

&lt;p&gt;Bitwise XOR ( ^ )&lt;/p&gt;

&lt;p&gt;Let’s take them one at a time:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Bitwise NOT&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Bitwise NOT ( ~ )  is easy.  If the bit is set (1) then bitwise NOT, the tilde sign ( ~ ) clears it (turns it into 0).  &lt;/p&gt;

&lt;p&gt;If the bit is clear (0) the tilde sign ( ~ ) will clear it.&lt;/p&gt;

&lt;p&gt;So given the base two number 11100.  If we apply bitwise NOT we’ll get 00011:&lt;/p&gt;

&lt;p&gt;~(11100) = 00011&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Bitwise AND&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Do not confuse the regular AND symbol that you already know (&amp;amp;&amp;amp;) with BITWISE AND (&amp;amp;).  Bitwise AND is represented by only one ampersand in the Java language.  We use that bitwise AND when we’re working on the level of bits only.&lt;/p&gt;

&lt;p&gt;Think of bitwise AND like multiplication.  When we take two base-two numbers OF EQUAL LENGTH and apply bitwise AND we’ll find the result by multiplying the column of one number with the corresponding column of the second number:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--SnZNWXdq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/53fg6pm2yrehduyvop6h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SnZNWXdq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/53fg6pm2yrehduyvop6h.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Starting in column A. Multiply 1 in column 3 by 1 right below it in cell A5 and we get 1 in cell A7 as the result.&lt;/p&gt;

&lt;p&gt;Column B: 1 x 0 gives us zero&lt;br&gt;
Column C: 1 x 1 gives us 1&lt;br&gt;
Column D:  0 x 1 gives us zero&lt;br&gt;
Column E: 0 x 0 gives us zero &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Bitwise OR ( | )&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Again, notice bitwise OR is represented by only ONE pipe symbol.  It is  different from the normal OR ( ||) with two pipe symbols that you’re used to using in the Java language.&lt;/p&gt;

&lt;p&gt;You can think of bitwise OR as like addition.  You’ll be adding each column individually:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--uk3GkHjb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/nkj2vau0k16mblxs4r1p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--uk3GkHjb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/nkj2vau0k16mblxs4r1p.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This time in Col A  we add 1 and 1 and get technically 10 in base two, but we’ll chop of the zero and call it one.&lt;/p&gt;

&lt;p&gt;In Col B: 0 + 1 gives us 1&lt;/p&gt;

&lt;p&gt;In Col C: 1 + 1 gives us 1&lt;/p&gt;

&lt;p&gt;In Col D: 1 + 1 gives us 1&lt;/p&gt;

&lt;p&gt;In Col E 0 + 0 gives us 0&lt;/p&gt;

&lt;p&gt;And now comes my favorite:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Bitwise XOR ( ^ )&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;XOR is pronounced EX-OR  and also as ZOR (As in Zoro, the famous swordsman!)&lt;br&gt;
For this one, all you have to remember is that in each column, if one number contains a “1”, the other number must contain a “0” in order to result in a “1”.  If both bytes are set, or if both bytes are clear, then we’ll get “0”&lt;/p&gt;

&lt;p&gt;Let’s see it in action:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QIxCdP1---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/is5lobnerg1a73oqvtpy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QIxCdP1---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/is5lobnerg1a73oqvtpy.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In column A,  1 and 1 return 0&lt;/p&gt;

&lt;p&gt;Col B: 1 and 0 return 1&lt;/p&gt;

&lt;p&gt;Col C: 1 and 1 return 0&lt;/p&gt;

&lt;p&gt;Col D: 1 and 0 return 1&lt;/p&gt;

&lt;p&gt;Col E: 0 and 0 return 0&lt;/p&gt;

&lt;p&gt;Now that you know some of the basic bit manipulations, we can begin to solve some bit manipulation algorithms which is where the beauty of bits come into play.  We’ll be able to “fine tune” and make subtle distinctions between integers using base 2 and bit manipulation as opposed to using base 10 to try to accomplish the same thing.  Think of the difference in your code the same as the difference between rose pedals (base 2) vs. a mess (base 10)!&lt;/p&gt;

&lt;p&gt;See you next time for more!&lt;/p&gt;

&lt;p&gt;And in the meantime:&lt;/p&gt;

&lt;p&gt;Keep coding out your dreams!&lt;/p&gt;

&lt;p&gt;Namaste!&lt;/p&gt;

</description>
      <category>java</category>
      <category>bits</category>
    </item>
    <item>
      <title>Interview Prep:  Remove the Nth Node From the End Of A Singly Linked List
</title>
      <dc:creator>Donny</dc:creator>
      <pubDate>Thu, 22 Oct 2020 01:24:38 +0000</pubDate>
      <link>https://dev.to/kuddleman/interview-prep-remove-the-nth-node-from-the-end-of-a-singly-linked-list-1bne</link>
      <guid>https://dev.to/kuddleman/interview-prep-remove-the-nth-node-from-the-end-of-a-singly-linked-list-1bne</guid>
      <description>&lt;p&gt;Welcome back to interview prep.  In this series, we are examining common technical interview questions in the realm of data structures and algorithms.&lt;/p&gt;

&lt;p&gt;If you’ve never heard of singly linked lists before, then you should read my basic article on linked lists first.  Otherwise, let’s push on!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/kuddleman/technical-interview-prep-singly-linked-list-cheat-sheet-in-javascript-1jj6"&gt;Linked Lists Part I&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/kuddleman/interview-prep-singly-linked-lists-part-2-20nj"&gt;Linked Lists part II&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So here’s our problem for today:  Given a singly linked list, remove the nth node from the end of the list.&lt;/p&gt;

&lt;p&gt;Let’s understand the question.&lt;/p&gt;

&lt;p&gt;We’re given the linked list below as well as the integer “4”&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fglkvv9bahvoe6cv9vij1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fglkvv9bahvoe6cv9vij1.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As we can see above, our linked list consists of node containing integers from 0 to 9.  The head node (H) is at 0 and the tail node (T) is at 9.&lt;/p&gt;

&lt;p&gt;Now let’s remove the nth node from the list.  We were given n = 4, so we’ll remove the 4th node from the end.&lt;br&gt;
If we count the nodes backwards beginning from the tail node, or “9”, the 4th node from the end is “6”.  Let’s remove it. Now our node will look like the list in blue below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fxd42a72zw50k1khm2s5z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fxd42a72zw50k1khm2s5z.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;How Do We Do This?&lt;/p&gt;

&lt;p&gt;First, let’s understand conceptually how we approach this question.&lt;/p&gt;

&lt;p&gt;Our first problem is finding the 4th node from the end of the list.  In our code, we cannot traverse a singly linked list backwards.  The only way we may traverse our list is starting at the head and moving in one direction until we reach “null” after the tail.&lt;/p&gt;

&lt;p&gt;Think of a singly linked list as a &lt;strong&gt;one-way street&lt;/strong&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  But not to worry, we have a plan!
&lt;/h3&gt;

&lt;p&gt;First, let’s set two pointers at the head of our list.  We’ll call those two pointers “first” (F) and “second” (S)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fql2qym0ewxvs31tr1l3x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fql2qym0ewxvs31tr1l3x.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now let’s advance our “second” pointer “n” number of places.  Our “n” is 4, so let’s advance “S” by 4 places:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ft1ktky1vjn53pl5sddnt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ft1ktky1vjn53pl5sddnt.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So now our pointers are 4 places apart from each other.&lt;br&gt;
Next step is to start advancing each pointer by 1.  Let’s do this together in our heads:&lt;/p&gt;

&lt;p&gt;Advance S to 5; advance F to 1&lt;br&gt;
Advance S to 6; advance F to 2&lt;br&gt;
Advance S to 7; advance F to 3&lt;/p&gt;

&lt;p&gt;and so on….&lt;/p&gt;

&lt;p&gt;We’ll have to stop advancing pointers when S gets to null.  At that moment, our points will look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F4jwgwitr2g65k1ju956w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F4jwgwitr2g65k1ju956w.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Look at that!  Our “S” pointer ended at “null” while our&lt;br&gt;
“F” pointer ended at “6”.   We notice that “6” is the 4th node from the end of the list--exactly the node we needed to find!&lt;/p&gt;

&lt;p&gt;Now that we’ve found the node we need to remove, we’ll get rid of it by resetting  the node before it, “5”, to point at “7”.&lt;/p&gt;
&lt;h3&gt;
  
  
  Let’s Code It Up!
&lt;/h3&gt;

&lt;p&gt;Now you have a conceptual understanding of how we’ll solve this algorithm.  Let’s code it up!&lt;/p&gt;

&lt;p&gt;Remember, the only things we can “see” of a linked list are the head, and tail.  Also, we can only traverse the linked list starting at the head and moving towards the tail.&lt;/p&gt;

&lt;p&gt;In our function, removeNthNodeFromEnd, we’ll use “head” and “n” as parameters.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;removeNthNodeFromEnd&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;


&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let’s set our first pointer, variable “first”, and our&lt;br&gt;
second pointer, variable “second”, to “head”.&lt;/p&gt;

&lt;p&gt;We’ll also need a counter variable( set counter to “1”) to keep track of the number of places we traverse in the list:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;removeNthNodeFromEnd&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;head&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;second&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;head&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To get the “second” pointer to traverse 4 places on our list, we’ll use a “while” loop&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;removeNthNodeFromEnd&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;head&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;second&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;head&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;

  &lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nx"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="nx"&gt;second&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;second&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;
     &lt;span class="nx"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We’re getting there!  We now have “second” set four places ahead of “first”.&lt;/p&gt;

&lt;p&gt;Next step is to start both pointers traversing the list--each moving one node at a time in step with each other.  When “second” finally gets to the end of the list and gets to “null”, we want to stop the traversing of “first”.&lt;/p&gt;

&lt;p&gt;But wait!  We have a little edge case to deal with.  What if, after advancing “second” by “n” places,  “second” points to “null”?  It would look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fswjybn6vknniwctttuxx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fswjybn6vknniwctttuxx.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We see that “S” is at “null” and the node we need to remove at “F” is actually the &lt;strong&gt;head node&lt;/strong&gt;.  We cannot just remove the head node like we could any middle node.  If we remove the head node, we must reset the head node to the next node.  In our example, the new head node would be “1”.  Let’s take care of that edge case:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;removeNthNodeFromEnd&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;head&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;second&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;head&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;

  &lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nx"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="nx"&gt;second&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;second&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;
     &lt;span class="nx"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;

   &lt;span class="c1"&gt;//edge case if second points to “null”:&lt;/span&gt;
 &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nx"&gt;second&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="c1"&gt;// update value of the head node &lt;/span&gt;
 &lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;

 &lt;span class="c1"&gt;//update the pointer of the head node:&lt;/span&gt;
 &lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;

&lt;span class="c1"&gt;// and we’re done.  Let’s just exit the function&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;head&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;



&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that the edge case is out of the way, let’s have each pointer traverse the list.  However, &lt;strong&gt;we want to stop the traversal when “second” gets to the last node before “null”&lt;/strong&gt;.&lt;br&gt;
That means “first” will land on the node &lt;strong&gt;before the one we really want to eliminate&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Our pointers will look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F578lr6jukj5awliav62u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F578lr6jukj5awliav62u.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Why do we do this?  Well, think of the links between the nodes as little tied knots in a string.  If we really traversed to the “6”, the one we want to eliminate, and then “untied” its knot to “7” we would have lost the reference to the “7”.  Think of the “7” then being unlinked to the rest of the list, it would just “float” away.&lt;br&gt;
&lt;strong&gt;The way we need to get rid of the “6” is via it’s immediate  previous neighbor -- the “5”&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;What we’ll do now that “first” is pointing at “5” is we’ll “retie” the 5’s “next” knot to 7.  Visualize this.  You’ll see how nothing gets untied in the process.    Once we “tie” the 5 to the 7, now we can safely untie the 6 from the 7.  The six can then just float off into computer infinity.&lt;/p&gt;

&lt;p&gt;Let’s do it.  I’ll write the code to advance both pointers so long as “second” IS NOT null:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;removeNthNodeFromEnd&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;head&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;second&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;head&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;

  &lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nx"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="nx"&gt;second&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;second&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;
     &lt;span class="nx"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;

   &lt;span class="c1"&gt;//edge case if second points to “null”:&lt;/span&gt;
 &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nx"&gt;second&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="c1"&gt;// update value of the head node &lt;/span&gt;
 &lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;

 &lt;span class="c1"&gt;//update the pointer of the head node:&lt;/span&gt;
 &lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;

&lt;span class="c1"&gt;// and we’re done.  Let’s just exit the function&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;head&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;

       &lt;span class="c1"&gt;// now we advance each pointer. Let’s&lt;/span&gt;
       &lt;span class="c1"&gt;// keep going so long as second IS NOT null&lt;/span&gt;
       &lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nx"&gt;second&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

           &lt;span class="nx"&gt;second&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;second&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;
           &lt;span class="nx"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;first&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="nx"&gt;next&lt;/span&gt;
       &lt;span class="p"&gt;}&lt;/span&gt;


&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We’re down to our last line of code now!&lt;/p&gt;

&lt;p&gt;We just have to do that “retying” explained above.  So we got our first pointer at 5, the node before the 6--the node we want to get rid of.  We know we just have to “retie” the 5 to the node after 6, or 7.  &lt;/p&gt;

&lt;p&gt;How do we “re-tie” our 5 to the 7?&lt;/p&gt;

&lt;p&gt;We just do:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;      &lt;span class="nx"&gt;first&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;first&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On the right side of the expression, “first” is set to “5”.  That means first.next would be “6” and first.next.next is “7”.   I’m saying, “Set 7 to be the next node after “first” or  “5”.&lt;/p&gt;

&lt;p&gt;See the final code below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;removeNthNodeFromEnd&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;head&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;second&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;head&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;

  &lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nx"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="nx"&gt;second&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;second&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;
     &lt;span class="nx"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;

   &lt;span class="c1"&gt;//edge case if second points to “null”:&lt;/span&gt;
 &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nx"&gt;second&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="c1"&gt;// update value of the head node &lt;/span&gt;
 &lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;

 &lt;span class="c1"&gt;//update the pointer of the head node:&lt;/span&gt;
 &lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;

&lt;span class="c1"&gt;// and we’re done.  Let’s just exit the function&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;head&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;

       &lt;span class="c1"&gt;// now we advance each pointer. Let’s&lt;/span&gt;
       &lt;span class="c1"&gt;// keep going so long as second IS NOT null&lt;/span&gt;
       &lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nx"&gt;second&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

           &lt;span class="nx"&gt;second&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;second&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;
           &lt;span class="nx"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;first&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="nx"&gt;next&lt;/span&gt;
       &lt;span class="p"&gt;}&lt;/span&gt;

      &lt;span class="nx"&gt;first&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;first&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;

     &lt;span class="c1"&gt;// does the interviewer want us to return something?&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I would ask the interviewer what, if anything, they want us to return.  Maybe the head?  Maybe “n”?  Maybe just the string “I did it! Yay!”&lt;/p&gt;

&lt;h3&gt;
  
  
  Space and Time Complexity
&lt;/h3&gt;

&lt;p&gt;We are just traversing a list one time.  There is no nested looping, so we have O(n) time complexity&lt;/p&gt;

&lt;p&gt;We are not creating any new data structures with our algorithm.  All our operations are done in place on the one list so our space complexity is a cool O(1)&lt;/p&gt;

&lt;p&gt;And there you have it.  A fun, relatively easy algorithm to remove a node “n” places from the end of a singly linked list.&lt;/p&gt;

&lt;p&gt;Happy coding and best wishes with your interviews!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>linkedlists</category>
    </item>
    <item>
      <title>What About These Fat Arrow Functions?</title>
      <dc:creator>Donny</dc:creator>
      <pubDate>Fri, 16 Oct 2020 00:48:13 +0000</pubDate>
      <link>https://dev.to/kuddleman/what-about-these-fat-arrow-functions-2p9i</link>
      <guid>https://dev.to/kuddleman/what-about-these-fat-arrow-functions-2p9i</guid>
      <description>&lt;p&gt;When I first started learning JavaScript a couple years ago, I was taught to write the classic arrow expressions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;myMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="nx"&gt;Hello&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;World&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then I started using the new-fangled &lt;strong&gt;&lt;em&gt;arrow function&lt;/em&gt;&lt;/strong&gt; ES2015 way of&lt;br&gt;
writing the same thing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;myMessage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="nx"&gt;Hello&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;World&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The new syntax seems cool to me, but then I found out there were some important differences between the new and the old syntax. Sometimes those differences made arrow functions really amazing and other times, they caused unforeseen problems.&lt;/p&gt;

&lt;p&gt;First, let’s go over some basics:&lt;/p&gt;

&lt;h3&gt;
  
  
  Omitting Parentheses.
&lt;/h3&gt;

&lt;p&gt;Normally, we use parentheses to define the parameters our arrow function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;addTwoNumbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Above, we’ve put parentheses around our two parameters, x and y.&lt;/p&gt;

&lt;p&gt;We also must use parentheses if we have zero parameters:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;mySecretMessage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="nx"&gt;This&lt;/span&gt; &lt;span class="nx"&gt;is&lt;/span&gt; &lt;span class="nx"&gt;fun&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, if we have just one parameter, we can optionally omit the parenthesis:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sayMyName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// we could have eliminated the parenthesis since there&lt;/span&gt;
&lt;span class="c1"&gt;// is only one parameter in our arrow function. &lt;/span&gt;

&lt;span class="c1"&gt;// Let’s rewrite it:&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sayMyName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To summarize:  you can only omit parentheses around the parameters of an arrow function if you have one parameter.  If you have zero, two, three or more parameters, you must use parentheses.&lt;/p&gt;

&lt;h3&gt;
  
  
  Omit the Curly Braces
&lt;/h3&gt;

&lt;p&gt;When we have just one statement in our function body, we can simplify the statement by vomiting the curly braces:&lt;/p&gt;

&lt;p&gt;This function with only one statement in the function body:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="c1"&gt;//Example A&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;addTwoNumbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Becomes this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//Example B&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;addTwoNumbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Cool!  In the re-written statement, we 1) removed the curly braces, 2) removed the keyword “return” and 3) put the function body statement on the same line as the function definition.&lt;br&gt;
Note the use of a couple of new vocabulary words.  in Example A above, when we use the return keyword it is known as an &lt;strong&gt;explicit return&lt;/strong&gt;.  In contract, when we omit the return keyword as in Example B, it is called an &lt;strong&gt;implicit return&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;***An Outlier Occassion Where Parentheses Are A Must:&lt;/p&gt;

&lt;p&gt;If you’re going to return an object literal, then you must wrap that object in parentheses:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;greetings&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`Hello, &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;!`&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="c1"&gt;//now call “greetings”&lt;/span&gt;

&lt;span class="nx"&gt;greetings&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;Timmy&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    &lt;span class="c1"&gt;// -&amp;gt; { message: Hello, Timmy! }&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we don’t wrap the object literal in parentheses, then JavaScript will confuse the curly braces with those that define the function body.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Famous “This”
&lt;/h3&gt;

&lt;p&gt;The “this” keyword is a well-known bugaboo for many a JavaScript programmer.  And to make it more fun, the “this” keyword acts differently in a classic function call vs. an arrow function call.&lt;/p&gt;

&lt;p&gt;Let’s look at how “this” works in a method of an object.&lt;/p&gt;

&lt;p&gt;See the method below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;car&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;model&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Fiesta&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;manufacturer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Ford&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;fullName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;manufacturer&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;model&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Above you see the object “car”.  Look at the key “fullName”.  The value corresponding to “fullName” is a classic anonymous function.&lt;/p&gt;

&lt;p&gt;In this case, when we call the fullName function like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;car&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fullName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;because we’re using a classic function,  JavaScript knows to look for the meaning of “this” in the object it’s being called in.  In our case, the “this” is being called in the object named “car”.  Good, now JS will know how to parse the literals “this.manufacturer” and “this.model”.  We just said the “this” must refer to the “car” object, so we we have “manufacturer” and a “model” property in our “car” object?  Yes, we do!  So JS can return:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;car&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fullName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;// returns: “Ford Fiesta”&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let see what would happen if we turn our car.fullName method into an arrow function like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;car&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;model&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Fiesta&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;manufacturer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Ford&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;fullName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;manufacturer&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;model&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What happens now when we try to call “car.fullName( )” ?&lt;br&gt;
The difference lies in the fact how the arrow function will interpret the “this” keyword.  We already saw how the classic function knew that “this” referred to the object itself and therefore all the object’s key/value pairs were made available to that function.&lt;/p&gt;

&lt;p&gt;However, our arrow function interprets the “this” keyword differently.&lt;/p&gt;

&lt;p&gt;Our arrow function will only look for a meaning of “this” in its &lt;strong&gt;lexical scope&lt;/strong&gt; meaning the context where that function is executed.&lt;/p&gt;

&lt;p&gt;In other words, this is the only thing our arrow function “sees”:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;fullName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;manufacturer&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;model&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our arrow function only sees the parts of the object that directly concern its own execution.  It does not see the “model” property nor does it see the “manufacturer” property.&lt;/p&gt;

&lt;p&gt;Therefore, when our fat arrow method function tries to interpret “this.manufacturer” and “this.model” it finds no references to anything like that.  Both values will be returned as undefined.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;car&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fullName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;  &lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;// “undefined undefined”&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The upshot of all “this” is: when constructing a method in an object, you have to use the classic function keyword.&lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>Interview Prep: Reverse Linked List Algorithm</title>
      <dc:creator>Donny</dc:creator>
      <pubDate>Thu, 08 Oct 2020 01:11:58 +0000</pubDate>
      <link>https://dev.to/kuddleman/interview-prep-reverse-linked-list-algorithm-9ag</link>
      <guid>https://dev.to/kuddleman/interview-prep-reverse-linked-list-algorithm-9ag</guid>
      <description>&lt;p&gt;Welcome back to Interview Prep.  Today we’ll look at a very popular interview question regarding linked lists.&lt;/p&gt;

&lt;p&gt;If you already know the basics of linked lists, read on.  If not, try starting with my “linked list basics” articles:&lt;/p&gt;

&lt;p&gt;LINKED LISTS ARTICLES BASICS:&lt;br&gt;
&lt;a href="https://dev.to/kuddleman/technical-interview-prep-singly-linked-list-cheat-sheet-in-javascript-1jj6"&gt;Linked Lists Basics Part I&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/kuddleman/interview-prep-singly-linked-lists-part-2-20nj"&gt;Linked Lists Basics Part II&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  What’s the Question?
&lt;/h3&gt;

&lt;p&gt;We’re given a linked linked list liked this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yEtcprUx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/voajy1prb8syqnisnknu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yEtcprUx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/voajy1prb8syqnisnknu.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is a linked list with 5 nodes.  Each node contains an integer 1-5.  In this list, 1 points to 2.  2 points to 3.  3 points to 4, 4 points to 5.   1 is the “head” of the list and 5 is the “tail” of the list&lt;/p&gt;

&lt;p&gt;Now, we want to reverse the links of the list so that it looks like one in green below&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9ebICg8g--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/u7oynse1q7bksfkrz7up.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9ebICg8g--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/u7oynse1q7bksfkrz7up.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You see that in the picture above, our green linked list has been reversed.  5 is now the head and points to 4.  4 now points to 3 and so on.  0 is now the tail.&lt;/p&gt;

&lt;p&gt;How to do it?&lt;/p&gt;

&lt;p&gt;Well, you might guess that to reverse our original list it will involve each node’s “next” property.  Yes, that’s right.  The tricky part is that when we reverse a node’s “next” property and have it point to its previous node, we’ll lose reference to the former next node.   Here’s what I mean:&lt;/p&gt;

&lt;p&gt;Let’s just take another linked list.  This time with just 3 nodes: 1, 2 and 3.  We want to take 2’s “next” property and point it at “1”.  Below I’ve circled in pink the arrow representing the “next” property we want to reverse &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kk1eNL8i--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/1776t181ctmrod449tk6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kk1eNL8i--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/1776t181ctmrod449tk6.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the second line of the image above, I’ve 2’s “next” property to the previous member: “1”.  However, as you can see, now there is no arrow pointing to “3”.  “3” is now lost to our linked list.&lt;/p&gt;

&lt;p&gt;So how do we avoid losing references when reversing a linked list?&lt;/p&gt;

&lt;p&gt;Use Pointers&lt;/p&gt;

&lt;p&gt;To illustrate what we’ll be doing, I’m going to use a picture and start in the middle of the list.  It will be easier to visualize that way.  (Yes, when we get to  the real code we’ll start with the head of the list.  Also for now, don’t worry about edge cases.)&lt;/p&gt;

&lt;p&gt;Let’s go back to our original white linked list with 5 nodes:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wPPDWZ3C--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/h5w4u4z1v618l6f0y516.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wPPDWZ3C--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/h5w4u4z1v618l6f0y516.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You’ll notice I’ve added pointers in blue.  P2, currently at node with the value of 3 is the main event!.  We want to reverse its “next” property(represented as an arrow in our diagram).  In order not to lose any references while we manipulate the “next” properties, we’ll set one more pointer: P1, which is currently at the node with the value of 2.&lt;/p&gt;

&lt;p&gt;There are just 4 steps to solve this problem:&lt;/p&gt;

&lt;p&gt;We still just one more pointer, a “P3” to point to the node after our “P2 node”.&lt;/p&gt;

&lt;p&gt;We’ll set p3 to p2.next:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RlnmSAvE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/fob9bcr6vvzdcl1inrdf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RlnmSAvE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/fob9bcr6vvzdcl1inrdf.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Set P2. next to its predecessor, “1”&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hI753WXI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/iqml1yqoaofzko4lq301.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hI753WXI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/iqml1yqoaofzko4lq301.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Above, you’ll see in pink that I’ve reversed P2’s “next” property .  P2 now points to P1, like we wanted.&lt;/p&gt;

&lt;p&gt;So now what?  How do we keep traversing the linked list?&lt;/p&gt;

&lt;p&gt;We’ll have to keep moving the pointers.  In fact there are just two more steps to go to finish the main logic!&lt;/p&gt;

&lt;p&gt;Set P1 to P2:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--oJ0_XDog--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/sz1b3rfzaws23jw5iznk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--oJ0_XDog--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/sz1b3rfzaws23jw5iznk.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Above, you’ll see I’ve shifted P1 to its new location&lt;/p&gt;

&lt;p&gt;Last step: now set P2 to P3:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--q_eo5e_e--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/hkokac4xmhfap7k8cdyb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--q_eo5e_e--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/hkokac4xmhfap7k8cdyb.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There you have one iteration of our traversal of our linked list.&lt;/p&gt;

&lt;p&gt;Before we go to the code, however, I want to show you how P3 gets shifted:&lt;/p&gt;

&lt;p&gt;We just did one complete iteration in steps 1-4 above.  We’re now ready to start our second iteration.  Let’s go back to just step one.  In step one, we set P3 to P2.next like so:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--c-WnaRzo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/7mzc139i3ytmu42qvfh1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--c-WnaRzo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/7mzc139i3ytmu42qvfh1.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You’ll remember we had already shifted P2 to P3’s position in step #4 above. Therefore, we can set a new P3 to the shifted P2’s next property.&lt;/p&gt;

&lt;p&gt;Now to the Code&lt;/p&gt;

&lt;p&gt;Just a reminder before we start coding.  &lt;/p&gt;

&lt;p&gt;That P2 pointer is our “star” pointer and our code will be constructed to accommodate it.&lt;/p&gt;

&lt;p&gt;2)In our original linked list with 5 nodes, do you know what comes before “1” and after “5”.  Yes, that right, NOTHING or “null”:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5---crCB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/49vxd9au7cf0gg1cs0h8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5---crCB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/49vxd9au7cf0gg1cs0h8.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Also, since we’re not always sure how long our linked list will be, let’s use a “while” loop. We’ll keep looping until our “star” pointer, P2, runs off the list and gets to “null”&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A minor point is, “what should this list return?”  This is a good question to ask your interviewer.  Maybe they don’t want anything returned! For now, let’s just return P1 ‘cause we can!&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Ok, let’s code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Declare our function and pass it our linked list’s head // &lt;/span&gt;
&lt;span class="c1"&gt;// node&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;reverseLinkedList&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;head&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// our p2 is the star pointer.  Let’s &lt;/span&gt;
&lt;span class="kd"&gt;set&lt;/span&gt; &lt;span class="nx"&gt;it&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;head&lt;/span&gt;
     &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;p2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;head&lt;/span&gt;
    &lt;span class="c1"&gt;// p1 comes BEFORE P2.  But if p2 is the head,&lt;/span&gt;
   &lt;span class="c1"&gt;//  what can come before the head?  Must be “null”&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;p1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;

  &lt;span class="c1"&gt;// Here’s our while loop.  We’ll keep looping &lt;/span&gt;
 &lt;span class="c1"&gt;// so long as P2, our star, doesn’t fall off the linked list&lt;/span&gt;
&lt;span class="c1"&gt;// and get to “null”&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nx"&gt;p2&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;p3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;p2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;   &lt;span class="c1"&gt;//step 1&lt;/span&gt;
        &lt;span class="nx"&gt;p2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;p1&lt;/span&gt;       &lt;span class="c1"&gt;//step 2&lt;/span&gt;
        &lt;span class="nx"&gt;p1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;p2&lt;/span&gt;              &lt;span class="c1"&gt;//step 3&lt;/span&gt;
        &lt;span class="nx"&gt;p2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;p3&lt;/span&gt;              &lt;span class="c1"&gt;//step 4&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;p1&lt;/span&gt;          &lt;span class="c1"&gt;//This imaginary interviewer wanted&lt;/span&gt;
                               &lt;span class="c1"&gt;// me to return P1.  Go figure!&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I should start adding a discussion of space and time complexity.   &lt;/p&gt;

&lt;p&gt;In this algorithm we have a time complexity of O(n) since we’re just traversing the list one time.&lt;/p&gt;

&lt;p&gt;Space complexity is a cool O(1) since we’re doing all our operations in place.  We’re not creating a new linked list or other object, for example, which would have taken up more space in memory.&lt;/p&gt;

&lt;p&gt;And there you have a solution to a popular linked list interview question.  Now you can knock ‘em dead!&lt;/p&gt;

&lt;p&gt;Have fun and&lt;br&gt;
Keep coding out your dreams!&lt;/p&gt;

&lt;p&gt;Namaste!&lt;/p&gt;

&lt;p&gt;Donny&lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>Interview Prep: Stacks: Part II</title>
      <dc:creator>Donny</dc:creator>
      <pubDate>Wed, 30 Sep 2020 22:46:32 +0000</pubDate>
      <link>https://dev.to/kuddleman/interview-prep-stacks-part-ii-1k8e</link>
      <guid>https://dev.to/kuddleman/interview-prep-stacks-part-ii-1k8e</guid>
      <description>&lt;p&gt;Welcome (back) to interview prep.  Over these past 3 weeks, we did a quick run-through of the first basic linear data structure which we’ll need knowledge of for technical interviews: linked lists.  Here are the links to my two posts:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/kuddleman/technical-interview-prep-singly-linked-list-cheat-sheet-in-javascript-1jj6"&gt;Linked List Part I&lt;/a&gt;&lt;br&gt;
&lt;a href="https://dev.to/kuddleman/interview-prep-singly-linked-lists-part-2-20nj"&gt;Linked List Part II&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we’re on the 2nd basic linear data structure: stacks.  If you have not already read Part I of stacks or don’t have previous knowledge of the subject, you can read about the basics of stacks in my post here:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/kuddleman/interview-prep-data-structures-stacks-d8c"&gt;Stacks Part I&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this post, now that we know the basics of stack, let’s look at a common algorithm problem involving stacks: Balance the Curly Braces, Brackets and Parens.&lt;/p&gt;

&lt;p&gt;This question goes like this:&lt;/p&gt;

&lt;p&gt;Given a string consisting of opening and closing curly braces, brackets and parens, determine if they “balance”, that is: does each opening brace, bracket and paren have a “mate” that closes it? If the symbols “balance” return the boolean “true”, if not, return boolean “false.”&lt;/p&gt;

&lt;p&gt;Here’s a string of symbols I’ve drawn.  Notice how each opening symbol has a “mate.” The algorithm testing this string would return the boolean “true.”  So you can see it better, I’ve shown matching pairs by color:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F57h6y340qrl88qte139r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F57h6y340qrl88qte139r.png" alt="stacks"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;By contrast, here’s a string of symbols that does NOT balanced:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F2xrfatwubcwyk96cl6t1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F2xrfatwubcwyk96cl6t1.png" alt="stacks 2"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note how the last coral-colored closing paren on the right end of the string does not have a “mate” and will therefore be evaluated as boolean “false.” &lt;/p&gt;

&lt;p&gt;So now let’s design an algorithm in JavaScript to solve this problem.&lt;/p&gt;

&lt;h3&gt;
  
  
  Let’s draw it out first:
&lt;/h3&gt;

&lt;p&gt;We’ll use a stack data structure, of course.  Let’s iterate through the string.  When we come upon an opening symbol we’ll simply add it to our stack.  Let’s take our balanced stack from the first illustration above.  The first five symbols in the string are &lt;strong&gt;opening symbols&lt;/strong&gt;, so we’ll just add them to our stack one at a time like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fr9dknn2dk55l7jy0w12r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fr9dknn2dk55l7jy0w12r.png" alt="stacks-3"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now on our next iteration of our original string, we come to a closing bracket at position 6.  We have to look (peek) at the top of the stack and see if we find its mate--and sure enough we do!  That means we can “pop” that opening bracket on the top of our stack:&lt;/p&gt;

&lt;p&gt;Here’s what our stack looks like now with the top blue opening bracket removed:&lt;/p&gt;

&lt;p&gt;Next stop on our iteration through our original string is position 7, a green-colored closing paren.  Is its mate, an opening paren, on the top of the stack?  Yes, it is! That means we can pop off that opening paren from the top of our stack.  Let’s pop it off and here’s what our stack looks like now:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fmk8yr2k90sxl6tzug2el.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fmk8yr2k90sxl6tzug2el.png" alt="Stacks-4"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I won’t go through the rest of the iteration as I’m sure you get the idea!&lt;/p&gt;

&lt;h3&gt;
  
  
  Let’s Code ‘em Up in JS
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/*
 Let's make a helper 'peek' function
that we can use to find the element
on the stack
*/&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;peek&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;isBalanced&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;OPENING&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;({[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;CLOSING&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;)}]&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="c1"&gt;// see FOOTNOTE 1 below:&lt;/span&gt;

  &lt;span class="c1"&gt;// make an empty stack from an array:&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;stack&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;

  &lt;span class="c1"&gt;// iterate through every letter of the string&lt;/span&gt;
  &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

      &lt;span class="c1"&gt;//store each symbol we visit in the string as variable "letter"&lt;/span&gt;
      &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;letter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;charAt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

      &lt;span class="c1"&gt;//if the letter we're visiting is part of the OPENING string, &lt;/span&gt;
        &lt;span class="c1"&gt;// we want to push it onto the stack&lt;/span&gt;
      &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;OPENING&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;letter&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;letter&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="c1"&gt;// otherwise, the letter must be a closing symbol...let's see   &lt;/span&gt;
          &lt;span class="c1"&gt;// if it's mate is on top of the stack:&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;CLOSING&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;letter&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

          &lt;span class="c1"&gt;// OOPS!  Before we check for a match, let's check to see that&lt;/span&gt;
            &lt;span class="c1"&gt;// the stack is not empty.  If the stack is empty, there cannot&lt;/span&gt;
            &lt;span class="c1"&gt;//  be a match.  We'll have to return "false"&lt;/span&gt;
          &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
              &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;

            &lt;span class="c1"&gt;//  Ok, the stack has something in it, let's check for a match&lt;/span&gt;
          &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
              &lt;span class="c1"&gt;// grab the symbol on the top of our stack using our 'peek' method&lt;/span&gt;
                 &lt;span class="c1"&gt;// and assign it to variable 'top'&lt;/span&gt;
              &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;top&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;peek&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
               &lt;span class="c1"&gt;// our top symbol can be found in our OPENING variable&lt;/span&gt;
                 &lt;span class="c1"&gt;//  get the index of our top symbol in the Opening Variable&lt;/span&gt;
                 &lt;span class="c1"&gt;//  and compare it to the index of the letter we're visiting in our CLOSING variable.  &lt;/span&gt;
                 &lt;span class="c1"&gt;//  if the two indicies are THE SAME, we know we have a match and we can pop the top&lt;/span&gt;
                 &lt;span class="c1"&gt;//  item off.&lt;/span&gt;
              &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;OPENING&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;indexOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;top&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;CLOSING&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;indexOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;letter&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                  &lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
                  &lt;span class="c1"&gt;// otherwise, we return false, we know we're not balanced&lt;/span&gt;
              &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
              &lt;span class="p"&gt;}&lt;/span&gt;
          &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="c1"&gt;//lastly before we end, let's make a final check.  If we really have a balanced&lt;/span&gt;
    &lt;span class="c1"&gt;//  string, it means everything has matched up and all the opening symbols from&lt;/span&gt;
    &lt;span class="c1"&gt;//  the stack have been removed and our stack should be empty&lt;/span&gt;
    &lt;span class="c1"&gt;//  stack.length === 0 will return the final boolean value.&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;span class="cm"&gt;/*
  FOOTNOTE 1
  Regarding these two statements:   
  let OPENING = "({["
  let CLOSING = ")}]"

  variable OPENING contains a string of all possible opening symbols. 
  variable CLOSING contains a string of all possible closing symbols.   

  Notice how the index of opening symbol "("  or [0] in OPENING, is exactly the same index
  as its mate, the symbol ")" or [0] in CLOSING.  

  Index of the opening symbol "{" or [1] in OPENING, is exactly the same index
  as its mate, the symbol "}" or [1] in CLOSING.  

  And so on.  This little correspondence will make it easier for us
  to match up opening and closing symbols--we'll use indicies.
*/&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And there you have it!  One of the common algorithms asked in coding interviews solved!&lt;/p&gt;

&lt;p&gt;Keep studying and keep applying for that amazing job you seek!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>stacks</category>
    </item>
    <item>
      <title>Interview Prep:  Data Structures:Stacks
</title>
      <dc:creator>Donny</dc:creator>
      <pubDate>Fri, 25 Sep 2020 21:16:04 +0000</pubDate>
      <link>https://dev.to/kuddleman/interview-prep-data-structures-stacks-d8c</link>
      <guid>https://dev.to/kuddleman/interview-prep-data-structures-stacks-d8c</guid>
      <description>&lt;p&gt;Are you like me and getting reading for a technical interview?  As one technical interviewer said to me, “Technical interviews are getting harder.  Used to be years ago we just asked you to reverse a string.  Now you have to be good at data structures and algorithms.*&lt;/p&gt;

&lt;p&gt;I’ve previously written a two part article one common data structure: linked lists&lt;/p&gt;

&lt;p&gt;Here are the links:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/kuddleman/technical-interview-prep-singly-linked-list-cheat-sheet-in-javascript-1jj6"&gt;Linked Lists Part 1&lt;/a&gt;&lt;br&gt;
&lt;a href="https://dev.to/kuddleman/interview-prep-singly-linked-lists-part-2-20nj"&gt;Linked Lists Part 2&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Today we’ll look at the next data structure that flows from linked lists and common arrays: &lt;strong&gt;the stack&lt;/strong&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  What is a Stack?
&lt;/h3&gt;

&lt;p&gt;The easiest way to think of a stack is to visualize a stack of pancakes on a plate.  When the chef wants to add another pancake to the stack, that new pancake can only be added to the top of the stack.  Later, when the chef is ready to serve those pancakes, they can only take the pancakes from the top of the stack.  &lt;/p&gt;

&lt;p&gt;In other words, &lt;strong&gt;whatever goes on the pancake stack first, will go off last&lt;/strong&gt;.  The pancake stack operates under&lt;br&gt;
a system of FILO (First in, last out).&lt;/p&gt;

&lt;p&gt;Let’s make a couple of other observations about our pancake stack before we start coding.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;The only pancake you can actually “see” or &lt;strong&gt;peek&lt;/strong&gt; at is the topmost pancake in the stack.  &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The only way we can access our pancake stack is via the topmost pancake again!  We can remove that topmost pancake which will reveal the one below it, then remove that newly revealed pancake and get the one below it and so one until we reach our sticky plate.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The pancakes in our stack will know which order they go in since each pancake will “point” to the one below it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The two other things we can do is know how many pancakes are in the stack and state whether there are any pancakes left on the plate, i.e. (isEmpty?).&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  Getting to the Code
&lt;/h3&gt;

&lt;p&gt;Similar to how we set up the Linked Lists, we’ll need two classes for our stack: &lt;/p&gt;

&lt;p&gt;1) a class named “Node” that will create the nodes of information that will go into our stack.  The nodes are our pancakes!&lt;/p&gt;

&lt;p&gt;2) We’ll also need a “Stack” class where we’ll write our methods we’ll need to manipulate our stacks.&lt;/p&gt;

&lt;p&gt;Here’s our skeleton so far:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// class “node” to create the nodes, or “pancakes” that
// will go into our stack:

class StackNode {
  constructor( data, next){
     this.data = data
     this.next = next
  }
}

// Here’s our class where we’ll keep the methods we need
// to manipulate our stack.
// To start each new stack, we’ll begin with a “blank slate”
//  so we’ll set both the “top” (top pancake) and the length
//  of the stack to “null”.

class LinkedStack {
  constructor() {
    this.top = null
    this.size = null
  }
  // methods for our stack will go here
}

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Let’s start adding threw easy methods to get us going.  We want a method to find out if the stack is empty (isEmpty), get the current length of the stack (getLength) and peek at the top pancake (peek).&lt;/p&gt;

&lt;p&gt;for isEmpty all we have to do is  see if there is a “top”.  We can do this by returning the boolean value to the expression this.top === null&lt;/p&gt;

&lt;p&gt;for getLength, we’ll just return the size property which we already initiated in the constructor.&lt;/p&gt;

&lt;p&gt;For peek, our constructor provided us with a "top" property, so we just have to return the data in that property to be able to peek.&lt;/p&gt;

&lt;p&gt;Let's look at our code now after having added the isEmpty(),getLength() and peek() methods:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class StackNode {
  constructor( data, next){
     this.data = data
     this.next = next
  }
}

class LinkedStack {
  constructor() {
    this.top = null
    this.size = null
  }

  isEmpty(){
    return this.top === null
  }

  getLength() {
     return this.size
  }

  peek() {
    return this.top.data
  }

}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Ok, that much is done!   Now let’s look at this picture to figure out what two methods will form the meat and potatoes of our stack implementation. Let’s look at the picture below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--POJumbMK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ekiysmjv4h4208f2bc73.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--POJumbMK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ekiysmjv4h4208f2bc73.png" alt="stack picture"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Starting at the left of the picture above, we see an empty stack.  To add a node, we need a “push” method.  To remove a node, we’ll need a “pop” method (Does this remind you of regular arrays in JavaScript?)&lt;/p&gt;

&lt;p&gt;Let’s do one method at a time:&lt;/p&gt;

&lt;h3&gt;
  
  
  push()
&lt;/h3&gt;

&lt;p&gt;To code out the “push” method, here’s what we’ll need to do:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;The method will take a value as a parameter.  This value is the data for the new node that is about to be pushed on the stack&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Inside the body of the method, we’ll create a new node with the help of our “stack” class and pass that new node our parameter.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Now we want to set our new node’s “next” property to the current top node.  In other words, we want our new node to point to the current top.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We’ll reset the top of the stack to our new node.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Add one to our size property to account for the additional node we just pushed on the stack.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;push(value) {    //pass in the value for the new node
  let node = new StackNode(value)    // create a new node
 node.next = this.top   // Our new node will point to the  
                                   //  current top node
 this.top = node          // our new node is now set as the top  
                                   //node     
 this.size ++               // increment size by one                           
}    

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;On to our next method: pop()&lt;/p&gt;

&lt;h3&gt;
  
  
  pop()
&lt;/h3&gt;

&lt;p&gt;For pop, we want to remove the top node.  Here’s how we'll accomplish that:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;First let save the top node to a variable because we’ll want to return what we popped at the end of the function.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Take the current top and set it to the node below it.  &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Decrement size by one to account for the node we just popped.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Return the data that was held in the node we popped.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here's the code for pop():&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pop(){
  let poppedNode = this.top   // save the 
                          //node we’ll pop to          
                          // a variable

 this.top = this.top.next    // set the top 
                           //node to be the 
                           // one below it

 return poppedNode.data    // return the data
                        // that was contained
                        // in the poppedNode. 
}


&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now let’s put the methods we must wrote back in our  LinkedStack class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class StackNode {
  constructor( data, next){
     this.data = data
     this.next = next
  }
}

class LinkedStack {
  constructor() {
    this.top = null
    this.size = null
  }

  isEmpty(){
    return this.top === null
  }


  getLength() {
     return this.size
  }






  push(value) {    

    let node = new StackNode(value)   
    node.next = this.top                               
    this.top = node                                           
    this.size ++                                         
 }    

  pop(){
    let poppedNode = this.top                                                   
    this.top = this.top.next                                                         
    return poppedNode.data                                            
  }

}


&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And there you have your basis stack data structure implementation.  Next time, let’s have a look at a common algorithm asked in relation to stacks.&lt;/p&gt;

&lt;p&gt;In the meantime,&lt;/p&gt;

&lt;p&gt;Keep coding out your dreams!&lt;/p&gt;

&lt;p&gt;Namaste,&lt;/p&gt;

&lt;p&gt;Donny&lt;/p&gt;

&lt;p&gt;*One interviewer gave me a standard to achieve: be able to do the problems of medium-difficulty on &lt;a href="https://leetcode.com/"&gt;Leet Code&lt;/a&gt;.  The problems of high-difficulty are rarely asked.  He also noted that, if LeetCode is too hard,  you could start with &lt;a href="https://www.hackerrank.com/interview/interview-preparation-kit"&gt;Hacker Rank&lt;/a&gt; which tends to be a bit easier than LeetCode.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>stacks</category>
      <category>datastructures</category>
    </item>
    <item>
      <title>Interview Prep:   Singly Linked Lists--Part 2
</title>
      <dc:creator>Donny</dc:creator>
      <pubDate>Sat, 19 Sep 2020 21:50:47 +0000</pubDate>
      <link>https://dev.to/kuddleman/interview-prep-singly-linked-lists-part-2-20nj</link>
      <guid>https://dev.to/kuddleman/interview-prep-singly-linked-lists-part-2-20nj</guid>
      <description>&lt;p&gt;Onward with interview prep.  If you are unfamiliar with singly linked lists, please read Part 1 as this post will continue from where we left off:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/kuddleman/technical-interview-prep-singly-linked-list-cheat-sheet-in-javascript-1jj6"&gt;Linked Lists Part 1&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;First a quick review:&lt;br&gt;
    Note:  When I refer to “linked lists,”  I am referring to &lt;strong&gt;singly linked lists&lt;/strong&gt;. (There are also doubly linked lists, but we’ll keep them for another time)&lt;br&gt;
 Linked lists are like arrays: they are “list-like” objects.  The difference is that linked lists do not have indices like arrays.  Linked lists have a beginning point( usually called a “head” and an ending point (usually called a “tail”).  If you want to access a given element of the list (also called a “node”), you just have to traverse the linked list starting always at the head.&lt;/p&gt;

&lt;p&gt;Imagine you are standing on one bank of a river and you want to cross it.  There are a series of big rocks that form a bridge across the river.  You can now step from one side of the river (the head) across to the other side of the river (the tail).  Oh yes, that rock bridge is one-way only!&lt;/p&gt;

&lt;p&gt;Ok, that was the review.  Now let’s talk about a common algorithm you might be asked in interviews involving linked lists:&lt;/p&gt;
&lt;h3&gt;
  
  
  Find the Median of the Linked List
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--C7_VQ5Wb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/meay5bjh87534so0qcju.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--C7_VQ5Wb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/meay5bjh87534so0qcju.png" alt="Linked List"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We’re given a linked list as pictured above.  Our linked list has 5 nodes.  Its first node, or head, contains the integer “5”.  This node points to “4”.  “4” points to “7” and so on.  The last node, “10”, points to “null”.  Our task is to find out what the median point of the node is.&lt;/p&gt;

&lt;p&gt;The brute force way might be to just traverse the list and keep a counter so we can find out how long the list is.  When we hit “null” we know we’ve reached the end of the list.  Now just divide the counter by 2 and then floor the result if we get a decimal.  We can then traverse a second time by “result’s” number of times to find the median.&lt;/p&gt;

&lt;p&gt;But let’s impress the interviewer instead.  Let’s show him a really sophisticated way of doing this.   We’ll use the “Tortoise and Hare” approach attributed to Robert W. Floyd.  Let's put both the tortoise and the hare at the head of the linked list.  The hare can traverse the list two times as fast as the tortoise.  In other words, the tortoise will always only be able to cover half the ground as the hare.&lt;/p&gt;

&lt;p&gt;Let’s now let them both start traversing our linked list.  The hare will finish first, of course.  He will have to stop at the tail of the linked list.  But once the hare has reached the end of the linked list, we know the tortoise will have only traversed &lt;strong&gt;half as much as the hare&lt;/strong&gt;.  What?  “Half as much” means half the length of the link or &lt;strong&gt;the middle point!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now we’ve found the median and we’ve done it so efficiently.  Instead of all that counting and extra time traversing twice in our brute force method, we’ve only traversed the list once using “pointers” (the hare and the tortoise).&lt;/p&gt;

&lt;p&gt;Have a look at a picture:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YEWtxDNG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/c9gzcsrb5opmf9y6q4gy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YEWtxDNG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/c9gzcsrb5opmf9y6q4gy.png" alt="Tortoise and Hare"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/Cycle_detection#Floyd.27s_Tortoise_and_Hare"&gt;Got it here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Ok, now let’s code it up in JavaScript:&lt;/p&gt;

&lt;p&gt;First, let’s recreate our two classes from Part I:  First, we'll make a Node class to create individual nodes and second: a SinglyLinkedList class where we’ll put all our methods.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Node {
 constructor(val) {
   this.val = val
   this.next = next
 }
}

class SinglyLinkedList {
 constructor() {
   this.length = 0
   this.head = null
   this.tail = null
 }
}

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now let’s create the shell of our new findMiddleElement method. We’ll set the variables “tortoise” and “hare” each to the head of the linked list as that is where they will start their "run."&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Node {
 constructor(val) {
   this.val = val
   this.next = next
 }
}

class SinglyLinkedList {
  constructor() {
   this.length = 0
   this.head = null
   this.tail = null
  }
  findMiddleElement() {
   let tortoise = this.head
   let hare = this.head         
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The first thing we should do is find out if the list really exists ( Testing for this edge case will show your interviewer that you’re really on your toes!)&lt;/p&gt;

&lt;p&gt;One easy way to do this is just check to see if there is a head.  If there is no head to the list, then there is no list and we can just return “undefined”. (Ask your interviewer what you should return in this case.  Maybe they want something else returned, like “-1” or “Oops!”.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Node {
 constructor(val) {
   this.val = val
   this.next = next
 }
}

class SinglyLinkedList {
 constructor() {
   this.length = 0
   this.head = null
   this.tail = null
 }
 findMiddleElement() {


   let tortoise = this.head
   let hare = this.head         
   if(!this.head) {
    return undefined
  }
}

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Next comes the “meat” of our logic.  We want our tortoise and hare to start moving along the linked list.  However, we don’t know how long our list is, so we should use a “while” loop.&lt;/p&gt;

&lt;p&gt;We’ll let our “while” loop run until the hare gets to the end of the list.  How will we know when the hare has completed his run?  There are two possibilities:&lt;/p&gt;

&lt;p&gt;1).  If there is an odd number of nodes he'll be at the end of the list when he gets to the very last node  We’ll know he’s at the last node when the next node is “null”.  For example:  in a list that has  7 nodes, he’ll start at node #1, and then moving 2 nodes at a time, he’ll go from node 1 to node 3 to node 5 to node 7.  At node 7, the next node is null, he’ll have to stop there. This means our condition for the “while” loop will be “keep going as long as the hare’s “next” node is not “null” (hare.next !== null)&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Now consider if there is an even number of nodes.  For example, if there are 8 nodes and our hare starts at node 1, he’ll go node 1 to node 3 to node 5 to node 7.  At node 7 when he then jumps 2 nodes, he’ll go off the list and be in “null” land.  So we want him to keep going as long as he’s NOT in “null” land ( hare !== null)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now let’s put in the shell of our “while” loop.  We’ll combine our two conditions with an “&amp;amp;&amp;amp;” logical operator.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Node {
 constructor(val) {
   this.val = val
   this.next = next
 }
}

class SinglyLinkedList {
 constructor() {
   this.length = 0
   this.head = null
   this.tail = null
 }
  findMiddleElement() {
   let tortoise = this.head
   let hare = this.head    

   if(!this.head) {
    return undefined
   }

   while ( hare !== null &amp;amp;&amp;amp; hare.next !== null) {
   }
  }
}

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The next part is easy.  In the body of the “while” statement we want to let our heroes go!  We’ll use “dot next” (.next) to tell each hero to move to the next node. That means the tortoise can go (.next), but the hare has to go twice as fast (.next.next).  Like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Node {
 constructor(val) {
   this.val = val
   this.next = next
 }
}

class SinglyLinkedList {
  constructor() {
   this.length = 0
   this.head = null
   this.tail = null
  }
  findMiddleElement() {
   let tortoise = this.head
   let hare = this.head 
   if(!this.head) {
    return undefined
  }

  while ( hare !== null &amp;amp;&amp;amp; hare.next !== null) {
    tortoise = tortoise.next
    hare = hare.next.next
  }
 }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And lastly, we’ll retrieve our prize.  Once the while loop has run its course, our hare will be sitting at the end of the linked list while our tortoise will be at the median point.  Let’s get tortoise’s data value in our final return statement to complete the algorithm:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Node {
 constructor(val) {
   this.val = val
   this.next = next
 }
}

class SinglyLinkedList {
 constructor() {
   this.length = 0
   this.head = null
   this.tail = null
 }
 findMiddleElement() {
   let tortoise = this.head
   let hare = this.head     
   if(!this.head) {
    return undefined
  }

   while ( hare !== null &amp;amp;&amp;amp; hare.next !== null) {
    tortoise = tortoise.next
    hare = hare.next.next
   }

   return hare.val
 }

}

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This tortoise and the hare approach is useful in other kinds of problems, too.  Keep this approach on your back burner whenever you’re looking at linked lists or any kind of cycles where you’re trying to find the end, middle point or where something intersects with something else.&lt;/p&gt;

&lt;p&gt;Happy interviewing and best wishes!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>linkedlists</category>
      <category>interview</category>
    </item>
  </channel>
</rss>
