<?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 Akash on Medium]]></title>
        <description><![CDATA[Stories by Akash on Medium]]></description>
        <link>https://medium.com/@akashspotdar7?source=rss-4f8380c94d27------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*2SsuE3hrUGKmq0tUtKWcbQ.jpeg</url>
            <title>Stories by Akash on Medium</title>
            <link>https://medium.com/@akashspotdar7?source=rss-4f8380c94d27------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 08:20:54 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@akashspotdar7/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[The Math of Comparisons in Binary Search]]></title>
            <link>https://medium.com/@akashspotdar7/the-math-of-comparisons-in-binary-search-eea993b09356?source=rss-4f8380c94d27------2</link>
            <guid isPermaLink="false">https://medium.com/p/eea993b09356</guid>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[time-complexity]]></category>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[dsa-courses]]></category>
            <category><![CDATA[binary-search]]></category>
            <dc:creator><![CDATA[Akash]]></dc:creator>
            <pubDate>Sat, 10 Feb 2024 20:05:50 GMT</pubDate>
            <atom:updated>2024-05-23T12:09:50.335Z</atom:updated>
            <content:encoded><![CDATA[<p>Demystifying the time complexity behind the infamous Binary search</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/798/1*li29KU7kzOgRjyzkQsMZFg.png" /><figcaption>Joy: Alright, alright Virus ji, I’ll use Binary search</figcaption></figure><blockquote><strong>Intro</strong></blockquote><p>Ok, in simple terms Binary search is a <em>common algorithm </em>used to find an element in the list.<br>Why is it called Binary search? What is binary about it?<br>The answer is ‘Divide and rule’ — because it divides the list into two halves at every iteration to narrow down the search space until the list is narrowed down to the possible locations to just one. The one 😜.<br>But, but.. there’s a catch, Binary search can work only on sorted list of elements.</p><blockquote><strong>Problem statement</strong></blockquote><p>Imagine you are playing a guessing game where someone has picked a number between 1 to 100, and you need to guess it. One way is to guess each number sequentially, instead you can use binary search approach.</p><ol><li>First Guess: You guess the middle number, 50. The person indicates whether the correct number is higher or lower.</li><li>Second Guess: Based on the feedback, you adjust your guess to the middle of the new range. If the correct number is lower, you guess 25; if higher, you guess 75.</li><li>Repeat: You continue narrowing down the range with each guess, always targeting the middle of the remaining possible numbers.</li></ol><p>By repeatedly halving the possible range, you efficiently approach the correct answer much faster than if you guessed sequentially. This process is similar to how binary search works, making it a practical approach in situations where you need to systematically narrow down possibilities.</p><p>For example if the target number is 30, linear search takes 30 times of comparisons. Whereas Binary search takes only ~5 comparisons.</p><p>Now what if data is humongous, say 1000000, thats a million items list.<br>Linear search will take length times in the worst case scenario. But Binary search takes only ~19 times. That’s some stupendous speed there.<br>How are we getting the number of comparison here. <br>Let’s explore that with time complexity.<br>…</p><blockquote><em>Deriving Time Complexity</em></blockquote><p>Time complexity ⏳is a gauge used in computer science to analyse the efficiency of an algorithm. It factors in the input size and yields the time taken. It provides an estimate of the worst-case runtime of an algorithm.<br>Analysing the time complexity of an algorithm helps in understanding how its performance scales as the input size increases.</p><p>Finding the number of comparisons it takes for a binary search algorithm.<br>The below picture shows:</p><ul><li>N : list size</li><li>K : level of comparisons</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/630/1*ws66EgRGOgnnd9UrJLBRDA.png" /><figcaption>Halving the search space by each iteration</figcaption></figure><p>If you look closely, in each iteration we divide the list into 2 for comparison until the size reduces to possible size of one.<br>Hence..<br><em>1 = (N/2^K)</em><br>Let’s do some simple math.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/438/1*kVCC_DN1h6cWU_NCzACo2g.png" /><figcaption>Where N is the size of the list</figcaption></figure><p>We will usually test the efficiency of an algorithm for the worst case scenario. Let’s see how the comparisons look for linear and binary search in the below table.</p><pre>   Size                     10  128  1,024 1,000,000,000<br><br>  Linear Search  O(n)       10  128  1,024 1,000,000,000<br>  Binary Search O(log2n)     3    7  10    30</pre><p>Now this seems quite clear that Binary search works bonkers!<br>Hope by now, my readers have a good understanding how Binary Search works under the hood. <br>Okay, enough talk…</p><blockquote><strong>Show me the code. 👨🏻‍💻</strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jCTKCSKWTOa6v43GZDld5g.jpeg" /><figcaption>Move to the left 👈</figcaption></figure><pre>function binarySearch(arr, target) {<br>    let left = 0;<br>    let right = arr.length - 1;<br><br>    while (left &lt;= right) {<br>        const mid = Math.floor((left + right) / 2);<br><br>        if (arr[mid] === target) {<br>            return mid; // Element found, return its index<br>        } else if (arr[mid] &lt; target) {<br>            left = mid + 1; // Target is in the right half<br>        } else {<br>            right = mid - 1; // Target is in the left half<br>        }<br>    }<br><br>    return -1; // Element not found<br>}<br><br>// Example usage:<br>const sortedArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];<br>const targetValue = 7;<br>const result = binarySearch(sortedArray, targetValue);<br><br>if (result !== -1) {<br>    console.log(`Element ${targetValue} found at index ${result}`);<br>} else {<br>    console.log(`Element ${targetValue} not found in the array`);<br>}</pre><blockquote><strong>Conclusion</strong></blockquote><p>So cool!<br>We just learnt a lot about Binary Search algorithm. It is simple yet efficient and powerful way of search operation.</p><p>Few pointers on stone<br>🚅 Binary search works only with sorted lists<br>🚅 Imp: Binary search can work order agnostic.<br>Bonus: <em>Add your thoughts on order agnostic Binary Search in the comments.</em></p><p><strong>Follow… </strong><a href="https://medium.com/@akashspotdar7"><strong>me</strong></a><strong> for more such insightful articles✍️</strong></p><p>Kudos reader to reach this far in my blog. 😊<br>Thanks for reading, clap clap! 👏<br>Will be back with another interesting article.</p><p>Happy coding, happy learning 👨🏻‍💻</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=eea993b09356" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Communication in Google Chrome browser extension]]></title>
            <link>https://medium.com/whatfix-techblog/communication-in-google-chrome-browser-extension-50f68d99e85c?source=rss-4f8380c94d27------2</link>
            <guid isPermaLink="false">https://medium.com/p/50f68d99e85c</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[browser-extension]]></category>
            <category><![CDATA[chrome-extension]]></category>
            <category><![CDATA[extension]]></category>
            <dc:creator><![CDATA[Akash]]></dc:creator>
            <pubDate>Wed, 15 Dec 2021 05:50:54 GMT</pubDate>
            <atom:updated>2021-12-15T05:50:54.059Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FIiGmYOeGJM9wpKpU5CTOw.jpeg" /></figure><p>Communication within different parts of the Chrome browser extension is one of the primary aspects in browser extension development. This post is about passing messages between Content script, Background and content(Browser action). Simply speaking how does a script in the scope of a website talk to the extension or the other way around.</p><p>Before we get to the topic I want to outline a fairly common communication model that is used. All event handlers receive an event object containing a data property, it includes data sent as part of the message.The event model is mostly around onMessage and postMessage.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c65822abb6cc7367e56bbed7994e1459/href">https://medium.com/media/c65822abb6cc7367e56bbed7994e1459/href</a></iframe><p>Extensions have several components to perform various actions. Most common ones are the content script and background script.<br>Let’s have a quick look at these shall we.</p><p><strong>Background script</strong> — This script runs regardless of the website. Scripts run in the background of the extension and keep running until the extension is unloaded from the browser. More info on the google documentation <a href="https://developer.chrome.com/docs/extensions/mv3/service_workers/">here</a>.<br><strong>Action Script </strong>— Local JavaScript file for the extension DOM. More on Action script <a href="https://developer.chrome.com/docs/extensions/mv3/getstarted/#user_interface">here</a><strong>.<br>Content script </strong>— this is JavaScript injected by extension into the web page. The injection can be targeted to one or more web pages. This script acts like a broker between the web site DOM and the actions of the background script. More info <a href="https://developer.chrome.com/docs/extensions/mv3/content_scripts/">here</a>.</p><blockquote>Note: I have used Manifest V2 along with this article and all the nuances are in point of view to V2.</blockquote><p>Since Chrome extensions are event-driven because of their architecture, once the injected scripts have access to the page’s variables and functions, they can pass it to the content script.<br>The content script then passes these objects to the background page.<br>And finally, the action script is able to call onto the background page using the Extension API and pass it to the Extension DOM.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/780/1*G9TvrjF9F0l1PEknWP4TVw.png" /></figure><p>Let’s now build a simple extension who’s job is to print the performance metrics of the web application. The extension will receive performance metrics from a global object on the window and pass onto the action script to render it on the DOM, the extension DOM.</p><p>Starting with the content script, let’s start coding. Inject content script into the web application DOM.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6085242a1f754566c803669d37fe1563/href">https://medium.com/media/6085242a1f754566c803669d37fe1563/href</a></iframe><p>Content script now should also start listening to messages. Essentially from the script running in the web app context. Making content script as the foreground communicator or mediator in the extension architecture.</p><p>When a message is received by content script from the custom/injected script, a quick validation for the event source is run and next simply we use the chrome extension’s runtime API to send the data further to the background script of the extension.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/79b09eb682a5954c713158dff4bd7cd0/href">https://medium.com/media/79b09eb682a5954c713158dff4bd7cd0/href</a></iframe><p>Now let’s have a look on how the injected custom script looks like which runs in the context on the web application layer. We know that this script has access to window functions and variables. So let’s get hands on the required object from the page’s context.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cfb5882667fb75cb02d22901b311b3c9/href">https://medium.com/media/cfb5882667fb75cb02d22901b311b3c9/href</a></iframe><p>The performance data procured is now sent via post message. Custom script uses <strong>window.postMessage </strong>function for safe communication. To observe the interim changes to the performance data we will go about adding a time interval. Although this is ironic to use an interval to observe performance metrics, hence is not suggested in the real world.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/40e2fe07a987ef034b130840938d1a14/href">https://medium.com/media/40e2fe07a987ef034b130840938d1a14/href</a></iframe><p>We are now at the Background Script.<br>The background script listens for any message broadcast by the content script via Chrome Runtime API, essentially on receiving the event we can use <strong>tabId</strong> as an identifier per tab and map the essential data to. The background script’s window object will store details in memory.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a841d2339c8fa9ed62996add5cf189db/href">https://medium.com/media/a841d2339c8fa9ed62996add5cf189db/href</a></iframe><p>Action script is what finally reads the data so as to append to the extension DOM and render performance metrics to the user about the web application.</p><p>In the popup script, the performance data is retrieved from the background script context. Using <strong>getBackgroundPage</strong>, and at an interval the current page’s details are fetched.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8fdec6827bc0cf5fa207d568314a715e/href">https://medium.com/media/8fdec6827bc0cf5fa207d568314a715e/href</a></iframe><p>This way we will finally receive the essential performance data required to render it to the user over the extension’s DOM i.e., the popup page. UI styling and rendering over the extension DOM can be tweaked as per need. Now all that we have is the metrics to render.</p><p><strong>Final thoughts</strong></p><p>Chrome extension or be it any modern day browser extension is a powerful tool provided by the browser and by leveraging the concept of messaging between the application and the extension it holds the potential to address complex problems.</p><p>I had an opportunity to work on communication in the chrome extension for one of our use cases at Whatfix. Hopefully, this simple walk-through saves time and infers knowledge on the concept.<br>I would highly recommend building an extension and playground with the config and code.</p><p>Ciao for now, Happy coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=50f68d99e85c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/whatfix-techblog/communication-in-google-chrome-browser-extension-50f68d99e85c">Communication in Google Chrome browser extension</a> was originally published in <a href="https://medium.com/whatfix-techblog">WhatfixEngineeringBlog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>