<?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: Nick Salvatore</title>
    <description>The latest articles on DEV Community by Nick Salvatore (@nicksalvatore).</description>
    <link>https://dev.to/nicksalvatore</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%2F1256508%2Ff3862ab9-ffd2-45d7-8c99-7f0f8db1eba7.jpg</url>
      <title>DEV Community: Nick Salvatore</title>
      <link>https://dev.to/nicksalvatore</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nicksalvatore"/>
    <language>en</language>
    <item>
      <title>Falsey values and -1 returns</title>
      <dc:creator>Nick Salvatore</dc:creator>
      <pubDate>Mon, 04 Mar 2024 14:59:10 +0000</pubDate>
      <link>https://dev.to/nicksalvatore/falsey-values-and-1-returns-45n</link>
      <guid>https://dev.to/nicksalvatore/falsey-values-and-1-returns-45n</guid>
      <description>&lt;p&gt;This has happened to me a lot, so I thought it was worth reflecting on. I came across a Leetcode problem this morning that asks you to create a method for an array that returns the last item in the array. If the array is empty, return &lt;code&gt;-1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It’s a beginner-level problem, but the gotcha comes in considering falsey values like &lt;code&gt;null&lt;/code&gt; or &lt;code&gt;undefined&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Intuition
&lt;/h2&gt;

&lt;p&gt;The intuition in solving this problem is to simply get the index of the last item using &lt;code&gt;.length-1&lt;/code&gt;. If the item exists, return the item. Otherwise, return &lt;code&gt;-1&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;This approach has one problem though which is that if the array contains falsey values like &lt;code&gt;null&lt;/code&gt; or &lt;code&gt;undefined&lt;/code&gt;, it will return &lt;code&gt;-1&lt;/code&gt;. &lt;/p&gt;

&lt;h2&gt;
  
  
  Don’t return -1 explicitly
&lt;/h2&gt;

&lt;p&gt;These Leetcode problems arise pretty regularly where you need to return something in an array, otherwise you return &lt;code&gt;-1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;There’s a strong intuition, at least for me, to check for some case and then explicitly return &lt;code&gt;-1&lt;/code&gt; if that case isn’t met. You’ll fail your tests and then see that since the last item you were trying to access is &lt;code&gt;null&lt;/code&gt;, &lt;code&gt;-1&lt;/code&gt; was returned instead of &lt;code&gt;null&lt;/code&gt;. Then you’ll add a check for &lt;code&gt;null&lt;/code&gt; or &lt;code&gt;undefined&lt;/code&gt;, but you’ll find that empty arrays also return &lt;code&gt;undefined&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;A sign that you are approaching the problem incorrectly is acting on this intuition to return &lt;code&gt;-1&lt;/code&gt; explicitly.&lt;/p&gt;

&lt;h2&gt;
  
  
  Derive -1 naturally
&lt;/h2&gt;

&lt;p&gt;If an array is empty, calculating the index with &lt;code&gt;.length-1&lt;/code&gt; will return &lt;code&gt;-1&lt;/code&gt;. So to solve the problem eloquently, all you need to do is check for an empty array. If the array is empty, return the index, otherwise return the item.&lt;/p&gt;

&lt;p&gt;The code would look something 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;Array.prototype.last = function() {
    const i = this.length-1

    if (i &amp;lt; 0) {
        return i
    }

    return this[i]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The lesson
&lt;/h2&gt;

&lt;p&gt;So again, if you don’t want to deal with the headache of failed Leetcode tests on simple af problems, which leave you feeling like more of an imposter than you already are (I'm projecting, you're probably great), make sure to consider falsey values and also be wary of explicit &lt;code&gt;-1&lt;/code&gt; returns.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>leetcode</category>
    </item>
    <item>
      <title>Conceptualizing the two crystal ball problem</title>
      <dc:creator>Nick Salvatore</dc:creator>
      <pubDate>Tue, 16 Jan 2024 05:09:11 +0000</pubDate>
      <link>https://dev.to/nicksalvatore/the-two-crystal-ball-problem-458b</link>
      <guid>https://dev.to/nicksalvatore/the-two-crystal-ball-problem-458b</guid>
      <description>&lt;h1&gt;
  
  
  The problem
&lt;/h1&gt;

&lt;p&gt;You have two identical crystal balls and you are to determine the most efficient way to figure out the lowest floor in a given building where the crystal balls will break when dropped.&lt;/p&gt;

&lt;h1&gt;
  
  
  Setting up the problem
&lt;/h1&gt;

&lt;p&gt;If all you had was one crystal ball, the only way to guarantee success in finding the answer would be with a linear search, or simply dropping the ball from each story, starting at the first, until you found the solution. Because we have 2 crystal balls though, we have more freedom to jump around. Suppose that the building in the problem has 120 floors. With a linear search, your worst case scenario would be 120 drops. If you moved in intervals of 2 floors though, your worst case scenario would be 61 steps. 60 steps forward, 1 step backward to see if the previous floor was the breaking point or if the floor at step 60 was the breaking point. If you moved in intervals of 3, your worst case scenario would be 42 steps. 40 steps forward, 1 step backward to the floor above the previous interval and 1 more step forward to complete scanning the entire interval.&lt;/p&gt;

&lt;p&gt;To write this out mathematically, you could use use the following variables:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;n = total number of floors&lt;/li&gt;
&lt;li&gt;m = step interval&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And the equation would look like this:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;n / m + (m - 1) = total number of steps in the worst case&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For the example using intervals of 3, the equation would be applied as:&lt;/p&gt;

&lt;p&gt;120 / 3 + (3 - 1) =&amp;gt; 40 + 2 =&amp;gt; 42&lt;/p&gt;

&lt;p&gt;So in order to figure out the optimal, or most efficient, interval size, we want to minimize the total number of drops and we can do this by finding the derivative of the function of n / m + (m - 1) which is the &lt;strong&gt;square root of n&lt;/strong&gt;. Another way to find this solution without the use of calculus is to approximate m - 1 as m and set n / m = m. Solve for m, which is also the &lt;strong&gt;square root of n&lt;/strong&gt;. &lt;/p&gt;

&lt;h1&gt;
  
  
  The solution
&lt;/h1&gt;

&lt;p&gt;Given this minimization, the optimal way to find the lowest floor at which one of the crystal balls would break is by traversing the building in intervals of the square root of n.&lt;/p&gt;

</description>
      <category>computerscience</category>
      <category>algorithms</category>
    </item>
  </channel>
</rss>
