<?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: Vineeth</title>
    <description>The latest articles on DEV Community by Vineeth (@vineeth).</description>
    <link>https://dev.to/vineeth</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%2F513872%2Fd7396e9d-f278-41f1-ae73-4462699490a6.jpg</url>
      <title>DEV Community: Vineeth</title>
      <link>https://dev.to/vineeth</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vineeth"/>
    <language>en</language>
    <item>
      <title>Find the nTh smallest element in an array</title>
      <dc:creator>Vineeth</dc:creator>
      <pubDate>Tue, 15 Dec 2020 03:16:20 +0000</pubDate>
      <link>https://dev.to/vineeth/find-the-nth-smallest-element-in-an-array-2eam</link>
      <guid>https://dev.to/vineeth/find-the-nth-smallest-element-in-an-array-2eam</guid>
      <description>&lt;p&gt;Couple of days ago I came across a problem which asked me to find the nth smallest element in an array. It was marked as a medium level problem, and it quickly got me thinking, why is it so hard??? Simply sort the array and then return the value which is present in the index...&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="c1"&gt;// This is assuming the nthSmallestIndex is within the array.&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;getNthSmallestElement&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;array&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;nthSmallestIndex&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="nc"&gt;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;sort&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;nthSmallestIndex&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;The above code seems quite elementary, so why would this be a medium level problem. Well, my question was answered quite soon. When I ran the code, the online editor complained that my code was slow. To sort an array where the range is completely unknown, the fastest which it can be done is in O(n log n). The question required me to complete the code in O(n) time. &lt;/p&gt;

&lt;p&gt;It then occurred to me that I need not sort the entire array. I only need to sort the array up-to the nth smallest index. So if I have the following array [5, 2, 1, 4, 6, 3], and if I need to find the 2nd smallest number, I just need to sort first 3 elements.&lt;/p&gt;

&lt;p&gt;Well I could use the &lt;a href="https://en.wikipedia.org/wiki/Quicksort#Lomuto_partition_scheme"&gt;lomuto partition&lt;/a&gt; to select a pivot element and put it at the correct index and reorder the array where all the elements smaller than the pivot is on left side and greater elements on the right. Let's take a look at how the lomuto partition is implemented.&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;int&lt;/span&gt; &lt;span class="nf"&gt;lomutoPartition&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;low&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;high&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;)&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;pivot&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;high&lt;/span&gt;&lt;span class="o"&gt;],&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;low&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;for&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;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;low&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;high&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;pivot&lt;/span&gt;&lt;span class="o"&gt;)&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;temp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
      &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
      &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
      &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="n"&gt;j&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="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;high&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
  &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;high&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
  &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;j&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;So if I have my above array, and if I apply the lomuto partition keeping the last element as the pivot, I might get something like this...[2, 1, 3, 6, 4, 5]. Notice how 3 is in the right place and all the elements to the left is lesser than 3 and elements to the right are greater than 3.&lt;/p&gt;

&lt;p&gt;Now all I need to check is, if the nth smallest index is greater or smaller than the current pivot and then keep on sorting the array until I reach the required index. Then, it dawned on me, this is QuickSort with a little modification!!! Let's take a look at the code...&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;int&lt;/span&gt; &lt;span class="nf"&gt;findTheNthSmallestElement&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;array&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;nThSmallest&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;left&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;right&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="o"&gt;)&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;position&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;lomutoPartition&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;array&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;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;position&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;left&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="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;nThSmallest&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;position&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
      &lt;span class="o"&gt;}&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;nThSmallest&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;findTheNthSmallestElement&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;nThSmallest&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;position&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="o"&gt;}&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;findTheNthSmallestElement&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;nThSmallest&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;position&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="n"&gt;right&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&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="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now this solution on an average case get the nth smallest element in O(n) time. Later when I was doing a google search I found that the above solution is called "QuickSelect"&lt;/p&gt;

&lt;p&gt;I hope you this post helped you to understand how to find the nth smallest element in O(n) time (Average case.) &lt;/p&gt;

&lt;p&gt;If you would like to see the complete implementation and other suck questions check out my &lt;a href="https://github.com/VinnieM/Popular-DS-Questions"&gt;Repo&lt;/a&gt; on github.&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>java</category>
      <category>computerscience</category>
      <category>career</category>
    </item>
    <item>
      <title>The weird substring quirk</title>
      <dc:creator>Vineeth</dc:creator>
      <pubDate>Thu, 10 Dec 2020 02:58:12 +0000</pubDate>
      <link>https://dev.to/vineeth/the-weird-substring-quirk-4d4l</link>
      <guid>https://dev.to/vineeth/the-weird-substring-quirk-4d4l</guid>
      <description>&lt;p&gt;Every once in a while have you ever come across a quirk in a programming language, which you quite do not understand, but yet come to terms with it.&lt;/p&gt;

&lt;p&gt;One such quirk is the substring API which is present in Java. For starters, substring is an overloaded function - the result; there are 2 different forms of the same function.&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="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;substring&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;beginIndex&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Returns a new string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string.&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="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;substring&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;beginIndex&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;endIndex&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex.&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="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;str&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Hello, World"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;substringOne&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;str&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;substring&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="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;substringTwo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;str&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;substring&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can quite confidentally say that the output for substringOne will be "World". But we would be wrong if we were to say the output of substringTwo would be "Hello,".&lt;/p&gt;

&lt;p&gt;The reason being the endIndex is exclusive. The inclusion/exclusive model is standard for ranges of the Java API. One of the main advantage is that the length of the string can be used as the end point without subtracting the "-1" from the length.&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="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;apollo13&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Houston, we have a problem"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;normalPeople&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;apollo13&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;substring&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;apollo13&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But, is this really necessary? Java already provides an overload to the substring method which captures exactly this behaviour. All I’m trying to say is that I’ve seen many coders get tripped up. I have seen most people forget about exclusion when providing the endIndex.&lt;/p&gt;

&lt;p&gt;Let me know if you feel the same or if I am totally off base here...&lt;/p&gt;

</description>
      <category>java</category>
      <category>programming</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>JSON parser</title>
      <dc:creator>Vineeth</dc:creator>
      <pubDate>Wed, 02 Dec 2020 18:56:09 +0000</pubDate>
      <link>https://dev.to/vineeth/json-parser-48b9</link>
      <guid>https://dev.to/vineeth/json-parser-48b9</guid>
      <description>&lt;p&gt;This is a JSON parser which allows the user to find the value of a key which is present inside the JSON structure. The question is why do I need a JSON parser? I can do that by writing a loop. Why do I need an additional parser which does that for me?&lt;/p&gt;

&lt;p&gt;Well in my last project I used to have a class ProjectUtils which contained functions of commonly used things. That is where I got the inspiration to come up with this.&lt;/p&gt;

&lt;p&gt;This parser is used to will retrieve a key from a JSON structure provided you give the path to where the key is present.&lt;/p&gt;

&lt;p&gt;let us take the below JSON structure as an example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"perms"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"changeDefault"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Yes"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"visible"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"notused"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"testArray"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[{&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"TestKey1"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"TestVal1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"TestKey2"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"TestVal2"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"TestKey3"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"TestVal3"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"TestKey4"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"TestVal4"&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;}],&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"selectable"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"allAccess"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"defaultOnly"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}],&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"avatarUrl"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"cartId"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"8ladf51ds65ga6"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"DeviceDetails"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"DeviceName"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"MotoXPlay"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"UDID"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"459f8202b2n92h1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"Info"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"IPDetails"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"192.168.3.155"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"TestMgmtID"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"8080"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"IsPhone"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If I want to get the value of key TestKey4, I would be able to do it by passing the JSON structure and then the location of where the value is present.&lt;/p&gt;

&lt;p&gt;getNodeValue(JSONStructure, perms.0.testArray.0.TestKey4);&lt;/p&gt;

&lt;p&gt;Where the first parameter is the JSONStructure to search.&lt;br&gt;
The second parameter is the location. As per the example perms is name of the array, the 0th index, then the testArray and 0th index and the Key TestKey4.&lt;/p&gt;

&lt;p&gt;If I wanted to get an object, then I would not need to pass the index. If I wanted to check if the isPhone is true of false. I could get it like&lt;/p&gt;

&lt;p&gt;getNodeValue(JSONStructure, DeviceDetails.Info.IsPhone);&lt;/p&gt;

&lt;p&gt;As per the current implementation the getNodeValue function would return an Java Object. The advantage of returning as an Object is that whatever type of value the key holds, the user could get that without anticipating the return value.&lt;/p&gt;

&lt;p&gt;This is the &lt;a href="https://github.com/VinnieM/JSONParser"&gt;GitHub repo&lt;/a&gt;&lt;br&gt;
Leave an comment if guys think this will be helpful. Hope it helps.&lt;/p&gt;

&lt;p&gt;Cheers&lt;/p&gt;

</description>
      <category>java</category>
      <category>coding</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Interview Questions</title>
      <dc:creator>Vineeth</dc:creator>
      <pubDate>Wed, 02 Dec 2020 01:29:45 +0000</pubDate>
      <link>https://dev.to/vineeth/interview-questions-4c02</link>
      <guid>https://dev.to/vineeth/interview-questions-4c02</guid>
      <description>&lt;p&gt;I am interested in solving questions on leetcode and GeeksForGeeks. Thought it would be good if I can curate all the questions in a repo and put it on Github. Would like to know the feedback from the general community.&lt;/p&gt;

&lt;p&gt;Github link - &lt;a href="https://github.com/VinnieM/Popular-DS-Questions"&gt;https://github.com/VinnieM/Popular-DS-Questions&lt;/a&gt;&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>beginners</category>
      <category>github</category>
    </item>
    <item>
      <title>A Software Developer’s Guide to Effective Debugging</title>
      <dc:creator>Vineeth</dc:creator>
      <pubDate>Fri, 13 Nov 2020 15:25:42 +0000</pubDate>
      <link>https://dev.to/vineeth/a-software-developer-s-guide-to-effective-debugging-27jn</link>
      <guid>https://dev.to/vineeth/a-software-developer-s-guide-to-effective-debugging-27jn</guid>
      <description>&lt;p&gt;There are some things in life you can’t escape, like death and taxes. And also for us techies, there’s programmers creating bugs.&lt;/p&gt;

&lt;p&gt;It’s inevitable that software developers end up devoting a great deal of their time to debugging code. Debugging is therefore an important skill to master for maximizing efficiency. Regrettably, even the most proficient of developers have the tendency to be, well… clumsy.&lt;/p&gt;

&lt;p&gt;There are an abundance of developers who can flick through new features, write code and implement the business requirements like the great Dennis Ritchie himself was tutoring them, but have you ever wondered who cleans up the colossal number of bugs they leave behind?&lt;/p&gt;

&lt;p&gt;It’s one thing to know how to write beautiful elegant code. It’s another thing to know how to debug the most disagreeable code you’ve ever seen in your lifetime, which was written by that previously mentioned mythical person who single-handily managed to put together the entire application in 48 hours.&lt;/p&gt;

&lt;p&gt;As luck would have it, debugging – like any other skill – is something that can be learned. If you apply the right techniques and practices, you can become great at it. Who knows? You might even enjoy it.&lt;/p&gt;

&lt;p&gt;The real secret to debugging&lt;/p&gt;

&lt;p&gt;The secret ingredient to debugging is this: understand that it’s all about the mind-set. It’s about taking a logical line of attack to confront the problem — not rushing, not fathoming the resolution and most certainly not assuming you can simply determine the problem. Most developers have adopted the approach of shooting first, shooting some more, and then when everybody’s dead, manage to ask a question or two. Debugging should be about staying calm and collected, and attacking the problem from a rational, analytical perspective, instead of a dramatic one.&lt;/p&gt;

&lt;p&gt;Before we take a deep dive, let us try to understand “what exactly is debugging?” It sounds pretty obvious, right? You open up the debugger perspective and you fix the glitches you are finding in your code. That is where you would be unequivocally mistaken. Debugging has got absolutely nothing to do with the debugger.&lt;/p&gt;

&lt;p&gt;Debugging has everything to do with:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Finding the cause of a problem in the code base&lt;/li&gt;
&lt;li&gt;Recognizing the possible reasons for it&lt;/li&gt;
&lt;li&gt;Trying out hypotheses until the eventual root cause is discovered&lt;/li&gt;
&lt;li&gt;Then, in due course, removing that cause and guaranteeing that it will never happen again.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;My argument is: debugging is more than fiddling all over the place in a debugger and mutating code while waiting for the business logic to magically kick in.&lt;/p&gt;

&lt;p&gt;Now, let’s walk through the typical programmer’s debugging process and you will see exactly what I mean.&lt;/p&gt;

&lt;p&gt;The one thing you should NOT do while debugging&lt;/p&gt;

&lt;p&gt;So you come into the office and your project manager says he’s got some bugs for you to fix. You, the programmer, decide to sit down at your cubicle with the thought “I will let loose the full power of my intellectual prowess on this blasphemous terror of bugs”.&lt;/p&gt;

&lt;p&gt;You start up the debugger. Cautiously you step through the code. Time seems to blur, minutes change into hours, hours into weeks. Slowly, you become the old bloke sitting at the keyboard, motionless in the unchanged debugging session, but in some way you are “closer.” Your children have all grown. Your wife may have left you. The only thing that remains is… the bug.&lt;/p&gt;

&lt;p&gt;What an absurd number of programmers do when they want to debug an issue in the code is to fire up the debugger and start looking around from one place to another. Never do this. The debugger ought to be your ultimate alternative. When you start up the debugger straightaway, you are saying, “I do not have any knowledge on the root of the problem is, I will just skim through everything and try to comprehend what is going on.”&lt;/p&gt;

&lt;p&gt;It’s like those chaps we see on the road every once in a while: when their car breaks down, even though they do not know anything about cars, they still pop open the bonnet and anticipate finding something inside, thereby giving a false impression to the outside world. A real mechanic might not even stop to help, thinking this fellow has got everything under control.&lt;/p&gt;

&lt;p&gt;The exact same principle applies when debugging. You need to know what you are looking for. Do not misunderstand me – the debugger is a wonderful and powerful tool. When used properly, the debugger can help you decipher all categories of glitches and see what materialises when your code is running. Nevertheless, the debugger is not place to start. Numerous bugs can be resolved without ever touching the debugger.&lt;/p&gt;

&lt;p&gt;The one thing you absolutely MUST do while debugging&lt;/p&gt;

&lt;p&gt;So, what do you do if you are not supposed to fire up the debugger when you begin to debug an issue? The first thing any well-balanced, rational software engineer ought to do is reproduce the bug. Why? To be certain that the issue is truly a bug and that you will be able to debug it.&lt;/p&gt;

&lt;p&gt;One hundred percent of glitches that cannot be replicated cannot be debugged. So if you cannot precisely replicate the problem, there is no point in even debugging it. You are exceedingly unlikely to fix a problem that cannot be properly replicated. Nevertheless, even if you did fix it, how on earth would you verify that it was fixed in the approved manner?&lt;/p&gt;

&lt;p&gt;Four steps to debugging effectively&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Replicate the bug - So, when you are trying to fix a bug, your initial objective should be proving that you can replicate the bug yourself. If you cannot, you ought to get the tester who raised the issue in the first place to precisely replicate it for you. If the bug is intermittent and cannot be precisely replicated, this effectively means one of two things: either you do not know the code base properly, or you need to add more variables into your environment that are mandatory to reproduce the problem. In situations like these, try to gather more evidence using some of the following practices: inserting more logging comments in the code, asking the developer who originally wrote the code, or asking people who have been working on the same module. Always understand there is no such thing as an intermittent problem. If you do not understand the bug enough to replicate it, you have a very slim probability of accidentally fixing it (even by a guess), and you will have an extremely difficult time knowing if your fix even worked. Always find a way to replicate the glitch, even if it is only replicable in the production environment.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Sit and think - Right after you reproduce your glitch, the stage that follows is the most crucial one that software developers avoid for the same reason that they are so hasty to decipher the glitch. Your next step is to sit and think. Yes, that’s correct. Ponder the glitch and what its conceivable roots could be. Consider how the system works and possible explanations for the odd behaviour you are seeing. You are going to be in a rush to jump into the code and into the debugger and start “looking at things”, but before you start doing that, it is vital to understand what you are looking for and what exactly to look at. You will likely generate a few concepts or hypotheses about what might perhaps be causing the glitch. You ought to have a minimum of two or three that you can experiment with before you move on.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Test your concepts - Most of the time, your hypotheses are not going to work out. That is just life. If that is the case, the next best thing you can do is to check your assumptions about how things are working. We naturally assume that code is working a certain way, or that some input or output must be some value. Time and again we think, “this cannot possibly be happening!” and often we are proven wrong. It happens to the best of us. The best thing you can do with these assumptions is to validate them. You do this by writing Unit Test Cases. Write a few unit tests that check the apparent things that have to be working along the workflow of the problem you are trying to debug. Most of these tests should easily pass, but every once in a while, you’ll write a unit test to test some evident assumption and the results would be nothing short of shocking. Always remember that if the answer to your glitch were obvious, it wouldn’t be a glitch at all.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Understand how you fixed it - If you fix a problem, understand what you did to fix it. If you do not recognize whether what you did fixed the problem, you are not done yet. For all you know, you may have inadvertently caused a different problem, or most likely, you haven’t fixed your original problem. Problems never go away on their own. When you fix the glitch, don’t stop there. Explore a little further and make sure you understand exactly what was going on that caused the problem in the first place, and how your solution fixed it. When software developers debug a glitch by fiddling around with the code and it seemingly starts working, they assume it is fixed without even knowing why. This is a dangerous habit for many reasons. As stated above, when you unsystematically tweak gears in the system and alter bits of code here and there, you could be triggering all kinds of other glitches without realising it. More importantly, you are training yourself to be a terrible debugger! You might get lucky from time to time, but you won’t have a repeatable procedure or a dependable skillset for debugging.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Effective debugging in practice&lt;/p&gt;

&lt;p&gt;A while ago I resolved a nasty bug for a client. It had to do with the Secure Socket Layer (SSL) connections to a third-party client’s system being released randomly. The third-party client was using ancient software, and my client had a custom code that connected with the third-party client’s system over SSL.&lt;/p&gt;

&lt;p&gt;There had been numerous efforts to resolve this glitch, which were grounded on not much more than blind deductions. One of these deductions (which seemed to work for some time) was to put all the information into a buffer, and at that time, issuing a single write() function call on the OutputStream function of the SSLSocket, with the expectation that it would all be propelled as a single SSL packet. For a while this gave the impression that it was working, but the bug would occasionally reappear after this so-called “fix.”&lt;/p&gt;

&lt;p&gt;Finally the third-party client called in the vendor of their Jurassic-era software, who in due course discovered precisely why their software was intermittently releasing the connection. The messages that we were sending began with a 4-byte length field. They observed that our data was fragmented into two SSL records, the first record comprised of merely the very first byte of the message, and the second comprised of the rest of the message. Their software was not equipped to handle that – it anticipated that the first SSL record would contain at least the 4-byte length field.&lt;/p&gt;

&lt;p&gt;The mystery now was why our code was transferring the first byte in a separate SSL record. To find out why this occurred, I ran the code in a debug mode and stepped into the JDK source code. After some time I came across a class sun.security.ssl.SSLSocketImpl. The getOutputStream() function of this class returns a sun.security.ssl.AppOutputStream. The class AppOutputStream implements the write() function. After prudently observing this, I saw something that looked a bit suspicious. There were some lines to decide how many bytes of data to put in an SSL record:&lt;/p&gt;

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

&lt;p&gt;Under certain circumstances, if isFirstRecordOfThePayload and c.needToSplitPayload() is true, it chooses to put at most 1 byte into the SSL record. This is precisely what the client was seeing and what was causing the problem. But it appeared that this was done intentionally, and was not a bug in the JDK. But why? And what is in the method needToSplitPayload()? This is a method in class SSLSocketImpl. After reading about it in detail, I understood that it turned out to be a workaround for a security problem with TLSv1.0 and older. Regrettably, the third-party client’s source code couldn’t deal with this workaround.&lt;/p&gt;

&lt;p&gt;Luckily, the people at Sun (now called Oracle) anticipated that the workaround might cause compatibility snags, so they provided a way to deactivate it by setting a system variable jsse.enableCBCProtection to false. We had a choice between disabling the workaround by setting the system property jsse.enableCBCProtection to false, which would make the software vulnerable to the security flaw, or ensuring that we used TLSv1.1 or newer, which is what the management finally decided to do.&lt;/p&gt;

&lt;p&gt;Debugging – like software development – has both a science and an art to it. You can only excel at debugging through constant practice, which you will most certainly get throughout your career. After all, life’s inevitabilities are death, taxes and programmers creating bugs.&lt;/p&gt;

</description>
      <category>firstpost</category>
      <category>debug</category>
      <category>java</category>
    </item>
  </channel>
</rss>
