<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Prince Kumar Chaurasia on Medium]]></title>
        <description><![CDATA[Stories by Prince Kumar Chaurasia on Medium]]></description>
        <link>https://medium.com/@pkcofficial24?source=rss-799771c4d075------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*W7Jb-wzo-uka0dqH</url>
            <title>Stories by Prince Kumar Chaurasia on Medium</title>
            <link>https://medium.com/@pkcofficial24?source=rss-799771c4d075------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 11:52:54 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@pkcofficial24/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Sorting Algorithms : Merge Sort]]></title>
            <link>https://medium.com/@pkcofficial24/sorting-algorithms-merge-sort-4bd7c16a9860?source=rss-799771c4d075------2</link>
            <guid isPermaLink="false">https://medium.com/p/4bd7c16a9860</guid>
            <dc:creator><![CDATA[Prince Kumar Chaurasia]]></dc:creator>
            <pubDate>Tue, 22 Jul 2025 11:05:21 GMT</pubDate>
            <atom:updated>2025-07-22T11:05:21.286Z</atom:updated>
            <content:encoded><![CDATA[<h3>Sorting Algorithms : Merge Sort</h3><p>Merge Sort is one of the most efficient sorting algorithm. We will walk over the algorithm in the easiest way possible.</p><p>It is a <strong>Divide and Conquer</strong> algorithm which divides the input array recursively and merge it back to sort it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/318/1*S0wEA_KuYG1yPKWgDfiGMg.png" /></figure><p>For Example : Given an array : <strong>[5, 6, 2, 3, 9, 12, 1, 8, 10]</strong> , you have to sort it using Merge Sort.</p><p>According to the steps of merge sort we need to divide the array into halves, like :<br>1 : [5, 6, 2, 3, 9] <strong>&amp; </strong>[12, 1, 8, 10]<br>2 : [5, 6, 2] <strong>&amp;</strong> [3, 9] <strong>|</strong> [12, 1] <strong>&amp;</strong> [8, 10]<br>3 : [5, 6] <strong>&amp;</strong> [2] | [3] <strong>&amp;</strong> [9] | [12] <strong>&amp;</strong> [1] | [8] <strong>&amp;</strong> [10]<br>4 : [5] &amp; [6] and so on.</p><p>Now, each element of the array are individual, we can use a temporary array to store sorted elements at each level going back the recursive calls.</p><p>Now we <strong>start merging</strong> these divided parts in sorted order:</p><ul><li>Merge [5] &amp; [6] → [5, 6]</li><li>Merge [5, 6] &amp; [2] → [2, 5, 6]</li><li>Merge [3] &amp; [9] → [3, 9]</li><li>Merge [2, 5, 6] &amp; [3, 9] → [2, 3, 5, 6, 9]</li></ul><p>Similarly for the right half:</p><ul><li>Merge [12] &amp; [1] → [1, 12]</li><li>Merge [8] &amp; [10] → [8, 10]</li><li>Merge [1, 12] &amp; [8, 10] → [1, 8, 10, 12]</li></ul><p>Finally, merge the two sorted halves:</p><p>[2, 3, 5, 6, 9] and [1, 8, 10, 12] → <strong>[1, 2, 3, 5, 6, 8, 9, 10, 12]</strong></p><p>This is how the algorithm internally works.</p><p>Let’s look into the algorithm’s code :</p><pre>class Solution {<br>   public static void main(String[] args){<br>      int[] arr = new int[]{5, 6, 2, 3, 9, 12, 1, 8, 10};<br>      mergeSort(arr);<br>      System.out.println(Arrays.toString(arr));<br>   }<br><br>   private static void mergeSort(int[] arr){<br>      int left = 0, right = arr.length - 1;      <br>      mergeSort(arr, left, mid, new int[]{});<br>   }<br>    <br>   private static void mergeSort(int[] arr, int left, int right, int[] temp){<br>      if(left &gt;= right) return;<br>      int mid = left + (right - left)/2;<br><br>      <br>      mergeSort(arr, left, mid, temp);<br>      mergeSort(arr, mid + 1, right, temp);<br><br>      merge(arr, left, mid, right, temp); <br>   }<br><br>   private static void merge(int[] arr, int left, int mid, int right, int[] temp){<br>      int i = left, k = left, j = mid;<br><br>      while( i &lt;= mid &amp;&amp; j &lt;= right){<br>          if(arr[i] &lt; arr[j]){<br>             temp[k++] = arr[i++];<br>          } else {<br>             temp[k++] = arr[j++];<br>          }<br>      }<br>        <br>      while(i &lt;= mid) temp[k++] = arr[i++];<br>      while(j &lt;= right) temp[k++] = arr[j++];<br><br>      for(int index = left; index &lt;= right; index++){<br>          arr[index] = temp[index];<br>      }<br>   }<br>}</pre><p>It’s a little tricky, try to do it multiple times, you will get it.</p><p>Happy coding.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4bd7c16a9860" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Javascript Beyond Basics]]></title>
            <link>https://medium.com/@pkcofficial24/javascript-beyond-basics-f25a020c4df3?source=rss-799771c4d075------2</link>
            <guid isPermaLink="false">https://medium.com/p/f25a020c4df3</guid>
            <dc:creator><![CDATA[Prince Kumar Chaurasia]]></dc:creator>
            <pubDate>Fri, 18 Apr 2025 19:47:37 GMT</pubDate>
            <atom:updated>2025-04-18T19:47:37.871Z</atom:updated>
            <content:encoded><![CDATA[<p>Learning the depth of Javascript</p><h3>JS Engine Internals (V8) + Optimization Magic</h3><p>What happens when you write:</p><pre>const x = 5 + 3;</pre><p>It’s not just magically calculated. Here’s what happens:</p><h3>Breakdown of JS Engine (like V8)</h3><ol><li><strong>Parsing</strong></li></ol><ul><li>Converts your code → an Abstract Syntax Tree (AST)</li><li>Detects syntax errors</li><li>Uses a parser (like Acorn, Esprima)</li></ul><p><strong>2. Compilation (not interpretation!)</strong></p><ul><li>Modern JS engines <strong>compile</strong> JS to machine code</li><li>Uses <strong>Just-In-Time (JIT)</strong> compilation</li><li>First: <strong>Ignition</strong> → bytecode</li><li>Then: <strong>TurboFan</strong> → optimized native code</li></ul><p><strong>3. Execution</strong></p><ul><li>Your code runs in bytecode/natively optimized machine instructions</li></ul><p><strong>4. Optimization</strong></p><ul><li>V8 watches how your code behaves</li><li>If a function runs often with the same type of data → it <strong>optimizes</strong></li><li>If behavior changes (e.g., types flip) → it <strong>de-optimizes</strong>!</li></ul><p>V8 Pipeline</p><pre>Source Code → Parser → AST<br>        ↓<br>   Interpreter (Ignition) → Bytecode<br>        ↓<br>   Profiler watches for &quot;hot code&quot;<br>        ↓<br>   Optimizing Compiler (TurboFan) → Native Machine Code</pre><h3>Inline Caching</h3><p>JS is dynamic, but V8 <strong>assumes things stay the same</strong> for speed.</p><pre>function greet(user) {<br>  return &quot;Hello &quot; + user.name;<br>}</pre><p>V8 caches the user.name lookup. If later you change the object’s shape — it has to throw away that optimization.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f25a020c4df3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Javascript Beyond Basics]]></title>
            <link>https://medium.com/@pkcofficial24/javascript-beyond-basics-76c3c9f90bb3?source=rss-799771c4d075------2</link>
            <guid isPermaLink="false">https://medium.com/p/76c3c9f90bb3</guid>
            <dc:creator><![CDATA[Prince Kumar Chaurasia]]></dc:creator>
            <pubDate>Fri, 18 Apr 2025 19:34:16 GMT</pubDate>
            <atom:updated>2025-04-18T19:37:35.835Z</atom:updated>
            <content:encoded><![CDATA[<p>Learning the depth of JavaScript</p><blockquote><strong>Handling Multiple Promises</strong></blockquote><p><strong>Promise.all()</strong> — Wait for <em>all</em> or fail fast</p><pre>const p1 = Promise.resolve(&quot;🚀 Rocket&quot;);<br>const p2 = Promise.resolve(&quot;🪐 Planet&quot;);<br><br>Promise.all([p1, p2])<br>  .then((results) =&gt; console.log(results)) // [&quot;🚀 Rocket&quot;, &quot;🪐 Planet&quot;]<br>  .catch((err) =&gt; console.log(&quot;Error:&quot;, err));</pre><pre>const p1 = Promise.resolve(&quot;✅ Success&quot;);<br>const p2 = Promise.reject(&quot;💥 Boom!&quot;);<br><br>Promise.all([p1, p2])<br>  .then(console.log)<br>  .catch(console.error); // 💥 Boom!</pre><p><strong>Promise.allSettled()</strong> — Wait for <em>all</em>, no matter what</p><p>That is your chill friend. It waits for everything, and gives you the status of each.</p><pre>const p1 = Promise.resolve(&quot;🍕&quot;);<br>const p2 = Promise.reject(&quot;💣&quot;);<br><br>Promise.allSettled([p1, p2]).then(console.log);</pre><pre>[<br>  { status: &quot;fulfilled&quot;, value: &quot;🍕&quot; },<br>  { status: &quot;rejected&quot;, reason: &quot;💣&quot; }<br>]</pre><p><strong><em>Promise</em></strong><strong>.race()</strong> — First to finish wins</p><pre>const p1 = new Promise(res =&gt; setTimeout(() =&gt; res(&quot;🐢 Slow&quot;), 2000));<br>const p2 = new Promise(res =&gt; setTimeout(() =&gt; res(&quot;🐇 Fast&quot;), 500));<br><br>Promise.race([p1, p2]).then(console.log); // 🐇 Fast</pre><p><strong>Promise.any()</strong> — First successful one wins</p><pre>const p1 = Promise.reject(&quot;❌&quot;);<br>const p2 = Promise.resolve(&quot;✅ Success&quot;);<br>const p3 = Promise.resolve(&quot;🎉 Another one&quot;);<br><br>Promise.any([p1, p2, p3]).then(console.log); // ✅ Success</pre><p>See you in next one.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=76c3c9f90bb3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Javascript Beyond Basics]]></title>
            <link>https://medium.com/@pkcofficial24/javascript-beyond-basics-bd40d788065b?source=rss-799771c4d075------2</link>
            <guid isPermaLink="false">https://medium.com/p/bd40d788065b</guid>
            <dc:creator><![CDATA[Prince Kumar Chaurasia]]></dc:creator>
            <pubDate>Fri, 18 Apr 2025 19:21:38 GMT</pubDate>
            <atom:updated>2025-04-18T19:21:38.739Z</atom:updated>
            <content:encoded><![CDATA[<p>Learning the depth of JavaScript</p><p>Callbacks sometimes get messy : The Callback hell</p><pre>getData(function(data) {<br>  getMoreData(data, function(moreData) {<br>    getEvenMoreData(moreData, function(finalData) {<br>      // 😵 Welcome to Callback Hell<br>    });<br>  });<br>});</pre><p>So, JS introduced Promises.</p><p>A <strong>Promise</strong> is an object representing the eventual result (or failure) of an async operation.</p><pre>const promise = new Promise((resolve, reject) =&gt; {<br>  // async operation<br>  let success = true;<br>  <br>  if (success) {<br>    resolve(&quot;Done!&quot;);<br>  } else {<br>    reject(&quot;Failed!&quot;);<br>  }<br>});</pre><p>You can use .then() and .catch() to handle the result.</p><pre>promise<br>  .then((result) =&gt; console.log(result))<br>  .catch((error) =&gt; console.log(error));</pre><h3>Promise States</h3><ol><li><strong>Pending</strong> — not resolved or rejected yet</li><li><strong>Fulfilled</strong> — resolve() called</li><li><strong>Rejected</strong> — reject() called</li></ol><p>Once fulfilled or rejected, it’s <strong>settled</strong>.</p><p>Instead of chaining .then() calls, we can use async and await in a function.</p><pre>async function getUser() {<br>  try {<br>    const data = await fetchUser();<br>    console.log(data);<br>  } catch (err) {<br>    console.log(&quot;Error:&quot;, err);<br>  }<br>}</pre><p>Behind the scenes:</p><ul><li>await pauses the function until the promise resolves</li><li>It resumes only when the data is available</li><li>It’s <strong>syntactic sugar</strong> over promises but way cleaner</li></ul><p>You can also use multiple awaits.</p><pre>async function loadData() {<br>  const user = await fetchUser();<br>  const posts = await fetchPosts(user);<br>  console.log(posts);<br>}</pre><p>Each await happens sequentially. You can also run them in parallel with:</p><pre>const [user, posts] = await Promise.all([fetchUser(), fetchPosts()]);</pre><p>Things to remember while using async &amp; await</p><ul><li>You <strong>must</strong> use await inside an async function.</li><li>Always wrap with try/catch for error handling.</li><li>await blocks only <strong>within the function</strong>, not the whole program.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bd40d788065b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Javascript Beyond Basics #3]]></title>
            <link>https://medium.com/@pkcofficial24/javascript-beyond-basics-3-2a7f579dd25f?source=rss-799771c4d075------2</link>
            <guid isPermaLink="false">https://medium.com/p/2a7f579dd25f</guid>
            <dc:creator><![CDATA[Prince Kumar Chaurasia]]></dc:creator>
            <pubDate>Thu, 17 Apr 2025 19:39:56 GMT</pubDate>
            <atom:updated>2025-04-17T19:39:56.798Z</atom:updated>
            <content:encoded><![CDATA[<p>Learning the depth of JavaScript</p><p>Closure : The feature of JavaScript that feels like MAGIC</p><p>A Closure happens when a function remembers the variables from its outer space, even after the outer function is done running.</p><pre>function outer() {<br>  let count = 0;<br><br>  function inner() {<br>    count++;<br>    console.log(count);<br>  }<br><br>  return inner;<br>}<br><br>const counter = outer(); // outer runs once, returns inner<br><br>counter(); // 1<br>counter(); // 2<br>counter(); // 3</pre><p>Here, when <strong>outer()</strong> function is run, it returns a function called <strong>inner().</strong></p><p>You can clearly see that we haven’t defined count variable inside inner() function, still on running inner() function via counter variable in which it is stored after running outer, it <strong>remembers</strong> the <strong>count variable</strong> defined inside outer() function and print the count.</p><p>This is called Closure;</p><p>Closures are used in:<br> → Data privacy</p><p>→ Function Factories</p><p>→ Async Logic [ event handlers, setTimeout etc ]</p><pre>function createButton(label) {<br>  let clicks = 0;<br><br>  return function clickHandler() {<br>    clicks++;<br>    console.log(`${label} clicked ${clicks} times`);<br>  };<br>}<br><br>const like = createButton(&quot;Like&quot;);<br>like(); // Like clicked 1 times<br>like(); // Like clicked 2 times</pre><p><strong>See you in 4rth one.</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2a7f579dd25f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Javascript Beyond Basics #2]]></title>
            <link>https://medium.com/@pkcofficial24/javascript-beyond-basics-2-44f0679f8b6c?source=rss-799771c4d075------2</link>
            <guid isPermaLink="false">https://medium.com/p/44f0679f8b6c</guid>
            <dc:creator><![CDATA[Prince Kumar Chaurasia]]></dc:creator>
            <pubDate>Thu, 17 Apr 2025 19:29:47 GMT</pubDate>
            <atom:updated>2025-04-17T19:29:47.328Z</atom:updated>
            <content:encoded><![CDATA[<p>Learning depth of JavaScript</p><p><strong>Scope :</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/270/1*WnQUY8oXW8pt8BVA464fng.jpeg" /></figure><p>Scope is the area in which a variable is accessible.</p><p>There are broadly 3 scopes :</p><p>→ Global Scope</p><p>→ Function Scope</p><p>→ Block Scope [ with let &amp; const ]</p><pre>let a = 10; // global scope<br><br>function test() {<br>  let b = 20; // function scope<br>  if (true) {<br>    let c = 30; // block scope<br>    console.log(a, b, c); // ✅ has access to all<br>  }<br><br>  console.log(a, b); // access to a &amp; b, but not ❌ c (block scope)<br>}<br><br>test();<br>console.log(a); // ✅<br>console.log(b); // ❌ not accessible</pre><p>→ Global scope variable can be accessed any where in the file.</p><p>→ Function scope variable can only be accessed inside the function itself.</p><p>→ Block scope variable can only be accessed inside the block in which it is defined.</p><p>This is known as <strong>lexical scoping</strong> where JS determines scope by the position of code when it is written, not when it is run.</p><p><strong>See you in part 3.</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=44f0679f8b6c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Javascript Beyond Basics #1]]></title>
            <link>https://medium.com/@pkcofficial24/javascript-beyond-basics-1-b373a30cb057?source=rss-799771c4d075------2</link>
            <guid isPermaLink="false">https://medium.com/p/b373a30cb057</guid>
            <dc:creator><![CDATA[Prince Kumar Chaurasia]]></dc:creator>
            <pubDate>Thu, 17 Apr 2025 19:20:25 GMT</pubDate>
            <atom:updated>2025-04-17T19:20:25.737Z</atom:updated>
            <content:encoded><![CDATA[<p>Learning the depth of javascript.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/0*Qp9o-uhYiKTenoEJ" /></figure><p>What happens when you write:</p><pre>console.log(&quot;Hello World&quot;);</pre><ol><li><strong>Javascript engine [ V8 ]</strong></li></ol><p>Your browser ( chrome ) or Node Js uses Javascript Engine V8 to run your code.</p><p>Internally it :</p><p>→ parses the code,</p><p>→ compiles it into low-level code ( machine instructions ) and then run it.</p><p><strong>2. Execution Context Creation</strong></p><p>The Execution Context is like a container that holds all the info that your JS code needs to run — Variables, Functions etc.</p><p>When you run the file :</p><ol><li>JS makes an Global Execution Context [ GEC ]</li><li>It runs in two faces :</li></ol><p>Memory creation phase known as HOISTING.</p><p><strong>3. Execution Phase</strong></p><p>→ <em>Memory execution phase :</em></p><p>In this phase, JS goes through your code and allocates memory for all variables and functions.</p><p>→ Variables defined as var are set to undefined.</p><p>→ Functions are fully stored in memory</p><p><strong>→ let</strong> and <strong>const</strong> are hoisted too, but in a weird limbo called <strong>Temporal Dead Zone, </strong>which means, the variable is hoisted but not accessible yet.</p><p>→ <em>Code Execution Phase :</em></p><p>JS runs your code line by line, assigning values and calling functions.</p><p><strong>4. The Call Stack</strong></p><p>JS runs in a single thread which means it can only do one thing at a time. It keeps track of what it is doing using a call stack.</p><pre>function greet() {<br> console.log(&quot;Hi&quot;);<br>}<br><br>function sayHello() {<br> greet();<br>}<br><br>sayHello();</pre><p>Internally :</p><p>→ GEC is created.</p><p>→ sayHello() is called → new execution context is pushed to the call stack.</p><p>→ greet() is called inside sayHello() → another context is pushed.</p><p>→ console.log(“Hi”) runs</p><p>Stack pops as each function ends.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/394/1*KetyGkQ1_HUgKPAt8EsMAw.png" /></figure><p><strong>Follow for 2nd part.</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b373a30cb057" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Quick Sort using Recursion]]></title>
            <link>https://medium.com/@pkcofficial24/quick-sort-using-recursion-8bc07e2db30e?source=rss-799771c4d075------2</link>
            <guid isPermaLink="false">https://medium.com/p/8bc07e2db30e</guid>
            <dc:creator><![CDATA[Prince Kumar Chaurasia]]></dc:creator>
            <pubDate>Wed, 12 Mar 2025 12:32:27 GMT</pubDate>
            <atom:updated>2025-03-12T12:32:27.631Z</atom:updated>
            <content:encoded><![CDATA[<p><em>“Quick Sort: Pick a pivot, split and sort — speed meets elegance in the art of ordering.”</em> ⚡🚀</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AEiigG5Nrf1bR4qKBm7llA.jpeg" /></figure><p>After understanding <a href="https://medium.com/@pkcofficial24/merge-sort-using-recursion-b7afb87f42fa">MERGE SORT</a> in the previous article, let’s move on to another sorting algorithm → QUICK SORT.</p><p>Like merge sort, we will use recursion to implement quick sort. But first let’s understand the basic idea about it.</p><h3>Basic Idea</h3><p>The basic idea behind quick sort is to pick a pivot and place it at its correct position such that all the elements on the left are smaller and those on right are greater than the pivot.</p><h3>Code Implementation</h3><pre>    private static void quickSort(int[] arr, int low, int high) {<br>        if(low &gt;= high){<br>            return;<br>        }<br><br>        int start = low, end = high, mid = start + ( end - start ) / 2, pivot = arr[mid];<br><br>        while (start &lt;= end){<br>            while (arr[start] &lt; pivot){<br>                start++;<br>            }<br><br>            while (arr[end] &gt; pivot){<br>                end--;<br>            }<br><br>            if(start &lt;= end){<br>                int temp = arr[start];<br>                arr[start] = arr[end];<br>                arr[end] = temp;<br>                start++;<br>                end--;<br>            }<br>        }<br><br>        quickSort(arr, low, end);<br>        quickSort(arr, start, high);<br><br>    }</pre><p>In the quickSort function we take the array to be sorted, low [ which is first index of the array ] and hight [ which is end of the array ].</p><p>Then, we define the base case, i.e., when the low crosses or is equal to high, return.</p><p>Further we initialise start, end, mid and pivot variables. The start begins at the low index, end at the high index, and mid is calculated as the middle element. We choose the element at the mid position as our pivot.</p><p>The main sorting happens in the while loop that runs until start and end cross each other:</p><ol><li>The first inner while loop finds elements that are greater than or equal to the pivot from the left side.</li><li>The second inner while loop finds elements that are less than or equal to the pivot from the right side.</li><li>When both loops find elements that are out of place (a larger element on the left and a smaller element on the right), we swap them.</li><li>After swapping, we increment start and decrement end to continue the process.</li></ol><p>Once the outer while loop completes, we have partitioned the array around the pivot. Elements smaller than the pivot are on the left, and elements greater than the pivot are on the right.</p><p>Now, we recursively apply quickSort on the two partitions:</p><ol><li>From low to end (left partition)</li><li>From start to high (right partition)</li></ol><p>Let’s analyze the time complexity:</p><ul><li>Best and Average case: O(n log n)</li><li>Worst case: O(n²) — occurs when the array is already sorted or when we always pick the smallest/largest element as pivot</li></ul><p>Space complexity is O(log n) for the recursion stack in the average case, and O(n) in the worst case.</p><p>Quick Sort is generally faster than other O(n log n) sorting algorithms like Merge Sort because it has better locality of reference and doesn’t require additional space for merging. However, it’s not stable (doesn’t preserve the relative order of equal elements) and its worst-case performance can be problematic for certain inputs.</p><p>In this way, you can sort an unsorted array using Quick Sort algorithm.</p><p>Thanks for reading this far, hit applaud if you find it helpful understanding the algorithm.<br>See you in the next one.</p><p>BYE 👋</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8bc07e2db30e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Merge Sort using Recursion]]></title>
            <link>https://medium.com/@pkcofficial24/merge-sort-using-recursion-b7afb87f42fa?source=rss-799771c4d075------2</link>
            <guid isPermaLink="false">https://medium.com/p/b7afb87f42fa</guid>
            <dc:creator><![CDATA[Prince Kumar Chaurasia]]></dc:creator>
            <pubDate>Mon, 10 Mar 2025 18:28:42 GMT</pubDate>
            <atom:updated>2025-03-10T18:28:42.535Z</atom:updated>
            <content:encoded><![CDATA[<p><em>“Merge Sort: Divide, conquer, and elegantly combine — because sorting should be as seamless as it is efficient.”</em> 🚀</p><p>We have seen quite a few sorting algorithm like — Bubble Sort, Insertion Sort &amp; Selection Sort. Now that we have learned <a href="https://medium.com/@pkcofficial24/understanding-recursion-in-depth-for-beginners-cc239de9ffbc">Recursion</a>, let’s use it to understand merge sort as well.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pBI00LbDevTaSxJtBuvkQw.png" /></figure><h3>Basic Idea</h3><p>The merge sort algorithm is a type of divide and conquer approach where we divide an unsorted array till the array contain one element then merge it to get the sorted array. Refer to the given image above.</p><p>So, we use recursion to keep dividing the array till one index and then merge it using a separate function.</p><p>If you look closely in the given image above, you can see that on the left side the two elements are divided into individual array then sorted and merged into an array of length two. Same on the right side and finally we have our sorted array.</p><h3><strong>Code Implementation</strong></h3><pre>private static int[] mergeSort( int[] arr ) {<br>  if(arr.length == 1){<br>    return arr;<br>  }<br><br>  int mid = arr.length / 2;<br><br>// copyOfRange: makes a copy of the original array with the defined range [ exclusive ]<br>  int[] left = mergeSort(Arrays.copyOfRange(arr, 0, mid));<br>  int[] right = mergeSort(Arrays.copyOfRange(arr, mid, arr.length));<br><br>  return merge(left, right);<br><br> }</pre><pre>private  static int[] merge(int[] first, int[] second){<br>  int[] mix = new int[first.length + second.length];<br><br>  int i = 0, j = 0, k = 0;<br><br>  while(i &lt; first.length &amp;&amp; j &lt; second.length){<br>    if(first[i] &lt; second[j]){<br>      mix[k] = first[i];<br>      i++;<br>    }else{<br>      mix[k] = second[j];<br>      j++;<br>    }<br>      k++;<br>  }<br><br>  while(i &lt; first.length){<br>      mix[k] = first[i];<br>      i++;<br>      k++;<br>  }<br><br>  while(j &lt; second.length){<br>      mix[k] = second[j];<br>      j++;<br>      k++;<br>  }<br><br>  return mix;<br>}</pre><p>As we see in the mergeSort() function, it takes an array and we divide the array in half every time until the base case hits, which is when the length of the array is one, it returns the array.</p><p>So, eventually left contains one element, and right contains one element and then we merge it using the merge() function.</p><p>Inside merge() function we are creating an array with the sorted elements and returning the sorted array to the mergeSort() function.</p><p><strong>Logic while merging :</strong></p><p>Let’s say you have 2 elements in each [ left &amp; right ] arrays that we have got from the recursive mergeSort() function call.</p><p>In the merge() function, we initiate an empty array of combined length of the left and the right array.</p><p>We put a pointer <strong>[ i ]</strong> at start of the left array and<strong> [ j ]</strong> at the start of right array. While [ <strong>i ] </strong>is less than than the length of the left array and [ <strong>j ] </strong>is less than the length of right array we will run the loop. [ <strong>k</strong> ] points to the indices of the new &amp; empty array we created.</p><p>If the element at i index of the left array is less than the j index of the right array, than we put it at k index of new array and move the i pointer forward otherwise we put the element at j index at k index of the new array and move the j pointer forward.</p><p>After each insertion in the new array we move the k pointer forward. In this way we are able to sort and merge two arrays.</p><p>At last, if elements are still left in either of the arrays [ left &amp; right ], we sequentially put those element in our new array, because those must be sorted previously.</p><p>In this way we get the desired sorted array using the merge sort algorithm.</p><p>Try yourself, debug and dry run to understand it better. Hope you understood something from the article.</p><p>Hit applaud if you liked it 👏.</p><p>Thanks for reading this far.</p><p>See you in the next one.</p><p>BYE 👋</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b7afb87f42fa" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Infinite Scroll using React & JavaScript — [Asked in Machine Coding Rounds of Big Tech Companies]]]></title>
            <link>https://medium.com/@pkcofficial24/infinite-scroll-using-react-javascript-asked-in-machine-coding-rounds-of-big-tech-companies-ae39e15ef28a?source=rss-799771c4d075------2</link>
            <guid isPermaLink="false">https://medium.com/p/ae39e15ef28a</guid>
            <category><![CDATA[tech]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[development]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Prince Kumar Chaurasia]]></dc:creator>
            <pubDate>Sat, 08 Mar 2025 20:38:33 GMT</pubDate>
            <atom:updated>2025-03-08T20:38:33.715Z</atom:updated>
            <content:encoded><![CDATA[<h3>Infinite Scroll using React &amp; JavaScript — [Asked in Machine Coding Rounds of Big Tech Companies]</h3><p><strong>“Scrolling Instagram/Twitter for hours? Ever wondered why the content never ends?”</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hIqA_3mkWt9CLmJ4JXOGYw.jpeg" /></figure><p>Infinite scrolling is a popular interview topic that may be asked in your next front-end interview. Let’s implement it using React &amp; JavaScript.</p><p>To understand how infinite scrolling works, you first need to understand these three things:</p><ol><li>scrollTop</li><li>clientHeight</li><li>scrollHeight</li></ol><h3>scrollTop</h3><p>JavaScript provides a method to determine how much content has scrolled out of view. You can simply use the event object to retrieve this value.</p><pre>const scrollTop = event.target.scrollTop</pre><h3>clientHeight</h3><p>clientHeight represents the visible portion of the screen. For example, if your screen height is 400px, then clientHeight will be 400px.</p><pre>const clientHeight = event.target.clientHeight</pre><h3>scrollHeight</h3><p>scrollHeight represents the total height of the rendered content. For example, if your content&#39;s total height is 1400px, then scrollHeight will be 1400px..</p><pre>const scrollHeight = event.target.scrollHeight</pre><p>Now we can play with these three things to implement infinite scrolling.</p><h3>Main Idea</h3><p>The most basic and simplest way to implement infinite scroll is to load more data as the user approaches the bottom of the content.</p><p>So, we check when the user is near the bottom, preload the data, and render it. This creates the illusion of infinite scrolling, making it seem like the content never ends.</p><h3>Code Implementation</h3><p>Below is a dummy component in react with an array in which we keep adding elements as we get to the bottom of the rendered content.</p><pre>const THRESHOLD = 20;<br><br>const InfiniteScroll = () =&gt; {<br>    const [data, setData] = useState(new Array(40));<br>    const [loading, setLoading] = useState(false);<br>    <br>    const loadMoreData = () =&gt; {<br>        loading(true);<br>        setTimeout(()=&gt;{<br>           setData(prev =&gt; [...prev, ...new Array(10));<br>           loading(false);<br>        }, 1000)<br>    };<br><br>    const handleScroll = (event) =&gt; {<br>         const {scrollTop, scrollHeight, clientHeight} = event.target;<br>         const remainingContentHeight = scrollHeight - ( scrollTop + clientHeight);<br>          <br>         if(remainingContent &lt; THRESHOLD &amp;&amp; !loading){<br>            loadMoreData();<br>         }<br>    }<br><br>    return (<br>      &lt;div onScroll={handleScroll}&gt;<br>        { <br>          data.map((item, index)=&gt;(<br>             &lt;div id=&quot;row&quot; key={index}&gt;<br>               {index + 1}<br>             &lt;/div&gt;<br>          ))<br>        }<br>      &lt;/div&gt;<br>    )<br>}</pre><h3>Code Explanation</h3><p>THRESHOLD defines the point at which more content should be loaded. As we scroll, the remaining content height decreases. When remainingContentHeight falls below THRESHOLD, we trigger the loading of more content.&quot;</p><p>We used setTimeout to simulate the delay that occurs while fetching data from an API.</p><p>The loading state is used to ensure that the loadMoreData() function is called only when loading is false, preventing unnecessary data fetching while a request is already in progress.</p><p>And that’s how we can easily implement INFINITE SCROLLING. 😃</p><p>Thanks for reading this far. Hope you understood and learnt something. Please give an applaud if you liked the article. See you in the next one.</p><p>BYE 👋</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ae39e15ef28a" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>