<?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 Anand Singh on Medium]]></title>
        <description><![CDATA[Stories by Anand Singh on Medium]]></description>
        <link>https://medium.com/@anandsingh372?source=rss-b594636b2dbe------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*XFcVcy0x-38-k1URl9V-lw.jpeg</url>
            <title>Stories by Anand Singh on Medium</title>
            <link>https://medium.com/@anandsingh372?source=rss-b594636b2dbe------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 27 Nov 2026 18:37:45 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@anandsingh372/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[Refill Your Cup: “The Art of Putting Yourself First”]]></title>
            <link>https://medium.com/@anandsingh372/refill-your-cup-the-art-of-putting-yourself-first-97617c6eee1c?source=rss-b594636b2dbe------2</link>
            <guid isPermaLink="false">https://medium.com/p/97617c6eee1c</guid>
            <category><![CDATA[self-care]]></category>
            <category><![CDATA[motivation]]></category>
            <category><![CDATA[self-care-tips]]></category>
            <category><![CDATA[self-improvement]]></category>
            <dc:creator><![CDATA[Anand Singh]]></dc:creator>
            <pubDate>Fri, 04 Apr 2025 20:02:52 GMT</pubDate>
            <atom:updated>2025-04-05T07:36:01.052Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Refill Your Cup: “The Art of Putting Yourself First”</strong></p><p>After onboarding the flight with my mom, air hostess started giving the flight instructions. My mom being a keen observer highlighted the safety measure where air hostess instructs to put on Oxygen mask when cabin pressure decreases. She quotes “Please put oxygen mask on yourself first before assisting others”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/698/1*ewmtzW63oCWXdcNwZd9gFA.png" /></figure><p>We travel frequently yet fail to understand the hidden meaning of above instruction. Above line clearly indicates if you don’t put yourself first and collapse somehow, then your ability to able help others perishes as well.</p><p>This shows why self love and keeping yourself a priority matters.</p><p>Fascinated by this I scouted into different professions and found out clear evidence of how important it is to keep yourself healthy wealthy and wise:</p><p><strong>1. Emergency Response Protocols</strong>: In disaster or emergency situations — whether it’s a sinking ship or a fire — trained professionals are taught to secure their own safety (like donning lifejackets or protective gear) before they attempt to rescue others. This protocol ensures that helpers remain safe and capable of providing assistance.</p><p><strong>2. Healthcare and Caregiving:</strong> In medical and caregiving settings, professionals are encouraged to manage their own health and stress levels. Burnout and fatigue can compromise the quality of care, so taking breaks and self-care are essential practices in fields like nursing and counseling.</p><p><strong>3. Military and Tactical Training:</strong> In military training, the importance of self-protection is paramount. Soldiers are taught to secure their own safety and ensure they’re fit for action before attempting to support their comrades in challenging or dangerous situations.</p><p><em>And so on……</em></p><p>I can go on with list of examples but the list is never ending. No wonder they say <em>“Health is Wealth”.</em></p><p>Apart from this if I talk about <strong><em>Philosophical and cultural wisdom</em></strong>, one of the most important writing <em>“Chanakya Neeti”</em> emphasize securing one’s own stability and resources before extending help.</p><p>In this ancient book an example is given where</p><blockquote>“If you want to secure your future” -&gt; “Secure Wealth”</blockquote><p>But if option comes</p><blockquote>“between wealth and your wife” -&gt; “choose your wife”.</blockquote><blockquote>However before all that take care of yourself because unless you do that you will not be able to provide for family or secure wealth for your future.</blockquote><p>The saying <em>“you can’t pour from an empty cup”</em> is a popular metaphor. It reminds us that unless we take care of our own physical, mental, and emotional needs, we won’t have the capacity to care for others effectively.</p><p>This was not a motivating post but a reminder to emphasise on your health and prioritise your well-being.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=97617c6eee1c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Bloom Filters ?? What are they and How are they different from a HashMap/HashSet ?]]></title>
            <link>https://medium.com/@anandsingh372/bloom-filters-what-are-they-and-how-are-they-different-from-a-hashmap-hashset-a630bd79fa61?source=rss-b594636b2dbe------2</link>
            <guid isPermaLink="false">https://medium.com/p/a630bd79fa61</guid>
            <category><![CDATA[counting-bloom-filter]]></category>
            <category><![CDATA[hashmap]]></category>
            <category><![CDATA[hashset]]></category>
            <category><![CDATA[bloom-filter]]></category>
            <dc:creator><![CDATA[Anand Singh]]></dc:creator>
            <pubDate>Wed, 05 Mar 2025 17:50:12 GMT</pubDate>
            <atom:updated>2025-03-05T17:50:12.179Z</atom:updated>
            <content:encoded><![CDATA[<h3>Bloom Filters ?? What are they and How are they different from a HashMap/HashSet ?</h3><p>Suppose you are given a stream of Numbers/Texts etc. and with every insertion you you need to determine whether each newly inserted element has appeared earlier in the stream. Which is the first Data Structure that comes to your mind??</p><p>Obviously, a <strong>HashMap</strong> (or <strong>HashSet</strong>) is the natural choice here. Since hash-based lookups provide <strong>O(1)</strong> average-time complexity for insertions and membership checks, it’s an efficient solution for this problem.</p><blockquote><strong>Q. What will be your straight forward approach ?</strong></blockquote><blockquote><strong>A.</strong> Just keep on adding elements in a HashMap and with every insertion check whether current element already exists in the HashMap in O(1) time. If exists then return true, else return false.</blockquote><p><strong>Let’s</strong> understand it with an example.</p><p>Suppose you have a stream of <strong>3 elements:</strong></p><p>New HashMap = {}</p><p><strong>1st element</strong>: <em>Alice</em> (not present in HashMap) -&gt; <em>insert and return false.</em></p><p>HashMap = {Alice}</p><p><strong>2nd element:</strong> <em>Bob</em> (not present in HashMap) -&gt; <em>insert and return false.</em></p><p>HashMap = {Alice, Bob}</p><p><strong>3rd element:</strong> <em>Alice</em> (present in HashMap) -&gt; <em>ignore insertion and return true.</em></p><p>Pretty Straight Forward. Right?</p><p>Now suppose there comes a stream of <strong>1 million unique elements.</strong> You will have to store all the elements in this HashMap. Suppose each elements is of <strong>64 bytes.</strong> So in total you will require :</p><blockquote>64*1 million bytes =~ 64 MB</blockquote><p>Now lets do some modifications in the requirements. We have to check availability but we have following leverages with us:</p><blockquote>1. It is okay if sometimes you say an element is in the set when it is not. (False positives are acceptable).</blockquote><blockquote>2. If you say an element is not in the set, it is definitely not in the set. (No False negatives).</blockquote><p>Definitely <strong>HashMap</strong> can still come in handy but a disadvantage with HashMap is <em>it will take a good amount of memory.</em></p><p>What if I told you that you could achieve the same functionality while using just a few megabytes of memory — less than one-eighth of what a HashMap would require for 1 million elements ?</p><p>This magic is possible by none other than the famous <strong>Bloom Filters</strong>.</p><p>Below is the typical PHD level definition of Bloom Filters:</p><blockquote>A <strong>Bloom filter</strong> is a <strong>probabilistic data structure</strong> that efficiently tests whether an element is in a set. It is space-efficient but allows for false positives (i.e., it may indicate that an element is in the set when it is not) while guaranteeing no false negatives.</blockquote><h3><strong>How does it work?</strong></h3><ul><li>A Bloom filter uses a <strong>bit array</strong> of size m, initially set to all 0s.</li><li>It uses <strong>k independent hash functions</strong> that each map an input element to one of the mmm positions in the bit array.</li><li><strong>Insertion:</strong> When an element is added, all k hash functions are applied to it, and the corresponding k bits in the bit array are set to 1.</li><li><strong>Lookup:</strong> To check if an element is in the set, the same hash functions are applied. If all the bits corresponding to the hashes are <strong>1</strong>, the element is <em>probably</em> in the set; if any bit is <strong>0</strong>, the element is <em>definitely not</em> in the set.</li></ul><p>Let’s go through a <strong>step-by-step example</strong> to understand how a <strong>Bloom filter</strong> works.</p><h3>Example</h3><h4>Scenario:</h4><p>You want to use a Bloom filter to check if a username is already taken in a system.</p><h4>Setup:</h4><ul><li>Suppose the bit array size is <strong>m = 10</strong> (indexed from 0 to 9).</li><li>We use <strong>k = 3</strong> hash functions.</li><li>We want to insert three usernames: &quot;Alice&quot;, &quot;Bob&quot;, and &quot;Charlie&quot;.</li></ul><h3>Step 1: Initialize the Bloom Filter</h3><p>We start with a bit array of size <strong>10</strong>, all set to 0:</p><pre>[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]</pre><h3>Step 2: Insert Elements</h3><p>Each element is inserted using <strong>3 hash functions</strong>, which return indices in the bit array.</p><h4>Insert “Alice”</h4><ul><li>Assume the three hash functions return indices <strong>2, 5, 7</strong>.</li><li>Set these indices to 1:</li></ul><pre>[0, 0, 1, 0, 0, 1, 0, 1, 0, 0]</pre><h4>Insert “Bob”</h4><ul><li>Assume hash functions return indices <strong>1, 3, 7</strong>.</li><li>Set these indices to 1:</li></ul><pre>[0, 1, 1, 1, 0, 1, 0, 1, 0, 0]</pre><h4>Insert “Charlie”</h4><ul><li>Assume hash functions return indices <strong>3, 6, 9</strong>.</li><li>Set these indices to 1:</li></ul><pre>[0, 1, 1, 1, 0, 1, 1, 1, 0, 1]</pre><h3>Step 3: Lookup (Check Membership)</h3><p>To check if an element exists, apply the hash functions and see if all corresponding bits are 1.</p><h4>Check “Alice”</h4><ul><li>Hash functions return <strong>2, 5, 7</strong>.</li><li>Bits <strong>2, 5, 7</strong> are all 1 → <strong>Probably in the set</strong> ✅</li></ul><h4>Check “David” (Not inserted)</h4><ul><li>Assume hash functions return <strong>2, 4, 8</strong>.</li><li>Bit at index <strong>4</strong> is 0 → <strong>Definitely not in the set</strong> ❌</li></ul><h4>Check “Bob”</h4><ul><li>Hash functions return <strong>1, 3, 7</strong>.</li><li>Bits <strong>1, 3, 7</strong> are all 1 → <strong>Probably in the set</strong> ✅</li></ul><h4>False Positive Case</h4><ul><li>If we check a random username &quot;Eve&quot;, hash functions might return <strong>1, 6, 7</strong>.</li><li>Since these bits are 1, the Bloom filter may <strong>falsely indicate</strong> that &quot;Eve&quot; exists in the set, even though it was never inserted.</li></ul><p>Wow that is fantastic, we have just found another method for checking availability of elements in a set. But now the question arises <strong>how much memory will Bloom Filters consume for 1 million elements ?</strong></p><p>Well you must have guessed, the answer definitely depends on the number value of <strong>m (number of bits).</strong></p><p>I won’t get into derivation of how the value of <strong>m (number of bits) </strong>and <strong>k (number of hash functions) </strong>is decided, I will just go forward and write down there formulas.</p><p>The <strong>number of bits (m)</strong> in a Bloom filter is chosen based on the expected <strong>number of elements (n)</strong> and the <strong>desired false positive probability (p)</strong>.</p><blockquote>Formula for m (Number of Bits):</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/246/1*6ylw_mzQNc3QukgegNCjaw.png" /></figure><p>Where:</p><ul><li>n = Expected number of elements</li><li>p = Desired false positive probability</li><li>ln⁡2 ≈ 0.693</li></ul><p>The <strong>optimal number of hash functions (k)</strong> in a Bloom filter depends on the size of the <strong>bit array (m)</strong> and the <strong>number of expected elements (n)</strong> to minimize false positives.</p><blockquote>Formula for Optimal k:</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/246/1*aaTGvkM-fqe5TyuELT0Y6Q.png" /></figure><p>Where:</p><ul><li>m = size of the bit array (total bits)</li><li>n = number of expected elements</li><li>ln⁡(2) ≈ 0.693</li></ul><p>Now we know the formula for both m and k. So let’s quickly go forward and calculate the memory that Bloom Filters will require for 1 million elements.</p><p><strong>Let’s calculate m:</strong></p><blockquote>m = -(n ln p)/(ln 2)²</blockquote><blockquote>let p = 1% = 0.01 =&gt; Desired false positive rate</blockquote><blockquote>m = − 1000000 * ln(0.01) / (0.693)²</blockquote><blockquote>m = − 1000000 * (-4.605)/ (0.693)²</blockquote><blockquote>m ~= 9588776 bits</blockquote><blockquote>m = 1.2 MB</blockquote><p>Wow, from <strong>64 MB to 1.2 MB,</strong> thats fantastic. With this I think we both are on same page and are convinced that if saving space is an <strong>ART</strong> then <em>Bloom Filters </em>are the <strong>ARTISTS.</strong></p><h3><strong>Conclusion</strong></h3><ul><li>A <strong>Bloom filter can never be 100% correct</strong> because of collisions in hashing.</li><li>However, <strong>by increasing m, you can make false positives arbitrarily small</strong>.</li><li>If <strong>100% accuracy is required</strong>, use <strong>a HashSet, Cuckoo Filter, or Perfect Hashing instead</strong>.</li></ul><h3>Comparison: Bloom Filter vs. HashMap</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/748/1*naPk-0Flqvr3TMxMkGNS5w.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TxwfgoWFSdrm7AIqjxEadw.png" /></figure><p>With this I would like to end this article. Do clap if you found this informative and follow for more such interesting ones.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a630bd79fa61" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Java Threads: Why do we call start() method and not run() to create a thread.]]></title>
            <link>https://medium.com/@anandsingh372/java-threads-why-do-we-call-start-method-and-not-run-to-create-a-thread-51a86199f34f?source=rss-b594636b2dbe------2</link>
            <guid isPermaLink="false">https://medium.com/p/51a86199f34f</guid>
            <category><![CDATA[threads]]></category>
            <category><![CDATA[threading-in-java]]></category>
            <category><![CDATA[multithreading]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Anand Singh]]></dc:creator>
            <pubDate>Sun, 02 Mar 2025 16:13:48 GMT</pubDate>
            <atom:updated>2025-03-02T16:17:04.408Z</atom:updated>
            <content:encoded><![CDATA[<h3>Java Threads: Why do we call start() method and not run() to create a thread. Difference between start() and run()</h3><p>If you’re familiar with Java, you’ve likely heard of multithreading. To truly understand multithreading, we first need to grasp how a thread is created in Java. While many of you may already know this, I’d like to briefly summarize it here, as it will lay the foundation for our deeper exploration into the difference between start() and run().</p><p>There are two very important classes in Java, <strong>Thread</strong> class and <strong>Runnable</strong> interface.</p><p><strong>Thread</strong> class has <em>start()</em> method.</p><p><strong>Runnable</strong> interface has <em>run()</em> method.</p><p>Following are the steps which creates a thread in JAVA. Along with the steps I would also be showing you the code example which I have written.</p><ol><li>Create a class which implements <strong>Runnable</strong> interface.</li><li>Implement the <strong>run()</strong> method of Runnable interface (You don’t have a choice here, you will be forced to implement the run method once you implement the Runnable interface).</li></ol><pre>public class Counter implements Runnable {<br><br>    private int count = 0;<br><br>    @Override<br>    public void run() {<br>        for(int i=1; i&lt;=1000000; i++) {<br>                count++;<br>        }<br>    }<br><br>    public int getCount() {<br>        return count;<br>    }<br><br>    public void setCount(int count) {<br>        this.count = count;<br>    }<br>}</pre><p>In the run method, I have written a simple <em>for</em> loop that will increment the <em>count</em> variable to 1 million.</p><p>3. Go to your <em>main</em> function and create an object of <em>Counter</em> class which we have just created in <strong>Step 2</strong>.</p><p>4. Create an object of <strong>Thread</strong> class and pass the <em>Counter’s</em> object created in <strong>Step 3</strong> in Thread class’ <em>constructor</em>.</p><p>5. Invoke the start method of the <strong>Thread</strong> class.</p><pre>public class Main {<br>    public static void main(String[] args) {<br><br>        Counter counter = new Counter();<br><br>        Thread thread = new Thread(counter);<br>        thread.start();<br><br>        /* You can write other stuffs here as well, which will be executed <br>          in main thread */<br><br>    }<br>}</pre><p>Woah, you have successfully created a thread now.</p><p>Now there are two points worth noting here.</p><ol><li><em>start()</em> method internally calls the <em>run()</em> method.</li><li>As soon as you write <em>thread.start() </em>, a new thread is created (which is separate from the main() thread and all the code inside the run() method starts executing in that separate thread). Diagram 1.1</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dtTQn2ezqW_nYTRwRuPkPQ.png" /><figcaption>Diagram 1.1</figcaption></figure><p>So both of the above code runs in parallel.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/716/1*zbYHlE7vb9eNZoi9TJkgUA.png" /></figure><p>Relax, he’s not angry at you — he’s angry at me, and rightly so. He’s asking a completely valid question:</p><p>“If the start() method internally calls run(), then why in the world wouldn&#39;t you just call run() directly?&quot;</p><p>Why make things unnecessarily complicated?</p><p>Calm down, kid, calm down. Let me explain.</p><p>Let’s take a deep dive into the start() method of the Thread class.</p><pre>    public synchronized void start() {<br>        /**<br>         * This method is not invoked for the main method thread or &quot;system&quot;<br>         * group threads created/set up by the VM. Any new functionality added<br>         * to this method in the future may have to also be added to the VM.<br>         *<br>         * A zero status value corresponds to state &quot;NEW&quot;.<br>         */<br>        if (threadStatus != 0)<br>            throw new IllegalThreadStateException();<br><br>        /* Notify the group that this thread is about to be started<br>         * so that it can be added to the group&#39;s list of threads<br>         * and the group&#39;s unstarted count can be decremented. */<br>        group.add(this);<br><br>        boolean started = false;<br>        try {<br>            start0();<br>            started = true;<br>        } finally {<br>            try {<br>                if (!started) {<br>                    group.threadStartFailed(this);<br>                }<br>            } catch (Throwable ignore) {<br>                /* do nothing. If start0 threw a Throwable then<br>                  it will be passed up the call stack */<br>            }<br>        }<br>    }<br><br>    private native void start0();</pre><p>If you look at start() method closely, you will see it calls start0() method. And start0() is a native method. If you dont’t know what a native method is below is the definition.</p><p><strong>NATIVE METHOD:</strong> A <strong>native method</strong> in Java is a method that is implemented in a language other than Java, typically <strong>C or C++</strong>, and is executed using the Java Native Interface (<strong>JNI</strong>). These methods are implemented in external <strong>shared libraries</strong> (e.g., .dll, .so, .dylib) that are loaded into the JVM at runtime.</p><h4>Q. What code is present inside the start0() method ?</h4><p><strong>A.</strong> As soon as start0() method is invoked, OS creates a native thread (New thread which was created in <strong>Diagram 1.1</strong> earlier). Then OS invokes JVM to execute the thread’s <em>run() </em>method. Below is the diagram (<strong>Diagram 1.2</strong>) illustrating this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5rSjhJ8S2PjFwcSn6mybTw.png" /><figcaption>Diagram 1.2</figcaption></figure><p>So <em>start0()</em>, contains the damn code of actually making the native thread.</p><p>Now I think you have understood the point of me explaining all the above things in detail.</p><p>So if I call the <em>run()</em> method directly it will create following disaster of events.</p><ol><li><em>start0()</em> will not be invoked.</li><li>If <em>start0() </em>is not invoked then no new thread will be created.</li><li>If no new thread is created then there is <strong>no way our code inside <em>run()</em> method will run in parallel to <em>main()</em> thread.</strong></li><li>So ultimately <strong>no multithreading.</strong></li></ol><p>So <strong>Diagram 1.1</strong> changes to below <strong>Diagram 1.3</strong> if we directly call the run() method.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/388/1*-BTf5hr0YJRP2DixdeSHXg.png" /><figcaption>Diagram 1.3</figcaption></figure><p>Damn, only a single main thread is running and no multithreading.</p><h4>I hope now you have enough understanding of why it is recommended to call start() method to create a thread and not the run() method, even though internally start() calls run().</h4><p>Thank you. Don’t forget to clap if you found this article helpful. Anymore explanation needed then please feel free to bombard the comment section below.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=51a86199f34f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>