<?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 Avinash Yadav on Medium]]></title>
        <description><![CDATA[Stories by Avinash Yadav on Medium]]></description>
        <link>https://medium.com/@avinashyadav16?source=rss-570103f5c627------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*KF2pa1Sk2fXE7y5G7ttxwg.png</url>
            <title>Stories by Avinash Yadav on Medium</title>
            <link>https://medium.com/@avinashyadav16?source=rss-570103f5c627------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 15:51:04 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@avinashyadav16/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[Mastering the Boyer-Moore Algorithm: The Secret Behind Lightning-Fast Text Search]]></title>
            <link>https://medium.com/@avinashyadav16/mastering-the-boyer-moore-algorithm-the-secret-behind-lightning-fast-text-search-a873f920049f?source=rss-570103f5c627------2</link>
            <guid isPermaLink="false">https://medium.com/p/a873f920049f</guid>
            <category><![CDATA[search-algorithm]]></category>
            <category><![CDATA[boyer-moore-algorithm]]></category>
            <category><![CDATA[cpp]]></category>
            <category><![CDATA[sad]]></category>
            <dc:creator><![CDATA[Avinash Yadav]]></dc:creator>
            <pubDate>Fri, 21 Aug 2026 08:49:49 GMT</pubDate>
            <atom:updated>2026-08-21T09:42:01.068Z</atom:updated>
            <content:encoded><![CDATA[<p>Have you ever wondered how our text editor finds a word in a massive file almost instantly? Or how grep searches through thousands of lines in the blink of an eye?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Xp6mJhhjnO_BQr9qGcnsLg.png" /></figure><p>The secret often lies in an elegant algorithm published in 1977 by Robert Boyer and J Strother Moore. Their brilliant insight was counterintuitive: instead of comparing characters left-to-right like we naturally would, what if we searched backwards?</p><p>This simple idea led to one of the most practical string-searching algorithms, <strong>Boyer-Moore algorithm</strong>, ever created the one that can actually skip over large chunks of text without examining every character.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*ri9ZI4v6wKoeQOK3NxvSDw.png" /></figure><h3>The Lightbulb Moment (Or Rather, the Darkness)</h3><p>Picture this: It’s 2 AM, you’re three coffees deep, and your string matching function is crawling through a 50MB log file like a snail on sleeping pills to search for the a particular word or a pattern in the log file.</p><p>First instinct? Blame the hardware. <br>Second instinct? Question our life choices. <br>Finally the third instinct? Google “why is my string search so damn slow.”</p><h3>What Makes Boyer-Moore Special?</h3><p>Boyer-Moore achieves something remarkable: <strong>sublinear time complexity</strong>. In the best case, it runs in <em>O(n/m)</em> time, where n is the text length and m is the pattern length. This means the longer your search pattern, the faster the algorithm becomes!</p><p>Imagine searching for “<em>antidisestablishmentarianism</em>” in a document. While a naive algorithm checks every position, Boyer-Moore might skip 28 characters at a time. That’s the magic.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NdFRYgSnywQa3cyhxizbUQ.png" /></figure><h3>What’s Actually Wrong With How We Usually Search?</h3><p>Let’s define our challenge clearly:</p><blockquote><strong><em>Given:</em></strong><em> A text </em><em>T of length </em><em>n and a pattern </em><em>P of length </em><em>m</em></blockquote><blockquote><strong><em>To Find:</em></strong><em> All occurrences of </em><em>P in </em><em>T</em></blockquote><p><strong>Example:</strong></p><pre>Text:    &quot;MISSISSIPPI&quot;<br>Pattern: &quot;ISSI&quot;<br>Output:  Pattern found at index 1 and index 4</pre><p>Let me show you what I was doing wrong (and you probably are too):</p><figure><img alt="Naive Search Approach" src="https://cdn-images-1.medium.com/max/1024/1*Clcv8Xd9SLRPXQv7Uw-EZg.png" /><figcaption><em>Naive Search </em>Approach</figcaption></figure><p>This looks innocent, right?</p><blockquote><strong>Wrong!</strong></blockquote><p>This is O(n*m) complexity (potentially checking every character multiple times!) masquerading as “simple and clean code.”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/242/1*ZSB2OUHfuFTEmbYE4rKYtg.png" /></figure><h3>Enter Boyer-Moore: The Game Changer { The “Aha!” Moment: Why Search Backwards? }</h3><p>Here’s where Boyer-Moore gets clever — almost annoyingly clever.</p><p>The algorithm uses two key ideas (we’ll focus on the most important one):</p><p><strong>1. Bad Character Rule:</strong> When we find a mismatch, we look at the mismatched character in the text. If it doesn’t appear in our pattern at all, we can skip the entire pattern length forward. If it does appear, we can align it with its rightmost occurrence in the pattern.</p><p><strong>2. Good Suffix Rule (optional):</strong> When we’ve matched some suffix of the pattern but then hit a mismatch, we can look for another occurrence of that matched suffix and align to it.</p><p>For practical purposes, the Bad Character rule alone gives us most of the performance gains. That’s what we’ll implement today.</p><p><strong>Three key insights:</strong></p><ol><li><strong>Search from right to left</strong> (counterintuitive but powerful!)</li><li><strong>Skip unnecessary comparisons</strong> using intelligent heuristics</li><li><strong>Learn from mismatches</strong> to make big jumps forward</li></ol><p>Instead of comparing strings left-to-right like a normal human being, it goes <strong>right-to-left</strong>.</p><p>“That’s stupid,” I thought initially. “How does that help?”</p><p>Oh, sweet summer child version of me. Here’s why it’s brilliant:</p><p>When we start from the RIGHT side of the pattern, a mismatch tells us WAY more information.</p><p>Let me show you:</p><pre>Text:    &quot;HELLO WORLD HELLO THERE&quot;<br>Pattern: &quot;THERE&quot;</pre><p><strong>Naive approach:</strong></p><pre>Step 1: H vs T? Nope. Shift by 1.<br>Step 2: E vs T? Nope. Shift by 1.<br>Step 3: L vs T? Nope. Shift by 1.<br>... (this continues forever)</pre><p><strong>Boyer-Moore:</strong></p><pre>Step 1: Check from right: H vs E? Nope.<br>        Wait... is &#39;H&#39; even in &quot;THERE&quot;? <br>        NO! Skip the ENTIRE pattern!<br>        Jump ahead by 5 positions!</pre><p>Mind. Blown. 🤯</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/250/1*blRKu68d10TO0cC6iM5xCw.png" /></figure><h3>The Secret Sauce: The Bad Character Rule</h3><p>This is where the magic happens. It is the heart of Boyer-Moore’s efficiency. Boyer-Moore asks a simple question when it finds a mismatch:</p><blockquote><em>“Hey, does this mismatched character appear anywhere in my pattern?”</em></blockquote><ul><li><strong>If NO:</strong> Skip the entire pattern past this character</li><li><strong>If YES:</strong> Align the pattern so this character matches its last occurrence</li></ul><p><em>Let me visualize this because it’s cooler than it sounds:</em></p><blockquote><strong>Case 1:</strong> Character NOT in pattern → Skip past it entirely</blockquote><blockquote><strong>Case 2:</strong> Character EXISTS in pattern → Align it with its rightmost occurrence</blockquote><figure><img alt="Bad Character Heuristic" src="https://cdn-images-1.medium.com/max/1024/1*bzylzgsXcQlzZ4C8BXM-iA.png" /><figcaption>Bad Character Heuristic</figcaption></figure><p><em>The two cases that make Boyer-Moore zoom</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*imdS0u5IT0fZ6mK6C71owg.jpeg" /></figure><h3>Let’s Build It</h3><p>Alright, enough theory. Let’s write some actual code. I’m going to walk you through this like I’m explaining it to past-me.</p><h3>The Bad Character Table</h3><p>The preprocessing step is crucial. We create a table that stores the rightmost position of each character in the pattern and build a lookup table.</p><blockquote>To keep things clear and practical, the code below implements the <em>Bad Character heuristic only</em>. This is actually what most real-world applications use, as it provides the majority of the performance benefit while being much simpler to understand and implement.</blockquote><blockquote>The full Boyer-Moore algorithm includes a second rule called the “Good Suffix” heuristic, which can help in specific edge cases. We’ll touch on that later, but for now, know that you’re learning the most practical version that powers tools like grep and text editors.</blockquote><figure><img alt="Bad Character Table" src="https://cdn-images-1.medium.com/max/1024/1*1_MYK0mibYimkinhUo6v-g.png" /><figcaption>Bad Character Table</figcaption></figure><p><strong>Output:</strong></p><pre>Searching for: &#39;ISSI&#39;<br>In text: &#39;MISSISSIPPI RIVER FLOWS IN MISSISSIPPI&#39;</pre><pre>Pattern found at positions: 1 4 28 31</pre><p><em>Walking through the algorithm on “MISSISSIPPI”</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VeEdJhdmkJzH28R4YCE7Gg.png" /></figure><h3>Real-World LeetCode Battle: Problem #28</h3><p>Let’s tackle a classic interview problem with our new superpower:</p><p><a href="https://leetcode.com/problems/find-the-index-of-the-first-occurrence-in-a-string/"><strong>LeetCode 28: Find the Index of the First Occurrence in a String</strong></a></p><blockquote>Given two strings needle and haystack, return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.</blockquote><p><strong>Example 1:</strong></p><pre>Input: haystack = &quot;sadbutsad&quot;, needle = &quot;sad&quot;<br>Output: 0<br>Explanation: &quot;sad&quot; occurs at index 0 and 6.<br>The first occurrence is at index 0, so we return 0.</pre><p><strong>Example 2:</strong></p><pre>Input: haystack = &quot;leetcode&quot;, needle = &quot;leeto&quot;<br>Output: -1<br>Explanation: &quot;leeto&quot; did not occur in &quot;leetcode&quot;, so we return -1.</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/252/1*4MwfrFdtsz9LPk8sYudMPg.png" /></figure><pre>#include &lt;iostream&gt;<br>#include &lt;string&gt;<br>#include &lt;unordered_map&gt;<br>using namespace std;<br><br>class Solution<br>{<br>public:<br>    int strStr(string haystack, string needle)<br>    {<br>        if (needle.empty())<br>        {<br>            return 0;<br>        }<br><br>        int n = haystack.length();<br>        int m = needle.length();<br><br>        if (m &gt; n)<br>        {<br>            return -1;<br>        }<br><br>        // Build bad character table<br>        unordered_map&lt;char, int&gt; badChar;<br>        for (int i = 0; i &lt; m; i++)<br>        {<br>            badChar[needle[i]] = i;<br>        }<br><br>        int shift = 0;<br><br>        while (shift &lt;= n - m)<br>        {<br>            int j = m - 1;<br><br>            // Match from right to left<br>            while (j &gt;= 0 &amp;&amp; needle[j] == haystack[shift + j])<br>            {<br>                j--;<br>            }<br><br>            if (j &lt; 0)<br>            {<br>                return shift; // Found! Return first occurrence<br>            }<br><br>            // Calculate shift using bad character heuristic<br>            char mismatchedChar = haystack[shift + j];<br><br>            int charLastPos = (badChar.find(mismatchedChar) != badChar.end())<br>                                  ? badChar[mismatchedChar]<br>                                  : -1;<br><br>            shift += max(1, j - charLastPos);<br>        }<br><br>        return -1; // Not found<br>    }<br>};<br><br>int main()<br>{<br>    Solution sol;<br><br>    cout &lt;&lt; sol.strStr(&quot;sadbutsad&quot;, &quot;sad&quot;) &lt;&lt; endl;  // Output: 0<br>    cout &lt;&lt; sol.strStr(&quot;leetcode&quot;, &quot;leeto&quot;) &lt;&lt; endl; // Output: -1<br>    cout &lt;&lt; sol.strStr(&quot;hello&quot;, &quot;ll&quot;) &lt;&lt; endl;       // Output: 2<br>    cout &lt;&lt; sol.strStr(&quot;aaaaa&quot;, &quot;bba&quot;) &lt;&lt; endl;      // Output: -1<br><br>    // The tricky one that catches naive implementations:<br>    cout &lt;&lt; sol.strStr(&quot;mississippi&quot;, &quot;issip&quot;) &lt;&lt; endl; // Output: 4<br><br>    return 0;<br>}</pre><pre>0<br>-1<br>2<br>-1<br>4</pre><p><strong>Why this beats the naive approach on LeetCode:</strong></p><ul><li><strong>Time Complexity:</strong> O(n/m) best case (vs O(n*m) naive)</li><li><strong>Space Complexity:</strong> O(k) where k is alphabet size</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/248/1*-7Nqhpf2Cdz8EnzZFdkd8w.png" /></figure><h3>LeetCode #686 (Repeated String Match)</h3><p>This one’s trickier, but Boyer-Moore handles it elegantly:</p><p><a href="https://leetcode.com/problems/repeated-string-match/"><strong>LeetCode 686: Repeated String Match</strong></a></p><p>Given two strings a and b, return <em>the minimum number of times you should repeat string </em>a<em> so that string</em> b <em>is a substring of it</em>. If it is impossible for b​​​​​​ to be a substring of a after repeating it, return -1.</p><p><strong>Notice:</strong> string &quot;abc&quot; repeated 0 times is &quot;&quot;, repeated 1 time is &quot;abc&quot; and repeated 2 times is &quot;abcabc&quot;.</p><p>This problem is a perfect showcase for Boyer-Moore’s flexibility. The challenge is determining how many times we need to repeat string `a` for string `b` to become a substring.</p><p>The key insight: we need at most `<strong><em>ceiling(len(b) / len(a)) + 1</em></strong>` repetitions. Here’s why:</p><p><strong>1. Minimum repetitions:</strong> At least enough to cover the length of `b`<br><strong>2. Plus one:</strong> For cases where `b` starts near the end of a repetition of `a`</p><p>Let’s see it in action:</p><p><strong>Example 1:</strong></p><pre>Input: a = &quot;abcd&quot;, b = &quot;cdabcdab&quot;<br>Output: 3<br>Explanation: We return 3 because by repeating a three times &quot;abcdabcdabcd&quot;, b is a substring of it.</pre><p><strong>Example 2:</strong></p><pre>Input: a = &quot;a&quot;, b = &quot;aa&quot;<br>Output: 2</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/252/1*4MwfrFdtsz9LPk8sYudMPg.png" /></figure><pre>#include &lt;iostream&gt;<br>#include &lt;string&gt;<br>#include &lt;vector&gt;<br>using namespace std;<br><br>class Solution<br>{<br>public:<br>    int repeatedStringMatch(string a, string b)<br>    {<br>        // Calculate minimum repetitions needed to cover b&#39;s length<br>        int minReps = (b.length() + a.length() - 1) / a.length();<br><br>        // Try with minimum repetitions<br>        string repeated = repeatString(a, minReps);<br>        if (boyerMooreSearch(repeated, b) != -1)<br>        {<br>            return minReps;<br>        }<br><br>        // Try with one extra repetition (for wrap-around cases)<br>        // Example: a=&quot;abc&quot;, b=&quot;cab&quot; needs &quot;abcabc&quot;<br>        repeated += a;<br>        if (boyerMooreSearch(repeated, b) != -1)<br>        {<br>            return minReps + 1;<br>        }<br><br>        // If not found by now, it&#39;s impossible<br>        return -1;<br>    }<br><br>private:<br>    string repeatString(const string &amp;s, int times)<br>    {<br>        string result;<br><br>        // Preallocate for efficiency<br>        result.reserve(s.length() * times);<br><br>        for (int i = 0; i &lt; times; i++)<br>        {<br>            result += s;<br>        }<br>        return result;<br>    }<br><br>    int boyerMooreSearch(const string &amp;text, const string &amp;pattern)<br>    {<br>        int n = text.length();<br>        int m = pattern.length();<br><br>        if (m &gt; n)<br>        {<br>            return -1;<br>        }<br><br>        // Build bad character table with vector for speed<br>        vector&lt;int&gt; badChar(256, -1);<br>        for (int i = 0; i &lt; m; i++)<br>        {<br>            badChar[(unsigned char)pattern[i]] = i;<br>        }<br><br>        int shift = 0;<br><br>        while (shift &lt;= n - m)<br>        {<br>            int j = m - 1;<br><br>            // Compare from right to left<br>            while (j &gt;= 0 &amp;&amp; pattern[j] == text[shift + j])<br>            {<br>                j--;<br>            }<br><br>            if (j &lt; 0)<br>            {<br>                return shift; // Match found!<br>            }<br><br>            char mismatchedChar = text[shift + j];<br>            int charLastPos = badChar[(unsigned char)mismatchedChar];<br><br>            shift += max(1, j - charLastPos);<br>        }<br><br>        return -1;<br>    }<br>};<br><br>// Test cases<br>int main()<br>{<br>    Solution sol;<br><br>    cout &lt;&lt; sol.repeatedStringMatch(&quot;abcd&quot;, &quot;cdabcdab&quot;) &lt;&lt; endl; // Output: 3<br>    // &quot;abcdabcdabcd&quot; contains &quot;cdabcdab&quot;<br><br>    cout &lt;&lt; sol.repeatedStringMatch(&quot;a&quot;, &quot;aa&quot;) &lt;&lt; endl; // Output: 2<br>    // &quot;aa&quot; contains &quot;aa&quot;<br><br>    cout &lt;&lt; sol.repeatedStringMatch(&quot;abc&quot;, &quot;wxyz&quot;) &lt;&lt; endl; // Output: -1<br>    // Impossible<br><br>    return 0;<br>}</pre><pre>3<br>2<br>-1</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/248/1*-7Nqhpf2Cdz8EnzZFdkd8w.png" /></figure><h3>Comparison with Other Algorithms</h3><p><strong><em>σ (sigma) represents the alphabet size (256 for extended ASCII)</em></strong></p><pre>| Algorithm              | Best Case | Average Case | Worst Case | Space    |<br>|------------------------|-----------|--------------|------------|----------|<br>| Naive Search           | O(n)      | O(n×m)       | O(n×m)     | O(1)     |<br>| KMP                    | O(n)      | O(n)         | O(n)       | O(m)     |<br>| Boyer-Moore (BC only)  | O(n/m)    | O(n/m)       | O(n×m)     | O(σ)     |<br>| Boyer-Moore (Full)     | O(n/m)    | O(n)         | O(n×m)     | O(m+σ)   |<br>| Rabin-Karp             | O(n+m)    | O(n+m)       | O(n×m)     | O(1)     |</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/248/1*-7Nqhpf2Cdz8EnzZFdkd8w.png" /></figure><h3>When Boyer-Moore Shines (And When It Doesn’t)</h3><p>Let me be real with you about when to use this:</p><h4>Use Boyer-Moore When:</h4><ol><li><strong>Searching English text</strong> (large alphabet = bigger skips)</li><li><strong>Long patterns</strong> (the longer, the better!)</li><li><strong>Random or natural data</strong> (not repetitive)</li><li><strong>You care about real-world performance</strong> over worst-case guarantees</li></ol><p>Example: Log file searching, text editors, grep-like tools</p><h4>Skip Boyer-Moore When:</h4><ol><li><strong>DNA sequences</strong> (only 4 characters: A, C, G, T)</li><li><strong>Very short patterns</strong> (&lt; 3 characters)</li><li><strong>Highly repetitive data</strong> (like “aaaaaaaaaa”)</li><li><strong>Guaranteed worst-case bounds needed</strong> (use KMP instead)</li></ol><h4>Real-World Applications</h4><ol><li><strong>Text Editors:</strong> Sublime Text, VS Code use Boyer-Moore variants for find operations</li><li><strong>Bioinformatics:</strong> DNA sequence matching (with modifications)</li><li><strong>Intrusion Detection Systems:</strong> Scanning network packets for malicious patterns</li><li><strong>Plagiarism Detection:</strong> Finding copied text in documents</li><li><strong>Data Mining:</strong> Pattern recognition in large datasets</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/248/1*-7Nqhpf2Cdz8EnzZFdkd8w.png" /></figure><h3>The “Good Suffix” Rule (Advanced Territory)</h3><p>I didn’t use it in my implementations above because honestly? For most real-world cases, bad character alone gets you 90% of the speedup. But if you’re curious…</p><p>The Good Suffix rule says: <em>“If I’ve already matched some characters (the ‘good suffix’), and then hit a mismatch, I can look for another occurrence of that suffix in my pattern and align to that.”</em></p><p>It’s more complex to implement, but can help in cases where bad character alone isn’t enough.</p><pre>// Good Suffix preprocessing<br>void buildGoodSuffixTable(const string &amp;pattern, vector&lt;int&gt; &amp;goodSuffix)<br>{<br>    int m = pattern.length();<br><br>    goodSuffix.resize(m + 1);<br><br>    vector&lt;int&gt; suffix(m + 1);<br><br>    // Compute suffix array<br>    suffix[m] = m + 1;<br>    int g = m, f = 0;<br><br>    for (int i = m - 1; i &gt;= 0; i--)<br>    {<br>        if (i &gt; g &amp;&amp; suffix[i + m - f] &lt; i - g)<br>        {<br>            suffix[i] = suffix[i + m - f];<br>        }<br>        else<br>        {<br>            if (i &lt; g)<br>                g = i;<br><br>            f = i;<br><br>            while (g &gt;= 0 &amp;&amp; pattern[g] == pattern[g + m - f])<br>            {<br>                g--;<br>            }<br><br>            suffix[i] = f - g;<br>        }<br>    }<br><br>    // Compute good suffix table<br>    for (int i = 0; i &lt;= m; i++)<br>    {<br>        goodSuffix[i] = m;<br>    }<br><br>    int j = 0;<br>    for (int i = m; i &gt; 0; i--)<br>    {<br>        if (suffix[i] == i)<br>        {<br>            while (j &lt; m - i)<br>            {<br>                if (goodSuffix[j] == m)<br>                {<br>                    goodSuffix[j] = m - i;<br>                }<br><br>                j++;<br>            }<br>        }<br>    }<br><br>    for (int i = 0; i &lt; m; i++)<br>    {<br>        goodSuffix[m - suffix[i]] = m - i;<br>    }<br>}</pre><p>Honestly? I’ve never needed it in practice. But it’s there if you want to squeeze out that last bit of performance.</p><h3>Common Pitfalls</h3><h4>Pitfall 1: Forgetting to Handle Empty Strings</h4><pre>// WRONG - Will crash<br>int strStr(string text, string pattern) <br>{<br>    // Immediately start searching...<br>}<br><br><br><br>// RIGHT<br>int strStr(string text, string pattern) <br>{<br>    if (pattern.empty()) return 0;  // Edge case!<br><br>    if (pattern.length() &gt; text.length()) return -1;<br><br>    // Now search...<br>}</pre><h4>Pitfall 2: Negative Shifts</h4><pre>// WRONG - Can shift backwards!<br>shift += j - badChar[text[shift + j]];<br><br><br><br>// RIGHT - Always positive<br>shift += max(1, j - badChar[text[shift + j]]);</pre><h4>Pitfall 3: Off-by-One in Bad Character Table</h4><pre>// WRONG - Misses last character<br>for (int i = 0; i &lt; m - 1; i++) <br>{  <br>    // Missing last char!<br>    badChar[pattern[i]] = i;<br>}<br><br><br>// RIGHT - Include ALL characters<br>for (int i = 0; i &lt; m; i++) <br>{<br>    badChar[pattern[i]] = i;<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/248/1*-7Nqhpf2Cdz8EnzZFdkd8w.png" /></figure><h3>The Bottom Line</h3><p>Boyer-Moore teaches us something important: <strong>Sometimes the “weird” approach is the right one.</strong></p><p>Searching backwards seems counterintuitive, but it leverages information in a way that left-to-right search can’t. And that’s beautiful.</p><p>Is it the perfect algorithm for everything? No. Will it make your code run faster in most real-world text searching scenarios? Absolutely.</p><p>So next time you’re writing string search code, remember:</p><blockquote><em>Life’s too short for O(n*m) complexity.</em></blockquote><p><strong>Questions? Corrections? Found a bug in my code?</strong> Drop a comment below!</p><h3>Quick Reference Card</h3><pre>// Quick Boyer-Moore Implementation<br>class QuickBM<br>{<br>    unordered_map&lt;char, int&gt; bc;<br>    string pat;<br><br>public:<br>    QuickBM(string p) : pat(p)<br>    {<br>        for (int i = 0; i &lt; pat.size(); i++)<br>        {<br>            bc[pat[i]] = i;<br>        }<br>    }<br><br>    vector&lt;int&gt; search(string txt)<br>    {<br>        vector&lt;int&gt; res;<br>        int n = txt.size(), m = pat.size(), s = 0;<br><br>        while (s &lt;= n - m)<br>        {<br>            int j = m - 1;<br><br>            while (j &gt;= 0 &amp;&amp; pat[j] == txt[s + j])<br>            {<br>                j--;<br>            }<br><br>            if (j &lt; 0)<br>            {<br>                res.push_back(s);<br>                s += (s + m &lt; n) ? m - (bc.count(txt[s + m]) ? bc[txt[s + m]] : -1) : 1;<br>            }<br>            else<br>            {<br>                s += max(1, j - (bc.count(txt[s + j]) ? bc[txt[s + j]] : -1));<br>            }<br>        }<br><br>        return res;<br>    }<br>};</pre><p>Save this. Print it. Tattoo it on your arm. Whatever helps you remember it.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a873f920049f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Bayesian Hyperparameter Tuning with Optuna: A Beginner-Friendly Guide]]></title>
            <link>https://medium.com/@avinashyadav16/bayesian-hyperparameter-tuning-with-optuna-a-beginner-friendly-guide-6a426f43c76e?source=rss-570103f5c627------2</link>
            <guid isPermaLink="false">https://medium.com/p/6a426f43c76e</guid>
            <category><![CDATA[fine-tuning]]></category>
            <category><![CDATA[hyperparameter-tuning]]></category>
            <category><![CDATA[optuna]]></category>
            <category><![CDATA[python-optuna]]></category>
            <dc:creator><![CDATA[Avinash Yadav]]></dc:creator>
            <pubDate>Mon, 22 Dec 2025 18:55:47 GMT</pubDate>
            <atom:updated>2025-12-22T18:55:47.583Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Hyperparameter tuning can make or break a machine learning model. Get it right, and the model shines. Get it wrong, and no amount of data cleaning or feature engineering will save us.</strong></p><p>FOR FULL CODE 👉 <a href="https://github.com/avinashyadav16/PyTorch/tree/main/10%20Hyperparameter%20Tuning%20of%20ANN%20Using%20Optuna">CLICK HERE</a> 🔗</p><p><em>In this post, we’ll explore </em><strong><em>Bayesian hyper-parameter tuning</em></strong><em> with </em><a href="https://optuna.org/"><em>Optuna</em></a><em>, a modern, lightweight optimization framework that makes finding the best hyperparameters both efficient and fun.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*PicyhDTbzA_PF3e7cMMwtA.jpeg" /><figcaption><a href="https://www.preferred.jp/en/news/pr20200114/">https://www.preferred.jp/en/news/pr20200114/</a></figcaption></figure><h3>Why Hyperparameter Tuning Matters</h3><p>Hyperparameters are the <em>knobs/settings</em> we <em>set/configure</em> before training a model (e.g., learning rate, number of trees, batch size, maximum depth, etc.). The right combination can vastly improve model performance.</p><p>But manually trying out values is exhausting. Traditional methods like <strong>grid search</strong> and <strong>random search</strong> waste time by exploring too many unpromising combinations.</p><p>Optuna is a modern, lightweight library that helps us do this efficiently with smart search strategies (including a Bayesian method called Tree-structured Parzen Estimator(TPE)) and nice features like pruning (early stopping of poor trials) and easy parallelization.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*ri9ZI4v6wKoeQOK3NxvSDw.png" /></figure><h3>1. Grid Search CV</h3><p>Grid Search CV is a hyperparameter optimization technique that <strong>systematically tests every possible combination</strong> of predefined hyperparameter values.</p><p><strong>How it works:</strong></p><ul><li>Define a fixed set of values for each hyperparameter.</li><li>Train the model using <strong>all possible combinations</strong> of these values.</li><li>Apply cross-validation to evaluate each combination.</li><li>Choose the hyperparameter set that produces the best cross-validation performance.</li></ul><p><strong>Advantages:</strong></p><ul><li><strong>Comprehensive:</strong> Evaluates all specified hyperparameter combinations.</li><li><strong>Simple to use:</strong> Easy to implement and interpret.</li></ul><p><strong>Disadvantages:</strong></p><ul><li><strong>Computationally intensive:</strong> The number of evaluations increases rapidly with more parameters.</li><li><strong>Poor scalability:</strong> Not suitable for large datasets or high-dimensional hyperparameter spaces.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GUd0D5dTYnEAuGajCANExQ.jpeg" /></figure><h3>2. Random Search CV</h3><p>Random Search CV is a hyperparameter optimization method that <strong>randomly samples combinations</strong> from a defined hyperparameter space rather than testing all possibilities.</p><p><strong>How it works:</strong></p><ul><li>Specify ranges or probability distributions for each hyperparameter.</li><li>Randomly select a user-defined number of hyperparameter combinations.</li><li>Evaluate each sampled configuration using cross-validation.</li><li>Select the configuration that achieves the best validation performance.</li></ul><p><strong>Advantages:</strong></p><ul><li><strong>More computationally efficient:</strong> Explores a wider range of values with fewer evaluations.</li><li><strong>Better scalability:</strong> Performs well even with large and complex hyperparameter spaces.</li></ul><p><strong>Disadvantages:</strong></p><ul><li><strong>No guarantee of optimality:</strong> The best possible combination may not be sampled due to randomness.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/966/1*Q1uosq79ZWV80G9bp5olNw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qDSOZS8h21LxMSd6G0BViQ.jpeg" /></figure><h3>3. Meet Optuna</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5yN0v9SHNuHTqbq88qBbnw.png" /><figcaption><a href="https://optuna.org/">https://optuna.org</a></figcaption></figure><p>Optuna is a Python library designed for <strong>automatic hyperparameter optimization</strong>.</p><p>Some key benefits:</p><ul><li>Uses <strong>Bayesian optimization {Tree-Structured Parzen Estimator</strong><em> </em><strong>(TPE)</strong>} to learn from past trials</li><li><strong>Pruning</strong>: stops bad trials early to save compute</li><li>Easy to run <strong>parallel optimization</strong></li><li>Clean, minimal API</li></ul><h3>Key Terminologies in Optuna</h3><h4>1. What is a Study in Optuna?</h4><p>An Optuna <strong>Study</strong> is the overall experimental session where we search for the best hyperparameters. Imagine a study as a master project — every attempt (trial) to find the best model settings falls under a single study.</p><p><em>Example: You’re trying to maximize the accuracy of an XGBoost model. Every single combination you try is logged in one study.</em></p><h4>2. Exploring Trials</h4><p>A <strong>Trial</strong> is a single run in which Optuna evaluates a specific set of hyperparameter values.</p><p><em>Example: In one trial, maybe the model uses a learning rate of 0.01 and a maximum depth of 5. Optuna records the results, then moves on to the next set.</em></p><h4>3. Understanding Trial Parameters</h4><p>Each <strong>Trial</strong> is unique because it uses its own set of <strong>Trial Parameters</strong> — the chosen hyperparameter values for that run.</p><p><em>Example:</em></p><ul><li><em>Trial 1: Learning rate = 0.001, Batch size = 32</em></li><li><em>Trial 2: Learning rate = 0.01, Batch size = 64</em></li></ul><p>Optuna keeps track of how each combination performs so it can make better decisions moving forward.</p><h4>4. The Objective Function</h4><p>Think of the <strong>Objective Function</strong> as the “goalkeeper” of your search — it’s the function Optuna tries to optimize. You feed in hyperparameters, and it outputs a metric like accuracy, loss, or F1 score.</p><p><em>Example: In a classification problem, the objective function could be cross-entropy loss. Optuna aims to minimize this value.</em></p><h4>5. What is a Sampler?</h4><p>The <strong>Sampler</strong> decides which hyperparameter combinations to try next. Optuna’s default is the Tree-structured Parzen Estimator (<strong>TPE</strong>), which is much smarter than random guessing. TPE explores areas where good results are probable, speeding up the search.</p><p><em>Example: TPE focuses on promising regions of the hyperparameter space that previous trials suggest might yield high performance.</em></p><p>Optuna also supports random sampling, custom algorithms, or other advanced techniques.</p><blockquote><strong>TL;DR<br></strong>Putting It All Together</blockquote><ul><li><strong>Study</strong>: The entire search session for the best model settings (a collection of trials).</li><li><strong>Trial</strong>: One round of testing specific hyperparameters OR one experiment with a specific set of hyperparameters.</li><li><strong>Trial Parameters</strong>: The actual hyperparameter values tried in a particular trial.</li><li><strong>Objective Function</strong>: The metric you want to maximize or minimize.</li><li><strong>Sampler</strong>: The algorithm that suggests next hyperparameters to try. Optuna’s default is <strong>TPE (Tree-structured Parzen Estimator)</strong>, a Bayesian approach.</li></ul><p>Optuna learns from results as it goes, steering the search toward areas most likely to deliver improvements.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/242/1*ZSB2OUHfuFTEmbYE4rKYtg.png" /></figure><h3>Step-by-Step Example with Optuna</h3><p>Let’s optimize a RandomForestClassifier on the Breast Cancer dataset from scikit-learn.</p><pre># Install first:<br>!pip install optuna</pre><pre>import optuna<br>import tqdm as notebook_tqdm<br>from sklearn.datasets import load_breast_cancer<br>from sklearn.model_selection import cross_val_score, StratifiedKFold<br>from sklearn.ensemble import RandomForestClassifier<br><br># Load dataset<br>X, y = load_breast_cancer(return_X_y=True)<br><br># Define the objective function<br>def objective(trial):<br>    n_estimators = trial.suggest_int(&quot;n_estimators&quot;, 50, 500)<br>    max_depth = trial.suggest_int(&quot;max_depth&quot;, 2, 30)<br><br>    min_samples_split = trial.suggest_int(&quot;min_samples_split&quot;, 2, 15)<br><br>    model = RandomForestClassifier(<br>        n_estimators=n_estimators,<br>        max_depth=max_depth,<br>        min_samples_split=min_samples_split,<br>        random_state=42,<br>        n_jobs=-1<br>    )<br><br>    score = cross_val_score(<br>        model, <br>        X, <br>        y, <br>        cv=StratifiedKFold(3),<br>        scoring=&quot;accuracy&quot;<br>    ).mean()<br><br>    return score<br><br># Create a study and optimize<br>study = optuna.create_study(<br>    direction=&quot;maximize&quot;,<br>    sampler=optuna.samplers.TPESampler(),<br>    study_name=&quot;RandomForestClassifier_On_Breast_Cancer&quot;<br>)<br><br>study.optimize(objective, n_trials=15)<br><br>print(&quot;Best score:&quot;, study.best_value)<br>print(&quot;Best parameters:&quot;, study.best_params)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*28NPyOXI5m5Gdw5Gb_5pfQ.png" /><figcaption>Output</figcaption></figure><p>That’s it! Optuna takes care of the search.</p><h3>Pruning: Save Time by Stopping Bad Trials</h3><p>If training takes long, you can prune poor performers early:</p><pre>study = optuna.create_study(<br>    direction=&quot;maximize&quot;,<br>    pruner=optuna.pruners.MedianPruner()<br>)</pre><p>Inside your objective, report intermediate scores:</p><pre>trial.report(intermediate_score, step)<br><br>if trial.should_prune():<br>    raise optuna.TrialPruned()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/250/1*blRKu68d10TO0cC6iM5xCw.png" /></figure><h3>Visualize Optuna Study Results</h3><pre># For visualizations<br>from optuna.visualization import plot_optimization_history, plot_parallel_coordinate, plot_slice, plot_contour, plot_param_importances</pre><h4>1. Optimization History Plot</h4><pre>plot_optimization_history(study).show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PnxHA1GArgFjZxl25cVK0g.png" /><figcaption>Optimization History</figcaption></figure><h4>How to interpret</h4><ul><li>Look for <strong>clusters of high-performing lines</strong></li><li>Identify which parameter ranges appear in good trials</li><li>Detect interactions between parameters</li></ul><h4>Use it to answer</h4><blockquote><em>“Which combinations of hyperparameters work well together?”</em></blockquote><h4>2. Parallel Coordinates Plot</h4><pre>plot_parallel_coordinate(study).show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GYXwPn0vWJj7p6iPx-WRfg.png" /><figcaption>Parallel Coordinates Plot</figcaption></figure><h4>How to interpret</h4><ul><li>Look for <strong>clusters of high-performing lines</strong></li><li>Identify which parameter ranges appear in good trials</li><li>Detect interactions between parameters</li></ul><h4>Use it to answer</h4><blockquote><em>“Which combinations of hyperparameters work well together?”</em></blockquote><h4>3. Slice Plot</h4><pre>plot_slice(study).show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Zn9HvpueYtq64r4nTQwG9A.png" /><figcaption>Slice Plot</figcaption></figure><h4>How to interpret</h4><ul><li>Clear trend → parameter is influential</li><li>Flat scatter → parameter has little impact</li><li>Multiple peaks → multiple good regions</li></ul><h4>Use it to answer</h4><blockquote><em>“Which values of this parameter tend to perform best?”</em></blockquote><h4>4. Contour Plot</h4><pre>plot_contour(study).show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9orDVLL6aI3fxQy6Gi5nxw.png" /><figcaption>Contour Plot</figcaption></figure><h4>How to interpret</h4><ul><li>Bright regions → good parameter combinations</li><li>Sharp gradients → sensitive interactions</li><li>Wide bright areas → robust parameter ranges</li></ul><h4>Use it to answer</h4><blockquote><em>“How do two hyperparameters interact with each other?”</em></blockquote><h4>5. Hyperparameter Importance Plot</h4><pre>plot_param_importances(study).show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6zWi2yN9FbWg38qOQZDmfw.png" /><figcaption>Hyperparameter Importance Plot</figcaption></figure><h4>How to interpret</h4><ul><li>Larger bar → greater impact on performance</li><li>Small bar → parameter may be safely fixed or removed</li></ul><h4>Use it to answer</h4><blockquote><em>“Which hyperparameters matter the most?”</em></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/252/1*4MwfrFdtsz9LPk8sYudMPg.png" /></figure><h3>Best Practices</h3><ul><li><strong>Set ranges wisely</strong> → too wide = wasted trials.</li><li><strong>Use </strong><strong>log=True</strong> for parameters that span many scales (like learning rate).</li><li><strong>Choose the right direction</strong> (maximize for accuracy/F1, minimize for loss).</li><li><strong>Parallelize</strong> with persistent storage if you have more compute.</li><li><strong>Visualize results</strong> with optuna.visualization to see parameter importance.</li></ul><h3>Common Pitfalls</h3><p>⚠️ Overfitting to validation → always keep a test set for final evaluation.</p><p>⚠️ Running too few trials on huge spaces → TPE needs data to learn patterns.</p><p>⚠️ Forgetting compute cost → balance accuracy vs runtime.</p><h3>Final Thoughts</h3><p>Optuna makes hyperparameter tuning simple and powerful. With just a few lines of code, you can:</p><ul><li>Define a search space</li><li>Let Bayesian optimization find good hyperparameters</li><li>Save compute with pruning</li></ul><p>Try it once, and you’ll never want to tune hyperparameters manually again.</p><p>👉 What’s next?</p><ul><li>Try swapping in XGBoost, LightGBM, or even a deep learning model.</li><li>Add pruning if training is long.</li><li>Experiment with visualizations to understand which hyperparameters matter most.</li></ul><p>Happy tuning! 🎯</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/875/0*Wf8C7-XlectblVRu.jpg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/248/1*-7Nqhpf2Cdz8EnzZFdkd8w.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6a426f43c76e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[What is an API & How to design it?]]></title>
            <link>https://medium.com/@avinashyadav16/what-is-an-api-how-to-design-it-d1410c0aea0f?source=rss-570103f5c627------2</link>
            <guid isPermaLink="false">https://medium.com/p/d1410c0aea0f</guid>
            <category><![CDATA[apı]]></category>
            <category><![CDATA[api]]></category>
            <category><![CDATA[api-development]]></category>
            <category><![CDATA[api-integration]]></category>
            <dc:creator><![CDATA[Avinash Yadav]]></dc:creator>
            <pubDate>Mon, 07 Jul 2025 22:46:49 GMT</pubDate>
            <atom:updated>2025-07-07T22:46:49.295Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>In this discussion, I will cover what an API is, what makes a good API, and how to design one effectively. By the end, you’ll have the knowledge to become a better software engineer when it comes to API design.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YIX7XZg7C4rj0It26sh2aQ.jpeg" /></figure><h3>Introduction to APIs</h3><p>An <strong>API (Application Programming Interface)</strong> is a documented way for external consumers (such as other developers, services, or applications) to interact with our code or service without needing to understand its internal workings.</p><blockquote><em>📌 Think of APIs as a </em><strong><em>contract</em></strong><em>: you expose </em><strong><em>what</em></strong><em> others can call, not </em><strong><em>how</em></strong><em> it’s implemented.</em></blockquote><p>An API serves as a contract between the <strong>service provider</strong> and the <strong>consumer</strong>, specifying:</p><ul><li><strong>What functionality</strong> is available</li><li><strong>How</strong> to request that functionality</li><li><strong>What responses</strong> to expect</li><li><strong>What errors</strong> might occur</li></ul><h4>API Example — Get Admins of a WhatsApp Group</h4><p>Imagine a user wants to retrieve all admins of a WhatsApp group. WhatsApp might expose a function like:</p><pre>function getAdmins(string groupID)</pre><p><strong>Example API Call:</strong></p><pre>GET /group/getAdmins<br>{<br>  &quot;groupId&quot;: &quot;xyz123&quot;<br>}</pre><p><strong>API Components:</strong></p><ul><li><strong>Function Name</strong>: getAdmins</li><li><strong>Input</strong>: groupID (string)</li><li><strong>Output</strong>: List&lt;Admin&gt; objects (each containing properties like name, user ID, etc.)</li></ul><blockquote>✅ <strong>Success Response</strong>: A list of Admin objects (typically returned as JSON)</blockquote><p><strong>Possible Errors</strong>: <br>- GroupDoesNotExist<br>- GroupDeleted <em>(may or may not be treated as an error depending on business logic)</em></p><blockquote><em>📌 The </em><em>Admin object may be complex (e.g., containing name, userID, role, etc.) and is typically returned in JSON format.</em></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jtmzD8LXaZo7O1Z5763CCg.jpeg" /></figure><h3>API Design Fundamentals</h3><p>The useful checklist that we need to run through while designing a good API are as follows:</p><h4>1. Purpose &amp; Belonging</h4><ul><li><strong>Ask:</strong> <em>Where does this function/API belong or where should it be exposed?<br></em>So if we have a microservice architecture and we have a service as a <strong><em>GroupService</em></strong>, which handles everything related to groups, this is the place where our getAdmins API belongs to, because it is something to do with finding all the admins with particular <strong>groupID</strong></li><li>APIs should be exposed by the service that owns the data/logic.</li></ul><h4>2. Proper Naming and Scope</h4><p><strong>Key Principle</strong>: The API name should exactly match its functionality(<strong>Name functions by what they do)</strong>.</p><ul><li><strong>Good</strong>: GetAdmins should only return admins.</li><li><strong>Bad</strong>: If it returns admin information plus their groups they belongs to, then in that case the API name is misleading or named wrongly. It is no longer <strong>getAdmins</strong> rather it became <strong>getAdminsAndGroups</strong> something similar. :) ⇒ &lt; NAMING IS IMPORTANT &gt;</li></ul><blockquote><em>🧠 Principle: </em><strong><em>Name = action + object + clarity</em></strong></blockquote><p><strong>Common Mistakes</strong>:</p><ul><li>Overloading functions with extra functionality not implied by the name</li><li>Making APIs too generic (trying to handle too many use cases)</li></ul><h4>3. Parameter Design</h4><p><strong>Principles</strong>:</p><ul><li>Only include parameters absolutely necessary for the core functionality and reflect the intent of the action. So that your API is not overloaded.</li></ul><p>For example, in our previous example we are asking for <strong>groupID</strong> , may be the caller has more than that, may be he is also wants to check if a list of users with their <strong>userID</strong> are admins. So in these cases should we also allow the function to accept an additional parameter or not, that depends on the use cases or the context in which we are using the API but the API/function in that case will be better to call it as checkAdmins, not getAdmins. :) ⇒ &lt; AGAIN NAMING IS IMPORTANT &gt;</p><ul><li>Avoid optional parameters unless they serve a clear optimization purpose</li><li>Each additional parameter increases complexity and maintenance burden</li></ul><p><strong>Optimization Exception(cases when extra parameters are acceptable)</strong>: When performance is critical (e.g., reducing microservice calls), you might accept additional parameters to avoid subsequent lookups or reduce internal service calls by giving more input upfront. Basically it is like giving your service more information so that it does not to depend on other services for that informations in <strong>GroupServices</strong>. In such cases also try to:</p><ul><li>Rename the API to reflect the expanded functionality</li><li>Document the performance benefits clearly</li></ul><h4>4. Response Design — Keep It Minimal &amp; Relevant</h4><p><strong>Anti-Patterns</strong>:</p><ul><li><strong>Do NOT overload</strong> responses with unnecessary data and return it to the caller with more data than requested “just in case” it’s needed later and you don’t need to change the response object.</li><li>Creating overly complex response objects to “future-proof” the API</li><li>Large payloads increase network usage and confuse clients.</li></ul><p><strong>Good Practices</strong>:</p><ul><li>Return only what’s needed for the current use case</li><li>Keep responses focused and clear</li><li>If more data is needed later, create new specific endpoints</li></ul><blockquote><em>✅ Keep responses concise, structured, and aligned with what the caller expects.</em></blockquote><h4>5. Error Handling — Be Clear and Responsible</h4><p>So there might be cases where people may define all the errors possible on earth for their API and there can also be case where people design absolutely nothing, taking no responsibility and just return a generic response for all the cases that may happen. &lt; though you may tend to define all the possible error for your respective service &gt;</p><p>For example:</p><ul><li>If groupId is expected to be an integer → enforce the data type to be an integer in the parameter itself, no need for extra error checks for parsing.</li><li>In case if <strong>groupID</strong> string is too long and your database query fails because you have defined the <strong>groupID</strong> to be of certain length. So in that case you may not require to return an error that the string is too long rather you can just fail that query. &lt;Although it is subjective in nature, but extremes are always odd&gt;</li><li>Define clear, <strong>expected errors</strong>:<br>- Group not found<br>- Group is deleted</li><li>Avoid vague or overly generic errors like just 400 or 500.</li></ul><blockquote><em>📌 Don’t go to extremes:<br>» </em>No errors defined → frustrating<br><em>» </em>Every small case defined → over-engineering<br><em>» </em>So in general while defining errors, think of the common expectations and the responsibilities that your API is going to have.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jtmzD8LXaZo7O1Z5763CCg.jpeg" /></figure><h3>HTTP API Design Considerations</h3><h3>1. Routing and Endpoint Structure</h3><p>A lot of time we may have to expose our APIs on HTTP endpoint and in those cases we need to understand how HTTP request and response object actually works.</p><p><strong>Best Practices</strong>:</p><ul><li>Place APIs in logical services (e.g., group-related APIs in a Groups service)</li><li>Use clear, hierarchical paths: /groups/v1/getAdmins</li><li>Version your APIs (e.g., v1) to allow for future changes</li></ul><p>For Example:</p><pre>POST www.exmple.com/group/v1/getAdmins<br>{<br>  &quot;groupId&quot;: &quot;xyz123&quot;<br>}</pre><p><strong>Routing Breakdown: The </strong><strong>WHERE question is answered here</strong></p><ul><li>www.exmple.com → Address of your site</li><li>/group → service, the thing that actually contains the API actions</li><li>/v1 → versioning (good for future changes)</li><li>/getAdmins → action or the function that needs to be called.</li><li>Request body: JSON { groupId: ... }</li><li>Response: JSON array of Admins with IDs and names</li></ul><pre>const data = {<br>    &quot;admin&quot;: [<br>        { &quot;id&quot;: &quot;1&quot;, &quot;name&quot;: &quot;Admin1&quot; },<br>        { &quot;id&quot;: &quot;2&quot;, &quot;name&quot;: &quot;Admin2&quot; },<br>        { &quot;id&quot;: &quot;3&quot;, &quot;name&quot;: &quot;Admin3&quot; }<br>    ]<br>};</pre><p><strong>GET vs POST</strong></p><p>The above we did using POST method but</p><ul><li>You <strong>can</strong> also use GET here by passing groupId=123 in the getAdmins function and we can also make getAdmins to admins:<br>- GET /group/v1/admins?groupId=123<br>- Slightly shorter as no request body or payload to be sent along with</li><li>Use POST if:<br>- Payload is large<br>- Sensitive data (not visible in URL)<br>- Complex filtering logic</li></ul><p><strong>Common Mistakes</strong>:</p><ul><li>Avoid mixing routing and action inside payloads (e.g., sending “action”: “getAdmins” in request body). It leads to confusion and poor design.</li></ul><pre>const request = {<br>     action: &quot;getAdmins&quot;,<br>     groupID: &quot;xyz123&quot; <br>};  <br><br>// Possible to do this also but it is going to be bad practice.</pre><ul><li>Redundancy with HTTP methods i.e. if you’re using HTTP GET, then don’t also call your API as getAdmins. The verb is already implied by HTTP. (e.g., GET /getAdmins is redundant - better as GET /admins)</li></ul><blockquote><em>✅ RESTful APIs should be resource-based, not action-based.</em></blockquote><h3>2. HTTP Methods</h3><ul><li>Use GET when you&#39;re retrieving information and all parameters are in the URL</li><li>Use POST when sending data in the request body</li><li>Match the HTTP method to the operation’s semantics (GET for read, POST for create, PUT for update, etc.)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jtmzD8LXaZo7O1Z5763CCg.jpeg" /></figure><h3>Advanced API Design Considerations</h3><h3>1. Side Effects and Atomicity</h3><blockquote><em>Atomicity means that operations must complete or fail as a whole unit.</em></blockquote><p><strong>Problem</strong>: APIs that perform multiple operations unexpectedly</p><p><strong>Example</strong>: So suppose that we have an API setAdmins which takes 2 parameters groupID as string and admin as a list of admins who needs to made admins in the groupID.</p><pre>POST /group/setAdmins<br>{<br>  &quot;groupId&quot;: &quot;xyz123&quot;,<br>  &quot;admins&quot;: [...]<br>}</pre><p><strong>What Can Go Wrong:</strong></p><p>What if these users mentioned in the admins list are not members of the group?</p><ul><li>In general, we can throw an error stating that since they are not members, they can’t be made the group administrative.</li><li>However, based on the use cases we can also have the scenario when we need to add the non-member to the group and then make him admin.</li></ul><p>What if the group doesn’t exist?</p><ul><li>In this case, we need to create the group and then we can add the mentioned members to group and make the admin. This can also be a possible expectation although it is no longer going to be a good API but it is possible.<br>- First: create group<br>- Then: add members<br>- Finally: set admins</li></ul><blockquote><em>🤯 Too many side effects = confusion(we are saying </em><em>setAdmins but we are doing much more than that), hard to test/debug, unclear behavior</em></blockquote><p><strong>Solutions</strong>:</p><ul><li>Keep APIs focused on single responsibilities with NO SIDE EFFECTS</li><li>If sometimes you need atomic operations (e.g., create + set admins in one go):<br>- Create new composite APIs with clear names reflecting all operations<br>- Or require clients to make multiple calls in sequence</li><li>Still, document all side effects well and avoid “magic behavior” hidden behind generic names.</li></ul><blockquote><em>🎯 APIs should do one thing, and do it well. Keep behavior predictable and atomic.</em></blockquote><h3>2. Handling Large Responses</h3><p>Think of a function from which we are getting all the members of a group i.e. almost 200 members and each one of them having their profile which might be pretty detailed. So, effectively what happened is that the response is really large. Therefore one of the things that we can think of is to break the response into pieces.</p><p><strong>Two Main Approaches</strong>:</p><h4><strong>1. Pagination</strong>:- Responsibility To The Client</h4><ul><li>Client requests data in chunks (e.g., 10 records at a time)</li><li>API exposes offset and limit or uses cursor-based navigation</li></ul><pre>GET /group/members?groupId=xyz123&amp;offset=0&amp;limit=10</pre><ul><li>Maintains statelessness</li></ul><h4><strong>2. Fragmentation</strong>:</h4><ul><li>Server breaks response into multiple packets</li><li>Each packet is numbered sequentially</li><li>Final packet signals completion. Include an “end” marker to indicate completion.</li><li>Useful for internal service-to-service communication or Internal technique for server-to-server calls</li></ul><blockquote>📦 Helps maintain performance, especially in microservice communication.</blockquote><h3>3. Consistency vs. Performance</h3><p>So this for the scenario where we have to choose that how consistent we want our data to be.</p><p><strong>Real-World Challenge:</strong></p><ul><li>Get admins for a group</li><li>You read list of admins from DB</li><li>While reading, someone else adds a new admin</li><li>You return stale data</li></ul><p><strong>Do You Care?</strong></p><ul><li>Depends on the <strong>use case</strong>:<br><em>»</em> If <em>eventual consistency</em> is acceptable → serve from cache<br><em>»</em> If strict consistency required → read from DB directly</li></ul><p><strong>Trade-offs</strong>:</p><ul><li><strong>Strong consistency</strong>: Guarantees up-to-date data but is slower</li><li><strong>Eventual consistency</strong>: May return slightly stale data but is faster</li></ul><p><strong>Implementation Options</strong>:</p><ul><li>Caching: Improves performance but risks stale data</li><li>Service degradation: Under heavy load<br><em>»</em> Use <strong>cache</strong> where possible<br><em>»</em> Trim down response: return essential data only, Instead of full profile, just send names<br><em>»</em> This is known as <strong>graceful degradation</strong></li><li>Let clients choose consistency level via parameters</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jtmzD8LXaZo7O1Z5763CCg.jpeg" /></figure><h3>Conclusion</h3><p>Good API design is about creating clear, focused contracts between services. Key principles include:</p><ul><li><em>Precise naming that matches functionality</em></li><li><em>Minimal, well-defined parameters and responses</em></li><li><em>Proper error handling</em></li><li><em>Careful consideration of side effects</em></li><li><em>Appropriate handling of performance and consistency trade-offs</em></li></ul><p>Well-designed APIs are easier to use, maintain, and evolve over time, making them critical for building scalable, maintainable systems.</p><blockquote><em>🗣️</em> <strong>APIs are like doors to your service. A good door is easy to open, labeled clearly, only lets in who’s allowed, and doesn’t lead to unintended places.</strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*ri9ZI4v6wKoeQOK3NxvSDw.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d1410c0aea0f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Namaste JavaScript — PART 02]]></title>
            <link>https://medium.com/@avinashyadav16/namaste-javascript-part-02-1a69cffe9c0e?source=rss-570103f5c627------2</link>
            <guid isPermaLink="false">https://medium.com/p/1a69cffe9c0e</guid>
            <category><![CDATA[javascript-development]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[javascript-tips]]></category>
            <dc:creator><![CDATA[Avinash Yadav]]></dc:creator>
            <pubDate>Wed, 25 Jun 2025 18:11:21 GMT</pubDate>
            <atom:updated>2025-06-25T18:11:21.066Z</atom:updated>
            <content:encoded><![CDATA[<h3>Namaste JavaScript — PART 02</h3><p><em>Part 02 of the series explores key JavaScript concepts, including the difference between undefined and not defined, scope and lexical environment, </em><em>let/</em><em>const and the temporal dead zone, block scope and shadowing, and finally wraps up with a discussion on closures.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/875/0*W4WGEmDHfggBp92G.jpeg" /></figure><p><em>In case you missed the last article: </em><a href="https://medium.com/@avinashyadav16/namaste-javascript-part-01-930ac27a1213"><em>Namaste JavaScript — PART 01</em></a></p><h3>TABLE OF CONTENT</h3><p><a href="#a151"><em>CHAPTER 06: Undefined vs Not Defined</em></a><em><br></em><a href="#503b"><em>CHAPTER 07: Scope and Lexical Environment</em></a><em><br></em><a href="#fbce"><em>CHAPTER 08: let, const, temporal dead zone, types of errors</em></a><em><br></em><a href="#a74c"><em>CHAPTER 09: Block Scope and Shadowing</em></a><em><br></em><a href="#870f"><em>CHAPTER 10: Closures in JS</em></a></p><h3>CHAPTER 06: Undefined vs Not Defined</h3><ul><li>In first phase (memory allocation) JS assigns each variable to a placeholder called <em>undefined.</em></li><li><em>undefined</em> is when memory is allocated for the variable, but no value is assigned yet.</li><li>If an object/variable is not even declared/found in memory allocation phase, and tried to access it then it is <em>Not defined</em></li></ul><blockquote>When variable is declared but not assigned value, its current value is undefined. But when the variable itself is not declared but called in code, then it is not defined.</blockquote><pre>console.log(x);<br><br>var x = 25;<br><br>console.log(x);<br><br>console.log(a);</pre><p><strong><em>OUTPUT:</em></strong></p><blockquote>undefined<br>25<br>Uncaught ReferenceError: a is not defined</blockquote><ul><li>JS is a loosely-typed / weakly-typed language. It doesn’t attach variables to any datatype. We can say var a = 5, and then change the value to bool (a = true) or string (a = &#39;hello&#39;) later on.</li><li><strong>Never</strong> assign <em>undefined</em> to a variable manually. Let it happen on it’s own accord.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*ri9ZI4v6wKoeQOK3NxvSDw.png" /></figure><h3>CHAPTER 07: Scope and Lexical Environment</h3><blockquote>This is why JS is confusing</blockquote><h3>Case 1</h3><pre>function a() {<br>  console.log(b); // surprisingly instead of printing `undefined` it prints `10`.<br>  //So somehow this `b` could access the `b` outside the function.<br>}<br><br>var b = 10;<br><br>a(); // Function Invocation</pre><h3>Case 2</h3><pre>function a() {<br>  c(); // Function Invocation<br>  <br>  function c() {<br>      console.log(b); // when cursor comes here, it still prints out 10 somehow!!<br>  }<br>}<br><br>var b = 10;<br><br>a(); // Function Invocation</pre><h3>Case 3</h3><pre>function a() {<br>  var b = 10;<br><br>  c(); // Function Invocation<br><br>  function c() {<br>    console.log(b); //it prints the right value. How?<br>  }<br>}<br><br>a(); // Function Invocation<br><br>console.log(b); // now when cursor comes here, it prints NOT DEFINED!</pre><ul><li>This is the intuition behind <strong>scope.</strong></li><li>Scope in a simple language means where we can access a specific variable or function inside the code.</li><li>Scope in JavaScript is directly related to and dependent on the lexical environment.</li><li><strong>Lexical Environment</strong> : local memory + lexical environment of its parent</li><li>Whenever an EC is created, a Lexical environment(LE) is also created and is referenced in the local EC(in memory space)</li><li>Lexical means hierarchy. In the case 3 code, function c is lexically inside function a.</li><li>So in EC of c(), variables and function in c (none) + reference of lexical environment of parent a() is there</li><li>LE of a() in turn is its memory space + reference to LE of parent (Global EC)</li><li>LE of Global EC points to <em>null</em></li></ul><h3>Summarizing the above points:</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/570/1*gM41oeEojRslUN-P8eS-Rw.png" /></figure><p>call_stack = [GEC, a(), c()]</p><p>Now lets also assign the memory sections of each execution context in call_stack.</p><p>c() = lexical environment pointer pointing to a()</p><p>a() = [b:10, c:{}, [lexical environment pointer pointing to GEC]]</p><p>GEC = [a:{},[lexical_environment pointer pointing to null]]</p><h3>For case 3</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/855/1*aGy9bfaS599BHl-pKxuGIA.png" /></figure><ul><li>First JS engine searches for b in local memory of c(). Nothing is there.</li><li>So it goes to the reference of Lexical environment of parent a(). Here b = 10 is here. So it takes this value, goes back to c() and console prints it.</li><li>Had `b` not been in a(), then pointer would have gone to a()&#39;s parent (Global EC and searched there). Had b not been there too, then it goes to LE of global&#39;s parent which is null. Now JS engine stops and says b is NOT DEFINED.</li><li><strong>Lexical environment of c = Local memory of c + LE of A + LE of Global</strong></li><li>This process of going one by one to parent and checking is called <strong>scope chain</strong></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*ri9ZI4v6wKoeQOK3NxvSDw.png" /></figure><h3>CHAPTER 08: let, const, temporal dead zone, types of errors</h3><blockquote>let and const declarations are hoisted. But it’s different from var since they are in temporal dead-zone for a time being.</blockquote><pre>console.log(a); // ReferenceError: Cannot access &#39;a&#39; before initialization<br><br>console.log(b); // prints undefined as expected<br><br>let a = 10;<br><br>console.log(a); // 10<br><br>var b = 15;</pre><p>It looks like `let` isn’t hoisted, <strong>but it is.</strong></p><ul><li>Both a and b are actually initialized as <em>undefined</em> in hoisting stage. But var b is inside the storage space of GLOBAL, and a is in a separate memory(script), where it can be accessed only after assigning some value to it first.</li><li>i.e. one can access ‘a’ only if it is assigned. Thus, it throws error.</li></ul><blockquote><strong><em>Temporal Dead Zone</em></strong><em>: Time since when the let variable was hoisted until it is initialized some value.</em></blockquote><ul><li>So any line till before “let a = 10” is the TDZ for a</li><li>Since a is not accessible on global, its not accessible in <em>window/this</em> also</li><li>window.b or this.b -&gt; 15; But window.a or this.a -&gt;undefined, just like window.x-&gt;undefined (x isn&#39;t declared anywhere)</li></ul><pre>let a = 10;<br>let a = 100; //this code is rejected upfront as SyntaxError. (duplicate declaration)<br><br>-----------------------------------------<br><br>let a = 10;<br>var a = 100; // this code also rejected upfront as SyntaxError. (can&#39;t use same name in same scope)</pre><p>Let is a stricter version of var. Now, <strong>const</strong> is even more stricter than let.</p><ul><li>const holds all above properties of let.</li></ul><pre>let a;<br>a = 10;<br>console.log(a) // prints 10 properly. Note declaration and assigning of a is in different lines.<br><br>-----------------------------------------<br><br>const b;<br>b = 10;<br>console.log(b); // SyntaxError: Missing initializer in const declaration. (This type of declaration won&#39;t work with const. const b = 10 only will work)<br><br>-----------------------------------------<br>const b = 100;<br>b = 1000;<br>//this gives us TypeError: Assignment to constant variable.</pre><p>Till now 3 types of errors have been covered: Syntax, Reference, and Type.</p><h4>Uncaught ReferenceError: x is not defined at ...</h4><ul><li>This Error signifies that x has never been in the scope of the program. This literally means that x was never defined/declared and is being tried to be accessed.</li></ul><h4>Uncaught ReferenceError: cannot access &#39;a&#39; before initialization</h4><ul><li>This Error signifies that ‘a’ cannot be accessed because it is declared as ‘let’ and since it is not assigned a value, it is its Temporal Dead Zone. Thus, this error occurs.</li></ul><h4>Uncaught SyntaxError: Identifier &#39;a&#39; has already been declared</h4><ul><li>This Error signifies that we are redeclaring a variable that is ‘let’ declared. No execution will take place.</li></ul><pre>//code example 1.1<br>let a = 10;<br>let a = 100;</pre><pre>//code example 1.2<br>let a = 10;<br>var a = 100;</pre><p>Will throw this Syntax error and no code will be run and be rejected affront. ‘let’ is a strict form of declaration and thus can be done only once.</p><h4>Uncaught SyntaxError: Missing initializer in const declaration</h4><ul><li>This Error signifies that we haven’t initialized or assigned value to a const declaration.</li></ul><h4>Uncaught TypeError: Assignment to constant variable</h4><ul><li>This Error signifies that we are reassigning to a const variable.</li></ul><h3>Type Error</h3><p>The Errors that occur due to conflicts with the declaration type. For example re-assigning const type declaration will throw this.</p><h3>Syntax Error</h3><p>The Errors that occur due to wrong syntax that doesn’t match with JS Engine syntactical rules.</p><p>For example, if const is not initialized, it will throw syntax error as by syntax, it must initialize if it sees a const declaration.</p><p>Also, if variable that is assigned with ‘let’ declaration is tried to re-declared, then it throws Syntax Error.</p><h3>Reference Error</h3><p>The Errors that occurs if no reference is available for access. Can occur when the variable is no where in scope or maybe it is in temporal dead zone.</p><h3>SOME GOOD PRACTICES</h3><ul><li>Try using const wherever possible.</li><li>If not, use let.</li><li>Avoid var.</li><li>Declare and initialize all variables with let to the top to avoid errors to shrink temporal dead zone window to zero.</li></ul><blockquote><strong>NOTE</strong>: If in any interview when asked “Are let and const hoisted?” explain fully about temporal deadzone and all the above concepts too.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*ri9ZI4v6wKoeQOK3NxvSDw.png" /></figure><h3>CHAPTER 09: Block Scope and Shadowing</h3><h3>What is a block?</h3><ul><li>Block also known as a <em>compound statement</em> is used to group JS statements together into 1 group. We group them within curly braces {…}</li><li>The purpose is to group multiple statments at a place where JS expects only 1 statement.</li></ul><pre>//code example 1<br><br>if(true) some statement</pre><p>But if we want to write more statements to execute after if condition; then:</p><pre>//code example 2<br><br>if(true) {<br>    statement 1<br>    statement 2<br>    ...<br>}</pre><ul><li>The {} block treats all the statements as one statement.</li><li>The if doesn&#39;t have any curly braces in syntax.</li></ul><h3>BLOCK SCOPE</h3><ul><li>What are the variables and functions that can be accessed inside the block.</li></ul><pre>//code example 3<br><br>{<br>  var a = 10;<br>  let b = 20;<br>  const c = 30;<br>}<br>console.log(a);<br>console.log(b);</pre><p>Output:</p><pre>10<br>Uncaught ReferenceError: b is not defined</pre><h4>Behind the Scenes</h4><ul><li>In the BLOCK SCOPE; we get b and c inside it initialized as <em>undefined</em> as a part of hoisting (in a seperate memory space called block)</li><li>While, a is stored inside a GLOBAL scope.</li><li>Thus we say, <em>let</em> and <em>const</em> are BLOCK SCOPED. They are stored in a separate mem space which is reserved for this block. Also, they can’t be accessed outside this block. But var a can be accessed anywhere as it is in global scope.</li><li>Thus, we can’t access them outside the Block.</li></ul><h3>What is SHADOWING in JS?</h3><pre>//code example 4<br><br>var a = 100;<br>{<br>  var a = 10; //same name as global var<br>  let b = 20;<br>  const c = 30;<br><br>  console.log(a); // 10<br>  console.log(b); // 20<br>  console.log(c); // 30<br>}<br><br>console.log(a); // 10 instead of the 100 we were expecting. So block &quot;a&quot; modified val of global &quot;a&quot; as well.<br>// In console, only b and c are in block space. a initially is in global space(a = 100), and when a = 10 line is run, a is not created in block space, but replaces 100 with 10 in global space itself.</pre><ul><li>If one has same named variable outside the block, the variable inside the block <em>shadows</em> the outside variable.</li><li>So, a is reassigned to 10. Since both refers to same memory space i.e GLOBAL SPACE. <strong>This happens only for var</strong></li></ul><h3>Instead of `var`, now let’s use `let`</h3><pre>//code example 5<br><br>let b = 100;<br>{<br>  var a = 10;<br>  let b = 20;<br>  const c = 30;<br><br>  console.log(b);<br>}<br><br>console.log(b);</pre><p>Outputs:</p><blockquote><em>20</em></blockquote><blockquote><em>100 // this was what we were expecting in this first place. Both b’s are in separate spaces (one in Block(20) and one in Script(another arbitrary mem space)(100))</em></blockquote><ul><li>In the Scope, we have b in two places. The b outside of the block is in <em>Script</em> SCOPE (seperate memory space for let and const)</li><li>The b inside the block is in <em>Block</em> scope.</li><li>Thus, when in Block scope, 20 is printed and 100 when outside.</li><li>This concept is called “Shadowing”.</li><li>It is also true for <em>const</em> declarations.</li></ul><h3>Same logic is true even for functions also</h3><pre>const c = 100;<br><br>function x() {<br>  const c = 10;<br>  console.log(c);<br>}<br><br>x();<br><br>console.log(c);</pre><p>Output:</p><blockquote><em>10</em></blockquote><blockquote><em>100</em></blockquote><h3>What is Illegal Shadowing?</h3><pre>// code example 6<br><br>let a = 20;<br>{<br>  var a = 20;<br>}</pre><p>Outputs:</p><blockquote><em>Uncaught SyntaxError: Identifier ‘a’ has already been declared</em></blockquote><ul><li>We cannot shadow let with var. But it is valid to shadow a let using a let.</li><li>However, we can shadow var with let.</li><li>All scope rules that work in function are same in arrow functions too.</li><li>Since var is function scoped, it is not a problem with the code below.</li></ul><pre>// code example 7<br><br>let a = 20;<br><br>function x() {<br>  var a = 20;<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*ri9ZI4v6wKoeQOK3NxvSDw.png" /></figure><h3>CHAPTER 10: Closures in JS</h3><h3>Important Interview Question</h3><p><strong>Closure :</strong> Function bundled together with its lexical environment/scope.</p><p>JS is a weird language. You can pass functions as parameters to another function, assign a variable to an entire function, or even return a function.</p><p>e.g.:</p><pre>function x() {<br>  var a = 7;<br><br>  function y() {<br>      console.log(a);<br>    }<br><br>    return y; // instead of y();<br>}<br><br>var z = x();<br><br>console.log(z); // value of z is entire code of function y.</pre><p>When functions are returned from another function, they still maintain their lexical scope.</p><ul><li>When y is returned, not only is the function returned but the entire closure (function y + its lexical scope) is returned and put inside z. So when z is used somewhere else in program, it still remembers variable a inside x()</li><li>Closure is a very powerful concept of JS, just because this function remembers things even if they are not in their lexical scope</li></ul><h3>Some Gottchas:</h3><h4>CODE — 01</h4><pre>function x() {<br>  var a = 7;<br><br>  function y() {<br>      console.log(a);<br>    }<br>  <br>    a = 100;<br>  <br>    return y;<br>}<br><br>var z = x();<br><br>z();</pre><p>OUTPUT:</p><pre>100</pre><h4>CODE — 02</h4><pre>function w() {<br>  var b = 900;<br>  <br>  function x() {<br>    var a = 7;<br><br>    function y() {<br>      console.log(a, b);<br>    }<br><br>    y();<br>  }<br><br>  x();<br>}<br><br>var z = w();<br><br>z();</pre><p>OUTPUT:</p><pre>7 900</pre><h3>Uses of Closure</h3><ul><li>Module Design Pattern,</li><li>Currying,</li><li>Functions like once(fxn that can be run only once),</li><li>memoize,</li><li>maintaining state in async world,</li><li>setTimeout,</li><li>iterators, etc…</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*ri9ZI4v6wKoeQOK3NxvSDw.png" /></figure><h3>Conclusion</h3><p>These core concepts give us a solid foundation and reveal just how much is happening behind the scenes in JavaScript.</p><p>Will love your valued feedback and any suggestions.</p><p>Stay tuned for next article in this series.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1a69cffe9c0e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[System Design Fundamentals — PART 02]]></title>
            <link>https://medium.com/@avinashyadav16/system-design-fundamentals-part-02-d8ebae6e562e?source=rss-570103f5c627------2</link>
            <guid isPermaLink="false">https://medium.com/p/d8ebae6e562e</guid>
            <category><![CDATA[system-design-concepts]]></category>
            <category><![CDATA[systems-thinking]]></category>
            <category><![CDATA[system-design-interview]]></category>
            <dc:creator><![CDATA[Avinash Yadav]]></dc:creator>
            <pubDate>Tue, 24 Jun 2025 12:51:04 GMT</pubDate>
            <atom:updated>2025-06-25T11:08:08.126Z</atom:updated>
            <content:encoded><![CDATA[<h3>System Design Fundamentals — PART 02</h3><p><em>In the following section, I’ll provide concise, one-line explanations of fundamental concepts you should understand to effectively approach system design problems.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*45RAjB3gPjsoDNAd-Kfyig.jpeg" /></figure><blockquote>Missed the last article in this series? Catch up before diving in! ⇒ <a href="https://medium.com/@avinashyadav16/introduction-to-system-design-interview-part-01-eee746501198">Introduction to System Design Interview — PART 01</a></blockquote><h4>Table Of Content</h4><p><strong>Essential System Design Concepts</strong></p><p><em>1. </em><a href="#e55f"><em>Vertical v/s Horizontal Scaling</em></a><em><br>2. </em><a href="#9c8b"><em>CAP Theorem (Brewer’s Theorem)</em></a><em><br>3. </em><a href="#cb3b"><em>ACID v/s BASE</em></a><em><br>4. </em><a href="#7afe"><em>Partitioning/Sharding</em></a><em><br>5. </em><a href="#582f"><em>Consistent Hashing</em></a><em><br>6. </em><a href="#0c69"><em>Optimistic v/s Pessimistic Locking</em></a><em><br>7. </em><a href="#39a0"><em>Strong v/s Eventual Consistency</em></a><em><br>8. </em><a href="#f4f8"><em>Relational v/s NoSQL Databases</em></a><em><br>9. </em><a href="#d944"><em>Caching</em></a><em><br>10. </em><a href="#fd75"><em>Data Center / Racks / Hosts</em></a><em><br>11. </em><a href="#d0da"><em>CPU / Memory / Hard Drives / Network Bandwidth</em></a><em><br>12. </em><a href="#671c"><em>Random vs Sequential Read/Writes to Disk</em></a><em><br>13. </em><a href="#de6b"><em>HTTP/1.1 v/s HTTP/2 v/s WebSocket</em></a><em><br>14. </em><a href="#350c"><em>TCP/IP Model (Internet Protocol Suite)</em></a><em><br>15. </em><a href="#f55e"><em>IPv4 v/s IPv6</em></a><em><br>16. </em><a href="#97d9"><em>TCP v/s UDP</em></a><em><br>17. </em><a href="#bc22"><em>DNS Lookup (Domain Name System)</em></a><em><br>18. </em><a href="#2f62"><em>HTTP &amp; TLS (HTTPS)</em></a><em><br>19. </em><a href="#019e"><em>Public Key Infrastructure (PKI) &amp; Certificate Authorities (CA)</em></a><em><br>20. </em><a href="#b644"><em>Symmetric vs. Asymmetric Encryption</em></a><em><br>21. </em><a href="#245b"><em>Load Balancers (L4 v/s L7)</em></a><em><br>22. </em><a href="#21e1"><em>CDNs &amp; Edge Computing</em></a><em><br>23. </em><a href="#c163"><em>Bloom Filters &amp; Count-Min Sketch</em></a><em><br>24. </em><a href="#b19d"><em>Paxos &amp; Leader Election</em></a><em><br>25. </em><a href="#88ca"><em>Design Patterns &amp; Object-Oriented Design</em></a><em><br>26. </em><a href="#b835"><em>Virtual Machines (VMs) and Containers</em></a><em><br>27. </em><a href="#8c56"><em>Pub-Sub Architecture (Publisher-Subscriber Pattern)</em></a><em><br>28. </em><a href="#7dd4"><em>MapReduce</em></a><em><br>29. </em><a href="#0050"><em>Multithreading, Locks, Synchronization, and CAS</em></a><em><br>30. </em><a href="#36ab"><em>Authentication &amp; Authorization</em></a><em><br>31. </em><a href="#0d57"><em>Advanced Networking (CIDR, VPN, Subnet, Broadcast)</em></a><em><br>32. </em><a href="#239a"><em>Data Lakes v/s Data Warehouses</em></a></p><p><strong>Essential System Design Tools</strong></p><p><em>1. </em><a href="#2a69"><em>Cassandra (Wide-Column NoSQL Database)</em></a><em><br>2. </em><a href="#face"><em>MongoDB / Couchbase (Document Databases)</em></a><em><br>3. </em><a href="#f476"><em>MySQL (Relational Database)</em></a><em><br>4. </em><a href="#012f"><em>Memcached v/s Redis (Caching)</em></a><em><br>5. </em><a href="#6fa2"><em>ZooKeeper (Coordination Service)</em></a><em><br>6. </em><a href="#2919"><em>Kafka (Distributed Event Streaming Platform)</em></a><em><br>7. </em><a href="#5929"><em>NGINX / HAProxy (Load Balancers)</em></a><em><br>8. </em><a href="#d829"><em>Solr / Elasticsearch (Search Engines)</em></a><em><br>9. </em><a href="#7f9c"><em>Blobstore (Object Storage)</em></a><em><br>10. </em><a href="#e6d8"><em>Docker / Kubernetes / Mesos (Containerization)</em></a><em><br>11. </em><a href="#fb62"><em>Hadoop / Spark / HDFS (Big Data)</em></a></p><blockquote>Let’s start our discussion with the important concepts which we need to keep in mind while designing the systems for our problem description.</blockquote><h3>1. Vertical v/s Horizontal Scaling</h3><p>When we need to scale up our system, either we can do vertical scaling or horizontal scaling.</p><h3>Vertical Scaling (Scale-Up)</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/299/1*9MdHbNxWCg844Q_WYDg0hA.png" /></figure><h4><strong>Definition</strong></h4><p><em>Increasing the capacity of a single server by adding more resources (CPU, RAM, storage).</em></p><h4><strong>Pros</strong></h4><ul><li>Simpler architecture (no distributed system complexities).</li><li>No need for application changes.</li></ul><h4><strong>Cons</strong></h4><ul><li><strong>Hardware limits</strong>: There’s a ceiling to how much you can upgrade a single machine or how much memory and CPU we can add to a single host.</li><li><strong>Downtime required</strong>: Upgrades often need server restarts.</li><li><strong>Single point of failure (SPOF)</strong>: If the server fails, the entire system fails.</li><li><strong>Costly</strong>: High-end servers are exponentially more expensive.</li></ul><h3>Horizontal Scaling (Scale-Out)</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/455/1*37srD--9gt9lUSn3JwEftA.png" /></figure><h4><strong>Definition</strong></h4><p><em>It is to keep one host small and add more host/machines to a system to distribute the load.</em></p><h4><strong>Pros</strong></h4><ul><li><strong>Infinite scalability</strong>: Can add as many machines as needed.</li><li><strong>Fault tolerance</strong>: If one node fails, others can take over.</li><li><strong>Cost-effective</strong>: Uses commodity hardware.</li></ul><h4><strong>Cons</strong></h4><ul><li><strong>Dealing with all the distributed system challenges</strong>:</li><li><strong>Consistency</strong>: Ensuring all nodes have the same data.</li><li><strong>Network latency</strong>: Communication between nodes introduces delays.</li><li><strong>Complexity</strong>: Requires load balancing, partitioning, and synchronization.</li></ul><h4><strong>When to Use Which?</strong></h4><ul><li><strong>Vertical scaling</strong> for small-scale applications that don’t need high availability.</li><li><strong>Horizontal scaling</strong> for large-scale, high-availability systems (e.g., cloud services).</li></ul><p>So, in general horizontal scaling is more preferred than vertical scaling.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9gYsyBVE4Zjobeqm_T0asA.png" /></figure><h3>2. CAP Theorem (Brewer’s Theorem)</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*X0mYQdpcbsJ9JWUTXWtKbQ.jpeg" /></figure><h4>What is CAP?</h4><ul><li><strong>Consistency (C)</strong>: Every read receives the most recent write.</li><li><strong>Availability (A)</strong>: Every request gets a response (even if stale) i.e. it might get the most recent one or might not.</li><li><strong>Partition Tolerance (P)</strong>: The system continues working despite network failures. In simple terms, between 2 nodes we could be dropping network packets.</li></ul><h4><strong>The Trade-Off</strong></h4><ul><li><strong>You can only have two out of three</strong> in a distributed system.</li><li><strong>Partition tolerance is mandatory</strong> (networks are unreliable), so basically we choose between <strong>CP</strong> or <strong>AP</strong>.</li></ul><h4><strong>Examples</strong></h4><p><strong>CP Systems</strong> (<em>Consistency + Partition Tolerance</em>): These system choose consistency over availability, meaning they could be less available but their data is always consistent.</p><ul><li>Traditional relational databases (PostgreSQL, MySQL).</li><li><strong>Downside</strong>: If a partition occurs, the system may become unavailable.</li></ul><p><strong>AP Systems</strong> (<em>Availability + Partition Tolerance</em>): These system prefer availability over consistency.</p><ul><li>NoSQL databases (Cassandra, DynamoDB).</li><li><strong>Downside</strong>: May return stale data during partitions.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9gYsyBVE4Zjobeqm_T0asA.png" /></figure><h3>3. ACID v/s BASE</h3><p>In database systems, ACID and BASE properties are two sets of principles that govern how transactions are handled, ensuring data integrity and reliability.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WtKRVVLpX2xLFCMljPW5_g.jpeg" /></figure><h3>ACID (Traditional Databases)</h3><ul><li><strong>Atomicity</strong>: Transactions are all-or-nothing.</li><li><strong>Consistency</strong>: Data remains valid before and after transactions.</li><li><strong>Isolation</strong>: Concurrent transactions don’t interfere.</li><li><strong>Durability</strong>: Once committed, data persists even after crashes.</li></ul><h3>BASE (NoSQL Systems)</h3><ul><li><strong>Basically Available</strong>: System remains available even if some nodes fail.</li><li><strong>Soft State</strong>: Data may change over time without new inputs (eventual consistency).</li><li><strong>Eventual Consistency</strong>: Data will become consistent over time.</li></ul><h4><strong>When to Use Which?</strong></h4><p>The ACID model <strong>provides a consistent system</strong>. The BASE model provides high availability.</p><ul><li><strong>ACID</strong> for banking, financial transactions (strong consistency required).</li><li><strong>BASE</strong> for social media, recommendation engines (high availability preferred).</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9gYsyBVE4Zjobeqm_T0asA.png" /></figure><h3>4. Partitioning/Sharding</h3><p>Sharding is a database partitioning technique that divides a large database into smaller, more manageable parts called shards.</p><p>These shards are then distributed across multiple servers or machines, allowing for improved performance and scalability. Essentially, it’s a way to horizontally scale a database by spreading the data across different physical locations.</p><h4><strong>Why Shard?</strong></h4><ul><li>Databases with <strong>trillions of records</strong> can’t fit on a single machine.</li><li><strong>Sharding splits data across multiple nodes</strong> based on a key (e.g., user ID, geographic region).</li><li>The key shards or splits the data such that every node of a database is responsible for some of the records of that large database.</li></ul><h4><strong>Sharding Strategies</strong></h4><ol><li><strong>Range-based</strong>: Splits data by ranges (e.g., A-M, N-Z).</li><li><strong>Hash-based</strong>: Uses a hash function to distribute data evenly.</li><li><strong>Directory-based</strong>: Uses a lookup table to locate shards.</li></ol><h4><strong>Challenges</strong></h4><ul><li><strong>Joins become expensive</strong> (data is spread across nodes).</li><li><strong>Rebalancing is hard</strong> when adding/removing nodes.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9gYsyBVE4Zjobeqm_T0asA.png" /></figure><h3>5. Consistent Hashing</h3><p>Consistent Hashing is a distributed hashing scheme that operates independently of the number of servers or objects in a distributed <em>hash table</em> by assigning them a position on an abstract circle, or <em>hash ring</em>. This allows servers and objects to scale without affecting the overall system. Consistent Hashing is one of the techniques used heavily in sharding/partitioning.</p><p><strong>Problem with Traditional Hashing</strong></p><ul><li>If a node is added/removed, most keys get remapped → <strong>inefficient</strong>.</li></ul><p><strong>How Consistent Hashing Solves It?</strong></p><ul><li>Uses a <strong>ring structure</strong> where nodes and keys are hashed onto the ring.</li><li>Each key is assigned to the <strong>next node clockwise</strong>.</li><li><strong>When a node is added/removed</strong>, only a small fraction of keys are remapped.</li></ul><p><strong>Used in</strong>: DynamoDB, Cassandra, CDNs, load balancers.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9gYsyBVE4Zjobeqm_T0asA.png" /></figure><h3>6. Optimistic v/s Pessimistic Locking</h3><p>Pessimistic and optimistic locking are <strong><em>concurrency control mechanisms</em></strong> used in databases to manage access to data when multiple users or applications are trying to modify it simultaneously.</p><h3>Optimistic Locking</h3><p>Let’s suppose we are doing a database transaction and in the optimistic locking we do not acquire any locks but when we are ready to commit our transaction at that point we check to see if no other transaction updated the record which we are working on.</p><ul><li><strong>No locks acquired</strong>; checks for conflicts at commit time (e.g., using version numbers).</li><li><strong>Pros</strong>: Higher concurrency.</li><li><strong>Cons</strong>: May require retries if conflicts occur.</li></ul><h3>Pessimistic Locking</h3><p>In pessimistic locking, we acquire all the locks beforehand and then we commit our transaction.</p><ul><li><strong>Locks data upfront</strong> (e.g., SELECT FOR UPDATE in SQL).</li><li><strong>Pros</strong>: Prevents conflicts.</li><li><strong>Cons</strong>: Can cause deadlocks, reduces concurrency.</li></ul><h4><strong>Use Cases</strong></h4><ul><li><strong>Pessimistic</strong>: <em>Banking systems (high contention).</em></li><li><strong>Optimistic</strong>: <em>E-commerce (low contention, high concurrency).</em></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9gYsyBVE4Zjobeqm_T0asA.png" /></figure><h3>7. Strong v/s Eventual Consistency</h3><p>In the context of distributed systems, strong consistency guarantees that all nodes will see the same data at the same time after a write operation, while eventual consistency implies that all nodes will eventually (but not necessarily immediately) reflect the same data after a write.</p><h3>Strong Consistency</h3><ul><li><strong>Reads always see the latest write</strong> (e.g., relational databases).</li><li><strong>Downside</strong>: Higher latency (waits for all replicas to sync).</li></ul><h3>Eventual Consistency</h3><ul><li><strong>Reads may see stale data, but will eventually catch up</strong> (e.g., NoSQL).</li><li><strong>Downside</strong>: Temporary inconsistencies.</li></ul><h4><strong>Use Cases</strong></h4><ul><li><strong>Strong</strong>: <em>Financial transactions, inventory systems.</em></li><li><strong>Eventual</strong>: <em>Social media feeds, comments.</em></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9gYsyBVE4Zjobeqm_T0asA.png" /></figure><h3>8. Relational v/s NoSQL Databases</h3><p>Now a days, we may see most of the time, developer preferring to use NoSQL databases. :) That’s fine. But don’t discard relational databases just yet.</p><p>Remember relational databases provides all the nice ACID properties, while NoSQL databases scales a little bit better and has higher availability.</p><p>So, depending on the use cases and situations, always evaluate which one fits better in the scenario.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/897/1*XpDRTzZua88ekXrqo_NXzQ.png" /><figcaption>Comparison Between SQL and NoSQL</figcaption></figure><h4><strong>Types of NoSQL</strong></h4><ol><li><strong>Key-Value</strong>: Redis, DynamoDB (fast lookups).</li></ol><ul><li><em>Simplest type, where we have key and value and it stores a key-value pair in the database.</em></li></ul><p><strong>2. Document</strong>: MongoDB (JSON storage).</p><ul><li><em>Mostly used when we have semi-structured data or XML or JSON kind of data and we want them to persist into the database, then we go for Document Based NoSQL database.</em></li></ul><p><strong>3. Wide-Column</strong>: Cassandra (time-series data).</p><ul><li><em>In this database, the row can have many different kinds of columns and can also have many columns as well.</em></li></ul><p><strong>4. Graph</strong>: Neo4j (relationships like social networks).</p><ul><li><em>Useful when we have entities and edges or relationship between those entities. Basically useful when we have graph, then the Graph Based NoSQL database is used to hold those data/graph.</em></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9gYsyBVE4Zjobeqm_T0asA.png" /></figure><h3>9. Caching</h3><p>Used to speed up our request. When we know that some data is going to be accessed more frequently, then store them in cache so that they can be accessed quickly.</p><h4><strong>Types of Caches</strong></h4><ol><li><strong>Local Cache</strong>: Each node has its own cache and cache data is not shared between nodes (fast but inconsistent).</li><li><strong>Distributed Cache</strong>: Cache data is shared between different nodes (Redis, Memcached).</li></ol><h4><strong>Cache Eviction Policies</strong></h4><ul><li><strong>LRU (Least Recently Used)</strong>: Discards oldest unused items.</li><li><strong>TTL (Time-To-Live)</strong>: Automatically expires data after a time.</li></ul><h4><strong>Cache Invalidation Challenges</strong></h4><ul><li><strong>Stale data</strong> if not updated properly.</li><li><strong>Cache stampede</strong>: Thundering herd problem if many requests miss the cache.</li></ul><blockquote>Always remember cache can not be the source of truth and cache data has to be small because cache tends to keep all the data in the memory and lastly consider some of the eviction policies around cache.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9gYsyBVE4Zjobeqm_T0asA.png" /></figure><h3>10. Data Center / Racks / Hosts</h3><p>Understand the physical and logical architecture of modern data centers.</p><p>A typical hierarchy is:</p><p><strong><em>Data Center → Racks → Hosts (Servers)</em></strong></p><ul><li>A <strong>data center</strong> is a facility housing computing infrastructure.</li><li><strong>Racks</strong> are metal frames that hold multiple servers (hosts).</li><li><strong>Hosts</strong> are individual servers (physical/virtual machines).</li></ul><h4>Key considerations</h4><ul><li><strong>Latency Awareness:</strong> <em>Communication latency can vary significantly</em></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_itW0MNCDs1OqJGCBqPphw.png" /></figure><ul><li><strong>Failure Scenarios:</strong> <em>Be prepared for various levels of failure</em></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IA7jFEZB4zHMEYqsKSwJfA.png" /></figure><h4><strong>Design Implications</strong></h4><ul><li>Distribute services and data across racks and data centers to minimize single points of failure</li><li>Use failure domains to isolate faults</li><li>Optimize for locality when deploying latency-sensitive applications.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9gYsyBVE4Zjobeqm_T0asA.png" /></figure><h3>11. CPU / Memory / Hard Drives / Network Bandwidth</h3><p>These are the core hardware resources that define the performance envelope of any system. All of them are <strong>finite and constrained</strong>, so system design must carefully account for their limitations.</p><h3>CPU</h3><ul><li>Affects processing speed and concurrency</li><li>Bottlenecks often occur with compute-heavy workloads</li><li>Mitigation strategies: optimize algorithms, parallelize tasks, offload work to more efficient services.</li></ul><h3>Memory (RAM)</h3><ul><li>Critical for in-memory computations, caching, and low-latency processing</li><li>Limited memory can cause swapping or out-of-memory errors</li><li>Strategies: efficient data structures, memory-aware caching, GC tuning (if applicable)</li></ul><h3>Hard Drives (Disk I/O)</h3><ul><li>Important for data persistence, logging, and large-scale storage</li><li>Disk access is slower than RAM; seek time matters</li><li>Strategies: use SSDs over HDDs, leverage sequential reads/writes, tune file formats, employ compression</li></ul><h3>Network Bandwidth</h3><ul><li>Impacts how fast data moves between services or systems</li><li>Limited bandwidth can create bottlenecks in distributed systems</li><li>Strategies: reduce payload sizes, use efficient protocols (e.g., gRPC, HTTP/2), colocate tightly-coupled services</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9gYsyBVE4Zjobeqm_T0asA.png" /></figure><h3>12. Random vs Sequential Read/Writes to Disk</h3><p>Disk I/O performance is a critical factor in system efficiency, especially for storage-heavy workloads. One of the most impactful aspects is the <strong>pattern of access</strong> — whether it is <strong>sequential</strong> or <strong>random</strong>.</p><h3>Sequential Reads/Writes</h3><ul><li><strong>Definition</strong>: Reading/writing data in contiguous blocks.</li><li>Highly efficient, especially on traditional HDDs due to reduced seek time(head moves linearly).</li><li>Modern SSDs also perform better with sequential access patterns</li><li>Ideal for log-structured data, streaming, and append-only file systems</li></ul><h3>Random Reads/Writes</h3><ul><li><strong>Definition</strong>: Reading/writing non-contiguous blocks. Data is accessed at non-contiguous, arbitrary locations</li><li>Significantly slower on HDDs due to mechanical seek times</li><li>Still more costly than sequential access on SSDs due to internal data structure management and write amplification</li><li>Common in workloads with frequent updates, small I/O sizes, or unstructured access patterns</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9gYsyBVE4Zjobeqm_T0asA.png" /></figure><h3>13. HTTP/1.1 v/s HTTP/2 v/s WebSocket</h3><p>Understanding these protocols is essential for designing modern web and real-time applications, as they define how data is transmitted between clients and servers.</p><h3>HTTP (1.1): One request per connection (head-of-line blocking)</h3><h4><strong>Request-Response Model</strong></h4><ul><li>Client sends a request; server sends back a response.</li><li>Stateless and unidirectional.</li></ul><h4><strong>Limitations</strong></h4><ul><li>One request per TCP connection at a time (known as <em>head-of-line blocking</em>).</li><li>Requires multiple connections for concurrent requests.</li><li>High overhead due to repeated headers.</li></ul><h3>HTTP/2: Multiplexing (multiple requests over one connection)</h3><h4><strong>Improvements over HTTP/1.1</strong></h4><ul><li><strong>Multiplexing:</strong> Multiple streams over a single TCP connection, eliminating head-of-line blocking.</li><li><strong>Header Compression (HPACK):</strong> Reduces overhead by compressing HTTP headers.</li><li><strong>Server Push:</strong> Server can pre-emptively send resources to the client.</li><li>Still follows a <strong>request-response</strong> pattern, not bi-directional messaging.</li></ul><h3>WebSocket: Full-duplex communication (real-time apps like chat)</h3><h4><strong>Full-Duplex, Bi-Directional Communication</strong></h4><ul><li>After an initial HTTP handshake, the connection is <strong>upgraded</strong> to WebSocket.</li><li>Allows both client and server to send messages at any time.</li></ul><h4><strong>Persistent Connection</strong></h4><ul><li>Ideal for real-time apps (e.g., chat, gaming, live data updates).</li><li>Lower overhead after handshake, no need to re-establish connection.</li></ul><h4><strong>When to Use What</strong></h4><p><strong>HTTP: </strong><em>Standard for traditional request-response communication (webpages, APIs).</em></p><p><strong>HTTP/2: </strong><em>Better performance for content-heavy websites and APIs with many resources.</em></p><p><strong>WebSocket: </strong><em>Best for real-time, event-driven applications where the server needs to push updates to the client continuously.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9gYsyBVE4Zjobeqm_T0asA.png" /></figure><h3>14. TCP/IP Model (Internet Protocol Suite)</h3><p>The TCP/IP model is the foundational framework for modern internet communication, consisting of <strong>four layers</strong> (simplified from the OSI model’s seven layers):</p><h4>Layer 1: Network Interface (Link Layer)</h4><ul><li><strong>Function</strong>: Handles physical transmission (Ethernet, Wi-Fi, MAC addresses).</li><li><strong>Key Protocols</strong>: ARP (Address Resolution Protocol), PPP (Point-to-Point Protocol).</li><li><strong>Example</strong>: When your computer sends data over Wi-Fi, this layer converts bits into electrical signals.</li></ul><h4>Layer 2: Internet (Network Layer)</h4><ul><li><strong>Function</strong>: Routes packets across networks using IP addresses.</li><li><strong>Key Protocols</strong>: IP (IPv4/IPv6), ICMP (Ping), BGP (Routing).</li><li><strong>Example</strong>: A router forwarding a packet from your home to a Google server.</li></ul><h4>Layer 3: Transport Layer</h4><ul><li><strong>Function</strong>: Ensures data reliability and flow control.</li><li><strong>Key Protocols</strong>: <br>- <strong>TCP (Transmission Control Protocol)</strong>: Reliable, connection-oriented.<br>- <strong>UDP (User Datagram Protocol)</strong>: Fast, connectionless.</li><li><strong>Example</strong>: Web browsing (TCP) vs. video streaming (UDP).</li></ul><h4>Layer 4: Application Layer</h4><ul><li><strong>Function</strong>: Provides user-facing services (HTTP, FTP, DNS).</li><li><strong>Key Protocols</strong>: HTTP(S), SMTP (Email), DNS, WebSocket.</li><li><strong>Example</strong>: Loading a webpage (HTTP) or sending an email (SMTP).</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9gYsyBVE4Zjobeqm_T0asA.png" /></figure><h3>15. IPv4 v/s IPv6</h3><h4>IPv4 (32-bit addresses)</h4><ul><li><strong>Format</strong>: 192.168.1.1 (4.3 billion unique addresses).</li><li><strong>Problems<br>- Exhaustion</strong>: Running out of addresses (NAT helps but isn’t a fix).<br>- <strong>Security</strong>: No built-in encryption (relies on TLS/IPSec).</li></ul><h4>IPv6 (128-bit addresses)</h4><ul><li><strong>Format</strong>: 2001:0db8:85a3::8a2e:0370:7334 (340 undecillion addresses).</li><li><strong>Advantages<br>- No NAT needed</strong>: Every device gets a unique global IP.<br>- <strong>Built-in security</strong>: IPSec for encryption.<br>- <strong>Efficient routing</strong>: Simplified header structure.</li></ul><h4><strong>Migration Challenges</strong></h4><ul><li>Legacy systems still rely on IPv4.</li><li>Dual-stack networks (IPv4 + IPv6) are common.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9gYsyBVE4Zjobeqm_T0asA.png" /></figure><h3>16. TCP v/s UDP</h3><p>TCP and UDP are two core transport layer protocols in the Internet protocol suite. Choosing between them depends on the trade-off between <strong>reliability</strong> and <strong>performance</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*b2lkrMrDLaqZECRjPPMLOw.png" /></figure><h4><strong>Example</strong></h4><ul><li><strong>TCP</strong>: Downloading a file (needs all packets in order).</li><li><strong>UDP</strong>: Live sports stream (dropped frames are acceptable).</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9gYsyBVE4Zjobeqm_T0asA.png" /></figure><h3>17. DNS Lookup (Domain Name System)</h3><p>DNS (Domain Name System) is the system responsible for translating human-readable domain names (like www.facebook.com) into machine-readable IP addresses (like 157.240.20.35), which are needed to route traffic on the internet.</p><h4><strong>How DNS Lookup Works</strong></h4><ol><li><strong>User Request:<br>- </strong>When you enter a URL in your browser, the system checks for a cached IP address locally.</li><li><strong>Cache Hierarchy (Fastest to Slowest):<br>- Browser Cache<br>- Operating System (OS) Cache<br>- Local DNS Resolver (often provided by ISP)<br>- Authoritative DNS Servers</strong> (last resort)</li><li><strong>DNS Resolution Steps (if not cached):<br>- Root DNS Server:</strong> Directs query to the appropriate top-level domain (TLD) server (.com, .org, etc.)<br>- <strong>TLD DNS Server:</strong> Directs query to the domain’s authoritative nameserver.<br>- <strong>Authoritative DNS Server:</strong> Returns the actual IP address of the domain</li><li><strong>Response:<br>- </strong>The IP address is returned to the client and cached for future use.</li><li><strong>Caching:<br>- </strong>DNS responses are cached at multiple levels to reduce latency and DNS server load.<br>- <strong>TTL (Time to Live)</strong> in DNS records controls how long a result is cached</li></ol><h4><strong>Important Concepts</strong></h4><ul><li><strong>Recursive vs Iterative Queries:</strong></li><li>Recursive resolver handles full lookup for the client</li><li>Iterative queries are step-by-step responses between DNS servers</li></ul><h4><strong>Forward and Reverse DNS</strong></h4><ul><li>Forward DNS: Domain name → IP</li><li>Reverse DNS: IP → Domain name</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9gYsyBVE4Zjobeqm_T0asA.png" /></figure><h3>18. HTTP &amp; TLS (HTTPS)</h3><p>TLS (Transport Layer Security) is a cryptographic protocol that provides <strong>encryption</strong>, <strong>authentication</strong>, and <strong>data integrity</strong> for communications over a network between client and server both in terms of privacy and data integrity.</p><h4>TLS (Transport Layer Security)</h4><ul><li>Operates <strong>on top of TCP</strong> and <strong>beneath application protocols</strong> like HTTP</li><li><strong>Purpose</strong>: Encrypts HTTP traffic (HTTPS = HTTP + TLS).</li><li>Ensures that data transmitted between client and server cannot be intercepted or altered by attackers</li><li>Replaces the older SSL protocol (Secure Sockets Layer), which is now deprecated</li></ul><p><strong>TLS Handshake Overview</strong></p><ol><li><strong>Client Hello:</strong> Client proposes supported TLS versions, cipher suites, and sends a random value</li><li><strong>Server Hello:</strong> Server selects TLS version and cipher, and sends its certificate</li><li><strong>Key Exchange:</strong> Both sides agree on a shared secret using public-key cryptography</li><li><strong>Secure Communication:</strong> Data is encrypted using symmetric keys derived from the handshake</li></ol><p><strong>Why TLS?</strong></p><ul><li>Prevents <strong>eavesdropping</strong> (man-in-the-middle attacks).</li><li>Ensures <strong>data integrity</strong> (no tampering).</li></ul><h4>HTTP (Hypertext Transfer Protocol)</h4><ul><li><strong>Stateless</strong>: Each request is independent (cookies add state).</li><li><strong>Methods</strong>: GET (read), POST (write), PUT (update), DELETE.</li></ul><h4>HTTP + TLS = HTTPS</h4><ul><li>When TLS is used with HTTP, it becomes <strong>HTTPS (HyperText Transfer Protocol Secure)</strong></li></ul><p><strong>HTTPS ensures</strong></p><ul><li><strong>Confidentiality:</strong> Data is encrypted and unreadable to eavesdroppers</li><li><strong>Integrity:</strong> Data can’t be modified in transit without detection</li><li><strong>Authentication:</strong> Verifies the identity of the server using digital certificates</li></ul><h4><strong>Design Implication</strong></h4><ul><li>Always use HTTPS for any data exchange over the public internet</li><li>TLS adds a slight performance overhead but is critical for <strong>security</strong> and <strong>user trust</strong></li><li>Proper certificate management (via Certificate Authorities) is essential for secure deployments</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9gYsyBVE4Zjobeqm_T0asA.png" /></figure><h3>19. Public Key Infrastructure (PKI) &amp; Certificate Authorities (CA)</h3><p>PKI is the system used to manage <strong>public keys</strong>, <strong>digital certificates</strong>, and <strong>trust relationships</strong> between parties on the internet. It enables secure communication through encryption, authentication, and integrity verification.</p><h3>PKI</h3><ul><li>A framework for managing <strong>asymmetric encryption keys</strong> (public/private key pairs)</li><li>Ensures that public keys actually belong to the entities they claim to represent</li><li>Used in securing HTTPS, email, code signing, and more</li></ul><h4><strong>PKI Components</strong></h4><ol><li><strong>Public/Private Keys</strong>: Asymmetric encryption (RSA, ECC).</li><li><strong>Digital Certificates</strong>: Bind a public key to an identity.</li><li><strong>CA (Certificate Authority)</strong>: Trusted entity issuing certificates (DigiCert, Let’s Encrypt).</li></ol><h3>Role of a Certificate Authority (CA):</h3><ul><li>A <strong>trusted third-party organization</strong> that issues <strong>digital certificates</strong></li><li>These certificates bind a public key to a domain or identity (e.g., <a href="http://www.facebook.com)/">www.facebook.com)</a></li><li>The CA verifies the identity of the certificate requester before issuing a certificate</li></ul><h4><strong>How It Works (Example with HTTPS):</strong></h4><ol><li>You type www.facebook.com in your browser</li><li>The browser initiates a <strong>TLS handshake</strong></li><li>The server (Facebook) sends back its <strong>digital certificate</strong>, which includes its public key</li><li>The browser checks the certificate:<br>- Is it <strong>signed by a trusted CA</strong>?<br>- Is it <strong>valid</strong> and <strong>not expired</strong>?<br>- Does the domain name match?</li></ol><p>5. If all checks pass, a secure connection is established.</p><h4><strong>Why This Matters</strong></h4><ul><li>Prevents <strong>man-in-the-middle (MITM) attacks</strong> by verifying you’re talking to the legitimate server</li><li>Ensures <strong>trust and authenticity</strong> on the web</li><li>Forms the backbone of <strong>secure online communication</strong></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9gYsyBVE4Zjobeqm_T0asA.png" /></figure><h3>20. Symmetric vs. Asymmetric Encryption</h3><p>Encryption ensures that data remains confidential and secure from unauthorized access. The two main types are <strong>symmetric</strong> and <strong>asymmetric</strong> encryption, each suited for different purposes.</p><h3>Symmetric Encryption</h3><ul><li><strong>Same key</strong> is used for both <strong>encryption and decryption</strong></li><li>Fast and efficient for <strong>large volumes of data</strong></li><li><strong>Key management</strong> is a challenge — securely sharing the secret key is critical</li></ul><h4><strong>Example</strong></h4><ul><li><strong>AES (Advanced Encryption Standard)</strong> — widely used in applications, file encryption, and secure data transfer</li></ul><h4><strong>Use Cases</strong></h4><ul><li>Bulk data encryption, database encryption, encrypted communication after key exchange</li></ul><h3>Asymmetric Encryption</h3><ul><li>Uses a <strong>key pair</strong>:<br>- <strong>Public Key</strong> to encrypt<br>- <strong>Private Key</strong> to decrypt</li><li>More secure for <strong>key exchange</strong> and <strong>authentication</strong>, but <strong>computationally expensive</strong></li><li>Typically used to <strong>securely exchange a symmetric key</strong>, not for large data encryption</li></ul><h4><strong>Example</strong></h4><ul><li><strong>RSA</strong>, <strong>Elliptic Curve Cryptography (ECC)</strong> — used in TLS handshakes, digital signatures</li></ul><h4><strong>Use Cases</strong></h4><ul><li>Secure key exchange, digital signatures, SSL/TLS, email encryption (e.g., PGP)</li></ul><h4><strong>How They Work Together (Hybrid Approach)</strong></h4><p><strong>⇒ TLS uses both</strong>: Asymmetric for key exchange, symmetric for bulk encryption.</p><ul><li>Asymmetric encryption is used first to <strong>exchange a symmetric key</strong></li><li>Then symmetric encryption is used to encrypt the actual data efficiently</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9gYsyBVE4Zjobeqm_T0asA.png" /></figure><h3>21. Load Balancers (L4 v/s L7)</h3><p>A <strong>load balancer</strong> is a foundational component that distributes incoming client requests across multiple backend servers (nodes) to ensure reliability, scalability, and optimal resource utilization under varying traffic loads.</p><h4><strong>Purpose</strong></h4><ul><li>Avoid overloading any single server</li><li>Improve fault tolerance and high availability</li><li>Reduce response time by efficiently routing traffic</li></ul><h4><strong>How It Works</strong></h4><ul><li>Sits <strong>in front of a service</strong> and acts as an entry point for client requests</li><li>Delegates each request to one of the backend servers based on a chosen algorithm</li></ul><h4><strong>Common Load Balancing Algorithms</strong></h4><ul><li><strong>Round-Robin:</strong> Cycles through each server in order</li><li><strong>Least Connections:</strong> Sends request to the server with the fewest active connections</li><li><strong>Least Load:</strong> Routes based on CPU or resource usage</li><li><strong>IP Hash:</strong> Consistently maps certain clients to specific servers</li></ul><h4><strong>Types of Load Balancers (Based on OSI Model)</strong></h4><ol><li><strong>Layer 4 (Transport Layer):</strong></li></ol><ul><li>Load balancer considers both client and destination <strong>IP address and port numbers</strong> to do the traffic routing.</li><li>Works with TCP/UDP</li><li>Faster and lower-level routing logic</li></ul><p><strong>2. Layer 7 (Application Layer):</strong></p><ul><li>Routes based on <strong>HTTP headers, URIs, cookies, etc.</strong></li><li>Can perform content-based routing (e.g., /api goes to one service, /static to another)</li><li>More flexible, widely used in modern web applications</li></ul><blockquote><em>Most modern load balancers operate at Layer 7 due to the flexibility needed for HTTP-based services.</em></blockquote><h4><strong>Design Implication</strong></h4><ul><li>Use Layer 4 for simple, fast traffic distribution</li><li>Use Layer 7 for intelligent routing based on application data</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9gYsyBVE4Zjobeqm_T0asA.png" /></figure><h3>22. CDNs &amp; Edge Computing</h3><p>Both <strong>CDNs (Content Delivery Networks)</strong> and <strong>Edge Computing</strong> aim to improve performance and reduce latency by bringing content and computation closer to end users.</p><h3>CDN (Content Delivery Network)</h3><ul><li>A <strong>globally distributed network of servers</strong> that cache and deliver content (static or dynamic) to users based on their geographic location</li><li>Reduces the load on the origin server and improves <strong>latency</strong>, <strong>load time</strong>, and <strong>availability</strong></li></ul><h4><strong>How it works (example)</strong></h4><ul><li>If you’re in Chile (CL) and watching a show on Netflix, the video is likely streamed from a <strong>CDN node</strong> near you, not from a remote data center</li><li>This <strong>shortens the distance</strong> data must travel, improving playback performance and reducing buffering</li></ul><h4><strong>Common CDN Content</strong></h4><ul><li>Images, videos, JavaScript, CSS, APIs, and other static assets</li><li><strong>Example</strong>: Netflix stores movies in CDN servers globally.</li></ul><h3>Edge Computing</h3><ul><li>Brings <strong>computation and data storage</strong> closer to the user’s location — often at or near the CDN node</li><li>Unlike CDNs which mostly serve content, <strong>edge nodes can process logic</strong>, such as personalization, analytics, or lightweight computation</li><li><strong>Example</strong>: Smart cities analyzing traffic data at the edge.</li></ul><h4><strong>Key Advantage</strong></h4><ul><li><strong>Dedicated, private backbone networks</strong> from edge locations to central data centers<br>- This ensures <strong>faster</strong>, <strong>more secure</strong>, and <strong>more reliable</strong> communication than traversing the open internet.</li></ul><h4><strong>Differences and Synergy</strong></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SlSf3x1jnwzflt2lFjSauQ.png" /></figure><h4><strong>Design Implication</strong></h4><ul><li>Use <strong>CDNs</strong> to serve content quickly and reduce strain on origin servers</li><li>Use <strong>edge computing</strong> to push computation closer to users (e.g., authentication, analytics)</li><li>Both improve user experience by reducing <strong>latency</strong>, <strong>bandwidth usage</strong>, and <strong>response times</strong></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9gYsyBVE4Zjobeqm_T0asA.png" /></figure><h3>23. Bloom Filters &amp; Count-Min Sketch</h3><p>These are <strong>space-efficient, probabilistic data structures</strong> used when approximate answers are acceptable in exchange for saving memory and computational cost.</p><h3>Bloom Filter</h3><ul><li><strong>Probabilistic data structure</strong>: Checks if an element <strong>might</strong> be in a set.</li><li>Extremely <strong>space-efficient</strong>, especially when the number of elements is large</li></ul><h4><strong>Properties</strong></h4><ul><li>May return <strong>false positives</strong> (says an element exists when it doesn’t)</li><li><strong>Never returns false negatives</strong> (if it says it’s not present, it’s definitely not)</li></ul><h4><strong>Use Cases</strong></h4><ul><li>Preventing duplicate processing (e.g., web crawlers, cache lookups)</li><li>Checking membership in a large set (e.g., checking if a password was seen in a data breach)</li><li>Spell checkers, blockchain (avoiding duplicate transactions).</li></ul><h4><strong>Design Implication</strong></h4><ul><li>Ideal for scenarios where some inaccuracy (false positives) is tolerable but space savings are critical</li></ul><h3>Count-Min Sketch</h3><ul><li>Used to <strong>estimate/approximate the frequency of events</strong> in a data stream</li><li>Stores frequencies using a fixed amount of space</li></ul><h4><strong>Properties</strong></h4><ul><li>Guarantees <strong>no underestimation</strong> of counts (never reports fewer than actual)</li><li>Can <strong>overestimate</strong> due to hash collisions</li><li>Error is <strong>bounded and controllable</strong> with tunable parameters</li></ul><h4><strong>Use Cases</strong></h4><ul><li>Counting most frequent items (e.g., top queries, trending hashtags)</li><li>Monitoring real-time analytics where exact counts aren’t required (e.g., DDoS detection)</li></ul><h4><strong>Design Implication</strong></h4><ul><li>Useful when exact counts are not needed but memory must be conserved</li><li>Returns <strong>“close enough”</strong> answers quickly and efficiently, suitable for high-throughput systems</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9gYsyBVE4Zjobeqm_T0asA.png" /></figure><h3>24. Paxos &amp; Leader Election</h3><p>In distributed systems, <strong>consensus</strong> is the process of getting multiple nodes to agree on a single value — even in the presence of failures. This is a fundamental challenge in distributed computing.</p><h3>Paxos</h3><ul><li>Proposed by Leslie Lamport, Paxos is known for being <strong>theoretically sound</strong> but <strong>complex to implement</strong></li><li><strong>Paxos</strong> is a family of protocols designed to achieve <strong>consensus</strong> among a group of distributed nodes</li><li>It ensures that <strong>a single value is agreed upon</strong>, even if some nodes fail or messages are delayed</li></ul><h4><strong>Phases</strong></h4><ol><li><strong>Prepare</strong>: Proposer suggests a value.</li><li><strong>Accept</strong>: Nodes agree on the value.</li></ol><h4><strong>Key Features</strong></h4><ul><li>Guarantees <strong>safety</strong>: nodes never decide on different values</li><li>Tolerates <strong>partial failures</strong> and network partitions</li><li>Ensures <strong>liveness</strong> if a majority of nodes are up and communicating</li></ul><h4><strong>Use Cases</strong></h4><ul><li><strong>Leader election</strong> in distributed clusters</li><li>Replicated state machines (e.g., Raft is a simplified Paxos variant used in systems like etcd, Consul)</li><li>Distributed databases, configuration stores, and log replication (e.g., Google’s Chubby lock service)</li></ul><h3>Leader Election</h3><ul><li>A process used in distributed systems to <strong>select a single node (the leader)</strong> to coordinate tasks</li><li><strong>Consensus protocols like Paxos</strong> help safely elect and agree on a leader</li></ul><h4><strong>Methods</strong></h4><ul><li><strong>Bully Algorithm</strong>: Highest ID node wins.</li><li><strong>Raft</strong>: Simplified Paxos alternative (used in Kubernetes).</li></ul><h4><strong>Why it matters</strong></h4><ul><li>Many systems use a <strong>leader-based architecture</strong> to simplify coordination (e.g., a leader managing writes)</li><li>If the leader fails, a <strong>new leader must be elected</strong> quickly and consistently to maintain availability</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9gYsyBVE4Zjobeqm_T0asA.png" /></figure><h3>25. Design Patterns &amp; Object-Oriented Design</h3><p>These are foundational concepts in software engineering that help in writing <strong>clean</strong>, <strong>reusable</strong>, and <strong>maintainable</strong> code.</p><h3>Design Patterns</h3><p>Design patterns are <strong>proven solutions</strong> to common problems in software design. They offer a way to <strong>structure code</strong> in a consistent and understandable manner.</p><h4><strong>Examples to Know</strong></h4><p><strong>Factory Method:</strong></p><ul><li>Used to create objects without specifying the exact class</li><li>Promotes loose coupling by relying on interfaces</li></ul><p><strong>Singleton:</strong></p><ul><li>Ensures only one instance of a class exists and provides a global access point (e.g., database connection).</li><li>Often used for configuration, logging, or caching services</li></ul><h4>Other useful patterns</h4><ul><li>Observer: Event-driven systems (e.g., pub-sub).</li><li>Strategy</li><li>Adapter</li><li>Decorator</li><li>Proxy</li></ul><blockquote><em>Design patterns help standardize solutions, reduce boilerplate, and improve team understanding.</em></blockquote><h3>Object-Oriented Design (OOD)</h3><p>OOD involves designing software using the principles of <strong>object-oriented programming (OOP)</strong> — focusing on <strong>objects</strong>, <strong>classes</strong>, and their interactions.</p><h4><strong>Core Concepts to Understand</strong></h4><p><strong>Abstraction:</strong></p><ul><li>Hiding internal implementation details and exposing only necessary parts</li></ul><p><strong>Encapsulation:</strong></p><ul><li>Grouping related data and behavior into a class and restricting/hiding direct access to internal state</li></ul><p><strong>Inheritance:</strong></p><ul><li>Creating new classes from existing ones to promote code reuse via parent-child classes.</li></ul><p><strong>Polymorphism:</strong></p><ul><li>Allowing different classes to be treated through the same interface (e.g., via method overriding). One interface, multiple implementations.</li></ul><blockquote><em>OOD helps you model systems that are modular, scalable, and easier to evolve over time.</em></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9gYsyBVE4Zjobeqm_T0asA.png" /></figure><h3>26. Virtual Machines (VMs) and Containers</h3><p>Understanding both these concepts is crucial for building modern, scalable, and maintainable systems. Both virtual machines (VMs) and containers allow you to run software in <strong>isolated environments</strong>, but they differ in how they achieve this isolation.</p><h3>Virtual Machines (VMs)</h3><ul><li>Virtual Machines are a way of giving us an operating system on top of shared resource such that we feel like we are the exclusive owner of this hardware while in reality that hardware is shared between different isolated operating systems.</li><li>A <strong>VM emulates an entire operating system</strong>, including its own kernel</li><li>Runs on top of a <strong>hypervisor</strong> (e.g., VMware, KVM, Hyper-V) that manages multiple VMs on the same physical hardware</li></ul><h4><strong>Key Features</strong></h4><ul><li>Each VM has its own OS, memory, CPU, and storage</li><li><strong>Strong isolation</strong> between environments</li><li>Useful for running different OS types on the same hardware</li></ul><h4><strong>Trade-Off</strong></h4><ul><li><strong>Heavyweight</strong>: Each VM has its own OS, which leads to more resource usage</li><li>Slower boot times compared to containers</li></ul><h3>Containers</h3><ul><li>Containers is a way of running our applications and its dependencies in an isolated environment.</li><li>A <strong>lightweight alternative to VMs</strong>, sharing the host OS kernel but isolating the application and its dependencies</li><li>Managed via tools like <strong>Docker</strong> and orchestrated with <strong>Kubernetes</strong></li></ul><h4><strong>Key Features</strong></h4><ul><li>Faster startup and lower resource usage compared to VMs</li><li>Portability: “Run anywhere” consistency (dev → test → prod)</li><li>Ideal for <strong>microservices</strong> and cloud-native architectures</li></ul><h4><strong>Trade-Off</strong></h4><ul><li><strong>Less isolated</strong> than VMs since they share the host OS kernel</li><li>Security boundaries are tighter but not as strong as full virtualization</li></ul><h4><strong>Design Implication</strong></h4><ul><li>Use <strong>VMs</strong> when you need strong OS-level isolation or need to run multiple OSes on the same hardware</li><li>Use <strong>containers</strong> for <strong>application-level isolation</strong>, faster deployment, and modern DevOps workflows. Containers are the backbone of scalable, cloud-native applications today</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9gYsyBVE4Zjobeqm_T0asA.png" /></figure><h3>27. Pub-Sub Architecture (Publisher-Subscriber Pattern)</h3><p>Pub-sub (publish-subscribe) is a <strong>messaging pattern</strong> where <strong>producers (publishers)</strong> send messages to a <strong>message broker</strong>, and <strong>consumers (subscribers)</strong> receive messages from it. Used in real-time notifications (Kafka, RabbitMQ).</p><h4>How It Works</h4><ul><li><strong>Publisher</strong> → sends message to a <strong>topic or queue</strong></li><li><strong>Message Broker</strong> → handles delivery (e.g., Kafka, RabbitMQ, NATS)</li><li><strong>Subscriber</strong> → receives messages asynchronously</li></ul><h4><strong>Key Features</strong></h4><ul><li><strong>Decouples</strong> producers and consumers</li><li>Supports <strong>asynchronous communication</strong></li><li>Enables <strong>scalability</strong> and <strong>resilience</strong></li></ul><h4><strong>Design Considerations</strong></h4><ul><li>Great for <strong>event-driven systems</strong>, <strong>log aggregation</strong>, <strong>metrics pipelines</strong>, etc.</li><li>Do <strong>not use pub-sub directly</strong> for handling <strong>customer-facing requests</strong> (e.g., web UI actions), because:</li><li>Pub-sub introduces <strong>eventual consistency</strong> and <strong>processing delays</strong></li><li>Clients typically expect <strong>immediate</strong> and <strong>reliable</strong> responses</li></ul><blockquote><em>Instead, use pub-sub for background processing, batch jobs, or internal service communication</em></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9gYsyBVE4Zjobeqm_T0asA.png" /></figure><h3>28. MapReduce</h3><p><strong>MapReduce</strong> is a programming model used for <strong>distributed and parallel processing</strong> of large-scale data sets across clusters of machines. It is ideal for tasks like <strong>log processing</strong>, <strong>analytics</strong>, <strong>indexing</strong>, and <strong>ETL pipelines.</strong> Knowing how it works helps when working with <strong>big data platforms</strong> or designing <strong>batch data workflows.</strong> Used in Hadoop, BigQuery.</p><h4><strong>Key Components</strong></h4><ul><li><strong>Map Phase</strong>: Filters/sorts data (e.g., count words in documents)</li><li>Takes input data and transforms it into a set of <strong>intermediate key-value pairs</strong></li><li>Often used for <strong>filtering</strong>, <strong>parsing</strong>, and <strong>organizing</strong> raw data</li><li><strong>Reduce Phase</strong>: Aggregates results (e.g., sum word counts)</li><li>Processes the intermediate key-value pairs generated by the map phase</li><li><strong>Aggregates</strong>, <strong>summarizes</strong>, or <strong>computes statistics</strong> from grouped keys</li></ul><h4><strong>Example Use Case</strong></h4><ul><li><strong>Counting word frequency</strong> in a massive dataset:</li><li><strong>Map</strong>: Emits (word, 1) for each word in the input</li><li><strong>Reduce</strong>: Sums all the 1s for each word to get total count</li></ul><h4><strong>Why It Matters</strong></h4><ul><li><strong>Scales horizontally</strong> to handle petabytes of data</li><li>Handles <strong>fault tolerance</strong> by re-executing failed tasks</li><li>Foundation for many big data tools (e.g., Hadoop, Hive, Spark)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9gYsyBVE4Zjobeqm_T0asA.png" /></figure><h3>29. Multithreading, Locks, Synchronization, and CAS</h3><p>In high-performance and concurrent systems, <strong>managing access to shared resources</strong> is critical. M<strong>ultithreading</strong> is essential for utilizing CPU cores effectively and increasing throughput. However, it introduces challenges around <strong>shared state</strong>, requiring proper coordination using tools like <strong>locks</strong>, <strong>synchronization</strong>, and <strong>atomic operations</strong> like <strong>CAS</strong>.</p><h3>Multithreading</h3><ul><li><strong>Multithreading</strong> allows a program to run multiple threads in parallel within the same process</li><li>Helps in <strong>utilizing multiple CPU cores</strong>, improving throughput and responsiveness</li><li>Common in real-time systems, UI applications, and high-performance servers</li></ul><h4><strong>Challenge</strong></h4><ul><li>Threads share memory → introduces risk of <strong>race conditions</strong> and <strong>inconsistent state</strong></li></ul><h3>Locks</h3><ul><li>A <strong>lock</strong> prevents multiple threads from accessing a shared resource <strong>at the same time</strong></li><li>Helps maintain <strong>mutual exclusion</strong> and <strong>data consistency</strong></li></ul><h4><strong>Types</strong></h4><ul><li><strong>Mutex (mutual exclusion)</strong>: Only one thread can hold the lock at a time</li><li><strong>Reentrant lock</strong>: Allows a thread to re-acquire a lock it already holds</li><li><strong>Read-write lock</strong>: Allows multiple readers or one writer</li><li><strong>Semaphore</strong>: Limits concurrent access (e.g., database connections).</li></ul><h4><strong>Downside</strong></h4><ul><li>Poorly designed locking leads to <strong>deadlocks</strong>, <strong>starvation</strong>, and <strong>performance bottlenecks</strong></li></ul><h3>Synchronization</h3><ul><li><strong>Synchronization</strong> ensures that multiple threads execute code in a <strong>safe and coordinated</strong> manner</li><li>Can be achieved using:</li><li><strong>Synchronized blocks/methods</strong> (e.g., in Java)</li><li><strong>Condition variables</strong>, <strong>monitors</strong>, <strong>semaphores</strong></li></ul><h4><strong>Goal</strong></h4><ul><li>Prevent unexpected interleaving of operations on shared resources</li></ul><h3>CAS (Compare-and-Set / Compare-and-Swap)</h3><ul><li>A <strong>lock-free mechanism</strong> used to update shared data safely without traditional locks</li><li>Atomically compares a memory location’s current value with an expected value, and <strong>updates it if they match</strong></li><li><strong>Atomic operation</strong>: Updates a value only if unchanged.</li><li><strong>Use Case</strong>: Lock-free algorithms (Java’s AtomicInteger).</li></ul><h4><strong>Benefits</strong></h4><ul><li>Enables <strong>non-blocking algorithms</strong></li><li>Reduces contention and improves scalability in multithreaded environments</li></ul><h4><strong>Example Use Cases</strong></h4><ul><li>Implementing atomic counters, queues, and lock-free data structures</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9gYsyBVE4Zjobeqm_T0asA.png" /></figure><h3>30. Authentication &amp; Authorization</h3><ul><li><strong><em>Authentication</em></strong><em>: Verifying </em><strong><em>who</em></strong><em> the user is</em></li><li><strong><em>Authorization</em></strong><em>: Determining </em><strong><em>what</em></strong><em> the user is allowed to do</em></li></ul><p>These are <strong>distinct</strong> but often work together to secure systems.</p><p><strong>Common Authentication &amp; Authorization Protocols and Standards</strong></p><h3>LDAP (Lightweight Directory Access Protocol)</h3><ul><li>A <strong>protocol</strong> used to access and maintain <strong>directory services</strong> (e.g., user accounts, group memberships)</li><li>Commonly used in enterprise settings for <strong>centralized user management</strong></li><li>Not an auth protocol by itself but used with authentication layers</li></ul><h3>SAML (Security Assertion Markup Language)</h3><ul><li>An <strong>XML-based</strong> standard for <strong>Single Sign-On (SSO)</strong></li><li>Common in enterprise environments to allow users to authenticate once and access multiple services</li><li>Relies on a <strong>trusted identity provider (IdP)</strong> and a <strong>service provider (SP)</strong></li><li><strong>Flow</strong>: IdP authenticates user → Issues a SAML assertion → SP trusts the assertion and grants access</li></ul><h3>Kerberos</h3><ul><li>A <strong>ticket-based authentication protocol</strong> used primarily in enterprise and Windows domains</li><li>Uses <strong>symmetric key cryptography</strong> and a <strong>trusted Key Distribution Center (KDC)</strong></li><li>Offers features like Mutual authentication, No passwords sent over the network, etc.</li><li><strong>Flow</strong>: User logs in → Gets a ticket-granting ticket (TGT) → Requests access to services with service tickets</li></ul><h3>OAuth2</h3><ul><li>A widely used <strong>authorization</strong> framework, especially in web and API-based systems</li><li>Allows third-party applications to access user resources <strong>without sharing credentials</strong></li><li>OAuth2 by itself is for <strong>authorization</strong>. For <strong>authentication</strong>, it’s often paired with <strong>OpenID Connect (OIDC)</strong>.</li></ul><h4><strong>Key Roles</strong></h4><ul><li><strong>Resource Owner</strong>: The user</li><li><strong>Client</strong>: App requesting access</li><li><strong>Authorization Server</strong>: Grants tokens</li><li><strong>Resource Server</strong>: Hosts protected resources</li></ul><h4><strong>Flows</strong></h4><ul><li><strong>Authorization Code</strong> (most secure, used with server-side apps)</li><li><strong>Implicit</strong> (legacy, for browser-based apps)</li><li><strong>Client Credentials</strong> (for machine-to-machine)</li><li><strong>Password Grant</strong> (legacy, direct user credentials)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9gYsyBVE4Zjobeqm_T0asA.png" /></figure><h3>31. Advanced Networking (CIDR, VPN, Subnet, Broadcast)</h3><h3>CIDR (Classless Inter-Domain Routing)</h3><ul><li>CIDR is a method for <strong>allocating IP addresses</strong> and <strong>routing IP packets</strong> more efficiently than traditional IP classes (A/B/C)</li><li>Uses a <strong>slash notation</strong> (e.g., 192.168.0.0/24) to indicate the <strong>network prefix</strong></li><li>/24 means the first 24 bits are the <strong>network part</strong>, and the remaining bits are for <strong>hosts</strong></li><li>Enables <strong>flexible IP address allocation</strong> and <strong>route aggregation</strong>, reducing the size of routing tables</li></ul><h4><strong>Example</strong></h4><p>192.168.1.0/24 covers IPs from 192.168.1.0 to 192.168.1.255</p><h3>Subnet (Subnetting)</h3><ul><li>Subnetting divides a large network into <strong>smaller, manageable segments (subnets)</strong></li><li>Helps improve <strong>network performance</strong>, <strong>security</strong>, and <strong>address utilization</strong></li><li>Each subnet has:</li><li>A <strong>network address</strong></li><li>A <strong>broadcast address</strong></li><li>A range of <strong>usable IP addresses</strong></li></ul><h4><strong>Example</strong></h4><ul><li>10.0.0.0/16 can be split into:</li><li>10.0.0.0/24, 10.0.1.0/24, ..., 10.0.255.0/24</li></ul><h4><strong>Benefits</strong></h4><ul><li>Isolates broadcast domains</li><li>Enables routing policies and access control per subnet</li></ul><h3>VPN (Virtual Private Network)</h3><ul><li>A VPN creates a <strong>secure, encrypted tunnel</strong> over an untrusted network (e.g., the internet)</li><li>Allows users or branches to <strong>securely connect to a private network</strong> (e.g., company intranet)</li><li>Common types:</li><li><strong>Site-to-site VPN</strong>: Connects two entire networks</li><li><strong>Remote-access VPN</strong>: Connects a single client to a network</li><li><strong>SSL VPN / IPSec VPN</strong>: Based on different tunneling/encryption protocols</li></ul><h4><strong>Use Cases</strong></h4><ul><li>Secure remote work</li><li>Connecting geographically distributed data centers</li><li>Bypassing geo-restrictions (personal use)</li></ul><h3>Broadcast</h3><ul><li>A <strong>broadcast</strong> sends a packet from one device to <strong>all devices on the local network segment</strong></li><li>Broadcasts are used for:</li><li>ARP (Address Resolution Protocol)</li><li>DHCP discovery</li><li>Only works within the <strong>same subnet</strong> or broadcast domain</li></ul><h4><strong>Limitations</strong></h4><ul><li>Can lead to <strong>network congestion</strong> if overused</li><li>Routers <strong>do not forward broadcasts</strong> across subnets, which is why subnetting helps control broadcast traffic</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9gYsyBVE4Zjobeqm_T0asA.png" /></figure><h3>32. Data Lakes v/s Data Warehouses</h3><p>Both are used for storing large volumes of data, but they serve different purposes and are optimized for different use cases.</p><h3>Data Lake</h3><ul><li>A <strong>centralized repository</strong> for storing <strong>raw, unstructured, semi-structured, or structured data</strong></li><li>Designed to store <strong>data in its native format</strong> (e.g., JSON, CSV, logs, images, Parquet)</li><li>Built for <strong>scalability</strong> and <strong>cost-effective storage</strong> of massive volumes of data</li></ul><h4><strong>Key Characteristics</strong></h4><ul><li>Schema-on-read: Structure is applied <strong>when the data is read</strong>, not when it’s written</li><li>Stores data from various sources: logs, IoT streams, click data, social feeds</li><li>Ideal for <strong>data scientists</strong>, <strong>ML workloads</strong>, and <strong>big data analytics</strong></li></ul><h4><strong>Common technologies</strong></h4><ul><li>AWS S3, Azure Data Lake, Hadoop HDFS, Delta Lake, Apache Iceberg</li></ul><h3>Data Warehouse</h3><ul><li>A <strong>structured repository</strong> designed for <strong>reporting</strong>, <strong>business intelligence</strong>, and <strong>analytical queries</strong></li><li>Optimized for <strong>fast querying</strong> and <strong>complex joins</strong> over structured data</li></ul><h4><strong>Key Characteristics</strong></h4><ul><li>Schema-on-write: Data must be <strong>cleaned, transformed, and structured before storage</strong></li><li>Supports <strong>OLAP (Online Analytical Processing)</strong></li><li>Designed for <strong>analysts</strong>, <strong>BI tools</strong>, and <strong>dashboarding</strong></li></ul><h4><strong>Common technologies</strong></h4><ul><li>Amazon Redshift, Google BigQuery, Snowflake, Apache Hive, Azure Synapse</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*ri9ZI4v6wKoeQOK3NxvSDw.png" /></figure><h3>Essential System Design Tools</h3><blockquote>Now it is time to move into the second part of our discussion where we will discuss some of the majorly required tools while designing the system.</blockquote><h3>1. Cassandra (Wide-Column NoSQL Database)</h3><p><strong>Apache Cassandra</strong> is a <strong>highly scalable</strong>, <strong>distributed NoSQL database</strong> designed for <strong>high availability</strong>, <strong>fault tolerance</strong>, and <strong>massive write throughput</strong>.</p><h4>Key Features</h4><ul><li><strong>Wide-column store</strong>: Data is organized into tables with rows and dynamic columns, optimized for large-scale analytics (not fixed schema like RDBMS)</li><li><strong>Distributed architecture</strong>: No single point of failure; data is <strong>replicated across multiple nodes</strong> (gossip protocol for cluster health).</li><li><strong>High Scalability</strong>: Linear scaling by adding more nodes.</li><li><strong>High availability</strong>: Designed to handle <strong>node failures without downtime</strong></li><li><strong>Partitioning</strong>: Uses <strong>consistent hashing</strong> to distribute data.</li><li><strong>Supports massive write throughput</strong> and is often used in <strong>time-series</strong>, <strong>log</strong>, or <strong>sensor data</strong> scenarios</li><li><strong>Consistency Models</strong>:<br>- <strong>Eventual Consistency</strong> (default, but tunable to <strong>strong consistency</strong> using read/write consistency levels): Faster writes, but reads may be stale.<br>- <strong>Strong Consistency</strong>: Configurable (quorum reads/writes).</li></ul><h4>How It Works</h4><ul><li><strong>Sharding (partitioning)</strong>: Uses <strong>consistent hashing</strong> to distribute data evenly across nodes</li><li><strong>Replication</strong>: Data is replicated across nodes for redundancy (Replication Factor determines this)</li><li><strong>Gossip Protocol</strong>: Lightweight communication mechanism where nodes share state and health info with each other</li><li><strong>Tunable Consistency</strong>:</li><li>ONE, QUORUM, ALL levels can be configured per query</li><li>You can balance between latency and consistency</li></ul><h4>Use Cases</h4><ul><li><em>Time-series data (metrics, logs)</em></li><li><em>Real-time analytics</em></li><li><em>IoT and sensor data storage</em></li><li><em>High-volume user activity feeds</em></li><li><em>Systems requiring </em><strong><em>linear scalability</em></strong><em> and </em><strong><em>geographical distribution</em></strong></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*ri9ZI4v6wKoeQOK3NxvSDw.png" /></figure><h3>2. MongoDB / Couchbase (Document Databases)</h3><h3>MongoDB</h3><p><strong>MongoDB</strong> is a <strong>document-based NoSQL database</strong> designed for <strong>flexibility</strong>, <strong>horizontal scalability</strong>, and <strong>developer productivity</strong>.</p><ul><li>Ideal for applications with <strong>changing schemas</strong> or <strong>hierarchical data</strong></li><li>Denormalization is encouraged for performance</li><li>Great for <strong>developer-friendly data modeling</strong> and <strong>JSON-native applications</strong></li></ul><h4>Key Features</h4><ul><li>Stores data in <strong>BSON</strong> (binary JSON) documents — ideal for semi-structured data</li><li><strong>Schema-less</strong>: Documents in the same collection can have different fields</li><li>Supports <strong>replication</strong>, <strong>sharding</strong>, and <strong>automatic failover</strong></li><li><strong>Powerful querying</strong>, indexing, and aggregation framework</li><li>Tunable <strong>consistency</strong>: default is eventual, but can be configured for stronger consistency</li></ul><h4>Use Cases</h4><ul><li><em>Content management systems</em></li><li><em>Real-time analytics dashboards</em></li><li><em>Catalogs, user profiles, and logs</em></li><li><em>Rapid prototyping and development</em></li></ul><h3>Couchbase</h3><p><strong>Couchbase</strong> is a distributed, high-performance <strong>NoSQL document store</strong> with a strong emphasis on <strong>performance</strong>, <strong>scalability</strong>, and <strong>low latency access</strong>.</p><ul><li>Suitable for applications needing <strong>high throughput and low latency</strong></li><li>Supports <strong>mobile and offline-first architectures</strong></li><li>Requires more operational expertise for managing multiple service roles (data, index, query, etc.)</li></ul><h4>Key Features</h4><ul><li>Stores JSON documents with <strong>optional schemas</strong></li><li>Built-in <strong>key-value engine</strong>, <strong>full-text search</strong>, <strong>eventing</strong>, and <strong>mobile sync</strong></li><li>Uses <strong>Memcached-style in-memory caching layer</strong> for fast access</li><li>Offers <strong>SQL-like N1QL</strong> query language</li><li>Supports <strong>multi-dimensional scaling</strong>: separate services for data, indexing, and querying</li><li>Designed for <strong>active-active</strong> deployments with <strong>cross-data center replication (XDCR)</strong></li></ul><h4>Use Cases</h4><ul><li><em>High-speed user session storage</em></li><li><em>Real-time personalization and recommendation</em></li><li><em>Mobile/IoT apps (with </em><strong><em>Couchbase Mobile</em></strong><em> and offline sync)</em></li><li><em>Applications requiring </em><strong><em>high availability</em></strong><em> and </em><strong><em>in-memory performance</em></strong></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*ri9ZI4v6wKoeQOK3NxvSDw.png" /></figure><h3>3. MySQL (Relational Database)</h3><p><strong>MySQL</strong> is a widely used <strong>open-source relational database management system (RDBMS)</strong> that uses <strong>SQL (Structured Query Language)</strong> to manage data.</p><ul><li>Great for <strong>structured data with clear schema</strong></li><li>Use <strong>indexes</strong> wisely for performance</li><li>Normalize data to avoid redundancy, denormalize selectively for performance</li><li>Requires care with scaling: vertical scaling is easy, horizontal scaling (sharding) is harder and often done via middleware or tools like Vitess</li></ul><h4>Key Features</h4><ul><li><strong>Relational</strong>: Organizes data into <strong>tables with rows and columns</strong></li><li><strong>ACID compliant</strong> (Atomicity, Consistency, Isolation, Durability) for reliable transactions</li><li><strong>Indexing</strong>: B-trees for fast lookups.</li><li><strong>Partitioning</strong>: Horizontal scaling via sharding (but complex).</li><li>Supports <strong>foreign keys</strong>, <strong>joins</strong>, <strong>indexes</strong>, <strong>views</strong>, <strong>stored procedures</strong></li><li><strong>Master-Slave Replication</strong>: Read scalability.</li><li>Multiple <strong>storage engines</strong>, most commonly:<br>- <strong>InnoDB</strong> (default) — supports ACID and transactions<br>- <strong>MyISAM</strong> — optimized for read-heavy workloads (no transactions)</li><li>Scalable through <strong>replication</strong>, <strong>sharding</strong>, and <strong>partitioning</strong></li><li>Strong support for <strong>backups</strong>, <strong>security</strong>, and <strong>role-based access control</strong></li></ul><pre>Write → Master | Reads → Replicas (Slaves)</pre><h4>Strengths</h4><ul><li>Mature, stable, and well-supported</li><li>ACID guarantees ensure data integrity</li><li>Large community and extensive tooling</li><li>Works well for transactional systems</li></ul><h4>Limitations</h4><ul><li>Not ideal for unstructured or schema-less data</li><li>Horizontal scalability is limited without external tools</li><li>JOIN performance can degrade with very large datasets</li></ul><h4>Use Cases</h4><ul><li><em>Traditional OLTP (Online Transaction Processing) systems</em></li><li><em>E-commerce platforms</em></li><li><em>Financial systems and payment processing</em></li><li><em>Web and mobile apps needing </em><strong><em>structured data</em></strong></li><li><em>Content management systems (e.g., WordPress, Joomla)</em></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*ri9ZI4v6wKoeQOK3NxvSDw.png" /></figure><h3>4. Memcached v/s Redis (Caching)</h3><p><strong>Memcached</strong> and <strong>Redis</strong> are both popular <strong>in-memory distributed caching systems</strong> designed to store and retrieve data with extremely low latency. However, they differ significantly in functionality and use cases.</p><h3>Memcached</h3><ul><li><strong>Simple key-value store</strong>, optimized for <strong>caching only</strong></li><li><strong>Memory-only</strong>, with <strong>no persistence</strong> (data is lost on restart)</li><li><strong>Multi-threaded</strong>: Can handle very high throughput with concurrent connections</li><li>Very lightweight and <strong>minimal overhead</strong></li><li>Ideal for <strong>simple session storage</strong>, <strong>API response caching</strong>, and <strong>object caching</strong></li><li><strong>No advanced data structures</strong>, scripting, or pub/sub capabilities</li></ul><h3>Redis</h3><ul><li><strong>Key-value store with rich data structures</strong>:<br>- Strings, Lists, Hashes, Sets, Sorted Sets, Streams, Bitmaps, HyperLogLogs, etc.</li><li>Supports <strong>persistence</strong>:<br>- <strong>RDB snapshots</strong> (point-in-time)<br>- <strong>AOF (Append-Only File)</strong> for durable writes</li><li>Supports <strong>Redis Cluster</strong> for <strong>sharding</strong>, <strong>high availability</strong>, and <strong>replication</strong></li><li><strong>Single-threaded</strong> core (with I/O threading support), still delivers high performance</li><li>Offers <strong>Pub/Sub</strong>, <strong>Lua scripting</strong>, and <strong>stream processing</strong></li><li>Ideal for:<br><em>- Real-time analytics<br>- Leaderboards<br>- Queues<br>- Distributed locks<br>- Rate limiting</em></li></ul><h4>Key Design Considerations</h4><ul><li><strong>In-memory systems</strong>: Both are constrained by the amount of RAM available on the host</li><li><strong>Not a source of truth</strong>: Should be used only as a <strong>caching layer</strong>, not as a primary database</li><li><strong>Eviction Policies</strong>: Both offer strategies like LRU, LFU, etc., for handling memory limits</li><li><strong>Latency-sensitive</strong>: Commonly used to reduce database load and improve response times</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*ri9ZI4v6wKoeQOK3NxvSDw.png" /></figure><h3>5. ZooKeeper (Coordination Service)</h3><p>Apache ZooKeeper is a <strong>highly available, consistent coordination service</strong> for distributed systems. It provides primitives for configuration management, synchronization, and naming.</p><h4>Key Features</h4><ul><li><strong>Centralized Configuration Management</strong> Stores <strong>small, critical metadata</strong> such as cluster states, configuration files, and service discovery information.</li><li><strong>Leader Election</strong> Commonly used by distributed systems like <strong>Kafka</strong> and <strong>Hadoop</strong> to ensure a <strong>single active master</strong>.</li><li><strong>Distributed Locks</strong> Offers <strong>synchronization primitives</strong> like locks and barriers to coordinate processes across nodes.</li><li><strong>ZAB Protocol (ZooKeeper Atomic Broadcast)</strong> Ensures <strong>sequential consistency</strong> across nodes using atomic message broadcasting — core to ZooKeeper’s <strong>CP</strong> (Consistency + Partition Tolerance) nature in the CAP theorem.</li></ul><h4>Performance Characteristics</h4><ul><li><strong>Read-Optimized</strong>: Scales <strong>very well for read-heavy workloads</strong>.</li><li><strong>Write Limitations</strong>: Write throughput is <strong>limited</strong>, as writes must be replicated to a quorum.</li><li><strong>In-Memory Storage</strong>: Keeps all data in <strong>memory</strong>, making it fast but unsuitable for large datasets.</li></ul><h4>Limitations</h4><ul><li><strong>Not Designed for Large Data Volumes</strong> Stores all data in memory, so it’s only suitable for <strong>small-sized metadata</strong>.</li><li><strong>Limited Write Scalability</strong> Due to strong consistency guarantees, <strong>write operations</strong> are slower and do not scale horizontally.</li></ul><h4>Common Use Cases</h4><ul><li><strong>Kafka Controller Election</strong><br><em>- Ensures that only one Kafka broker acts as the controller at any time.</em></li><li><strong>Hadoop NameNode Failover</strong> <br><em>- Helps coordinate </em><strong><em>active/passive failover</em></strong><em> between NameNodes.</em></li><li><strong>Service Discovery and Configuration Management</strong> <br><em>- Enables services to </em><strong><em>register themselves</em></strong><em> and </em><strong><em>fetch configuration values</em></strong><em> dynamically.</em></li><li><strong>Distributed Locks and Barriers</strong> <br><em>- Coordinates tasks that must run in a mutually exclusive or synchronized fashion.</em></li></ul><h4>When to Use ZooKeeper</h4><p>Use ZooKeeper when you need:</p><ul><li><em>Strong consistency</em></li><li><em>High availability</em></li><li><em>Coordination between distributed services</em></li><li><em>Reliable, centralized metadata management</em></li></ul><p>But <strong>avoid using it</strong> for large-scale data storage or write-heavy operations.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*ri9ZI4v6wKoeQOK3NxvSDw.png" /></figure><h3>6. Kafka (Distributed Event Streaming Platform)</h3><p><strong>Apache Kafka</strong> is a <strong>fault-tolerant, highly available distributed system</strong> used for <strong>real-time data streaming</strong>, <strong>messaging</strong>, and <strong>event-driven architecture</strong>.</p><h4>Key Features</h4><ul><li><strong>Pub-Sub Model with Topics &amp; Partitions</strong> <br>- Producers publish messages to <strong>topics</strong>, which are divided into <strong>partitions</strong> for parallelism and scalability. <br>- Consumers subscribe to topics and process messages.</li><li><strong>Fault Tolerance</strong> <br>- Kafka ensures high availability via <strong>replicated partitions</strong> across multiple brokers. <br>- Even if a broker fails, data is preserved and available from replicas.</li><li><strong>Exactly-Once Semantics (EOS)</strong> <br>- Supports <strong>exactly-once delivery</strong> in Kafka Streams and with idempotent producers + transactional consumers — critical for financial or stateful workflows.</li><li><strong>Message Ordering</strong> <br>- Guarantees <strong>strict order of messages</strong> within a single partition.</li><li><strong>Durability</strong> <br>- Messages are <strong>persisted to disk</strong>, and retention is configurable — Kafka is often used as a <strong>persistent log</strong>.</li><li><strong>High Throughput</strong> <br>- Optimized for high-volume, low-latency workloads with support for <strong>batching</strong> and <strong>compression</strong>.</li></ul><h4>Use Cases</h4><ul><li><strong>Real-Time Log and Event Processing</strong> <br>- <em>Ingest, process, and analyze logs, metrics, and event streams as they happen.</em></li><li><strong>Event Sourcing &amp; State Changes</strong> <br>- <em>Systems like </em><strong><em>Uber</em></strong><em>, </em><strong><em>LinkedIn</em></strong><em>, and </em><strong><em>Netflix</em></strong><em> use Kafka to track state transitions (e.g., ride updates, order lifecycle).</em></li><li><strong>Stream Processing Pipelines</strong> <br>- <em>Paired with tools like </em><strong><em>Kafka Streams</em></strong><em>, </em><strong><em>ksqlDB</em></strong><em>, or </em><strong><em>Apache Flink</em></strong><em> for real-time analytics.</em></li><li><strong>Data Ingestion for Data Lakes/Warehouses</strong> <br>- <em>Acts as a buffer between OLTP systems and big data platforms like Hadoop, Redshift, or Snowflake.</em></li><li><strong>Decoupled Microservices Communication</strong> <br>- <em>Enables asynchronous messaging patterns for loosely coupled services.</em></li></ul><h4>Architecture Summary</h4><ul><li><strong>Producers</strong> → write to topics</li><li><strong>Consumers</strong> → read from topics (can be grouped for parallelism)</li><li><strong>Brokers</strong> → store and serve messages</li><li><strong>Zookeeper (or KRaft)</strong> → used for metadata management and controller election (moving to KRaft to remove Zookeeper dependency)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*ri9ZI4v6wKoeQOK3NxvSDw.png" /></figure><h3>7. NGINX / HAProxy (Load Balancers)</h3><p>Both <strong>NGINX</strong> and <strong>HAProxy</strong> are widely used open-source load balancers that help distribute client traffic across multiple backend servers to ensure high availability, scalability, and reliability.</p><h3>NGINX</h3><p>NGINX is a <strong>high-performance, event-driven load balancer and web server</strong>, known for its ability to handle a massive number of concurrent connections efficiently.</p><h4>Key Features</h4><ul><li><strong>Layer 7 (HTTP) Load Balancing<br>- </strong>Supports intelligent routing based on <strong>URLs</strong>, <strong>headers</strong>, and <strong>cookies</strong>.<br>- Handles <strong>SSL/TLS termination</strong>.</li><li><strong>Reverse Proxy Capabilities<br>- </strong>Enables <strong>caching</strong>, <strong>compression</strong>, and <strong>connection reuse</strong>.<br>- Improves performance for static and dynamic content.</li><li><strong>High Concurrency<br>- </strong>Can handle <strong>thousands to tens of thousands of simultaneous connections</strong> from clients on a single instance.</li></ul><h4>Use Cases</h4><ul><li><em>Load balancing for </em><strong><em>web servers</em></strong><em> and </em><strong><em>RESTful APIs</em></strong><em>.</em></li><li><em>Front-end proxy for </em><strong><em>microservices</em></strong><em>.</em></li><li><em>SSL termination and static content delivery.</em></li></ul><h3>HAProxy</h3><p>HAProxy (High Availability Proxy) is a robust and highly configurable load balancer often used for <strong>TCP</strong> and <strong>HTTP</strong> traffic.</p><h4>Key Features</h4><ul><li><strong>Layer 4 (TCP) and Layer 7 (HTTP) Load Balancing<br>- </strong>Supports routing both <strong>non-HTTP</strong> and <strong>HTTP-based</strong> traffic.<br>- Works well for <strong>databases</strong> and <strong>binary protocols</strong>.</li><li><strong>Advanced Health Checks<br>- </strong>Actively monitors backend node health.<br>- Automatically <strong>removes failed nodes</strong> from rotation.</li><li><strong>Fine-Grained Control<br>- </strong>Allows <strong>rate limiting</strong>, <strong>sticky sessions</strong>, and <strong>connection queuing</strong>.</li></ul><h4>Use Cases</h4><ul><li><em>Load balancing </em><strong><em>databases</em></strong><em> and other non-HTTP services.</em></li><li><em>High-availability setups with failover handling.</em></li><li><em>Infrastructures requiring </em><strong><em>low latency</em></strong><em> and </em><strong><em>detailed traffic management</em></strong><em>.</em></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*ri9ZI4v6wKoeQOK3NxvSDw.png" /></figure><h3>8. Solr / Elasticsearch (Search Engines)</h3><p><strong>Solr</strong> and <strong>Elasticsearch</strong> are open-source, distributed search platforms built on top of <strong>Apache Lucene</strong>. Both support <strong>full-text search</strong>, are <strong>highly available</strong>, <strong>scalable</strong>, and <strong>fault-tolerant</strong>, and expose <strong>powerful indexing and querying capabilities</strong>.</p><h3>Elasticsearch</h3><p>A modern, distributed search engine designed for ease of use and real-time search.</p><h4>Key Features</h4><ul><li><strong>Distributed by Design<br>- </strong>Horizontal scalability via sharding and replication.</li><li><strong>JSON-Based REST API<br>- </strong>Easily integrates with web applications and microservices.</li><li><strong>Full-Text Search<br>- </strong>Advanced features: <strong>analyzers</strong>, <strong>tokenizers</strong>, stemming, etc.</li><li><strong>Schema-Free<br>- </strong>Dynamic mappings allow flexible data ingestion.</li><li><strong>Real-Time Analytics<br>- </strong>Powers the <strong>ELK Stack</strong> (Elasticsearch, Logstash, Kibana) for log and metrics analytics.</li></ul><h4>Use Cases</h4><ul><li><strong><em>Log and event data analytics</em></strong></li><li><strong><em>Search across documents, websites, or apps</em></strong></li><li><strong><em>Monitoring systems</em></strong><em> (e.g., ELK for observability)</em></li></ul><h3>Solr</h3><p>A mature, battle-tested search platform favored for more static or batch-oriented use cases.</p><h4>Key Features</h4><ul><li><strong>Rich Query Capabilities<br>- </strong>Complex faceting, filtering, and custom ranking out of the box.</li><li><strong>Advanced Configuration<br>- </strong>XML-based, with fine-grained tuning options.</li><li><strong>Strong Ecosystem Support<br>- </strong>Enterprise-level features like <strong>SolrCloud</strong>, <strong>ZooKeeper integration</strong>, and <strong>data import handlers</strong>.</li><li><strong>Better for Static Datasets<br>- </strong>Optimized for batch indexing and search-heavy scenarios.</li></ul><h4>Use Cases</h4><ul><li><em>E-commerce product search</em></li><li><em>Enterprise document search</em></li><li><em>Library and catalog systems</em></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*ri9ZI4v6wKoeQOK3NxvSDw.png" /></figure><h3>9. Blobstore (Object Storage)</h3><p><strong>Blobstore</strong> refers to a storage service optimized for <strong>storing large binary objects</strong> such as images, videos, documents, or backups. It is a core building block in modern cloud infrastructure.</p><h4>Key Concepts</h4><ul><li><strong>BLOB</strong>: Stands for <em>Binary Large Object</em> — any form of unstructured data like media files, logs, archives, etc.</li><li><strong>Object Storage Model</strong>: Data is stored as objects, each consisting of:<br>- <strong>Data</strong> (the file itself),<br>- <strong>Metadata</strong> (descriptive info about the file),<br>- <strong>Unique Identifier</strong> (used to retrieve the object).</li></ul><h4>Popular Blobstore Services</h4><ul><li><strong>Amazon S3</strong> (AWS)</li><li><strong>Google Cloud Storage (GCS)</strong></li><li><strong>Azure Blob Storage</strong></li><li><strong>MinIO</strong> (self-hosted, S3-compatible)</li><li><strong>DigitalOcean Spaces</strong></li></ul><h4>Benefits</h4><ul><li><strong>Scalable</strong>: Designed to handle petabytes of data.</li><li><strong>Durable</strong>: High durability through replication (e.g., S3 provides 99.999999999% durability).</li><li><strong>Accessible via HTTP/HTTPS</strong>: Easy to integrate with applications.</li><li><strong>Cost-effective</strong>: Tiered storage options for hot, cold, and archive data.</li></ul><h4>Use Cases</h4><ul><li><em>Storing </em><strong><em>user-uploaded media</em></strong><em> (images, videos, documents).</em></li><li><strong><em>Backup and archive</em></strong><em> solutions.</em></li><li><em>Static asset hosting (e.g., images, CSS/JS files).</em></li><li><em>Input/output storage for </em><strong><em>big data pipelines</em></strong><em>.</em></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*ri9ZI4v6wKoeQOK3NxvSDw.png" /></figure><h3>10. Docker / Kubernetes / Mesos (Containerization)</h3><p>These are essential tools in the modern cloud-native and containerized application ecosystem. They help build, package, deploy, and manage distributed systems efficiently.</p><h3>Docker</h3><p><strong>Docker</strong> is a platform for developing, shipping, and running applications inside lightweight, isolated environments called <strong>containers</strong>.</p><h4>Key Features</h4><ul><li><strong>Containerization</strong>: Packages an application and its dependencies into a single container.</li><li><strong>Portability</strong>: Containers can run on laptops, servers, data centers, or any cloud provider.</li><li><strong>Fast and Lightweight</strong>: Faster startup and lower resource usage than virtual machines.</li></ul><h4>Use Cases</h4><ul><li><em>Developing and running </em><strong><em>microservices</em></strong><em>.</em></li><li><strong><em>CI/CD pipelines</em></strong><em>.</em></li><li><em>Consistent environments across </em><strong><em>development</em></strong><em>, </em><strong><em>test</em></strong><em>, and </em><strong><em>production</em></strong><em>.</em></li></ul><h3>Kubernetes (K8s)</h3><p><strong>Kubernetes</strong> is an open-source platform that automates <strong>deployment</strong>, <strong>scaling</strong>, and <strong>management</strong> of containerized applications.</p><h4>Key Features</h4><ul><li><strong>Container Orchestration</strong>: Schedules and manages containers across a cluster of machines.</li><li><strong>Self-Healing</strong>: Restarts failed containers and reschedules them as needed.</li><li><strong>Auto-Scaling</strong>: Adjusts resource usage based on traffic.</li><li><strong>Service Discovery &amp; Load Balancing</strong>: Handles internal communication between services.</li></ul><h4>Use Cases</h4><ul><li><em>Managing large-scale, </em><strong><em>cloud-native applications</em></strong><em>.</em></li><li><em>Running </em><strong><em>stateless and stateful workloads</em></strong><em>.</em></li><li><em>Automating </em><strong><em>rolling updates</em></strong><em> and </em><strong><em>zero-downtime deployments</em></strong><em>.</em></li></ul><h3>Mesos</h3><p><strong>Apache Mesos</strong> is a distributed systems kernel for managing <strong>cluster resources</strong>. It can run containerized and non-containerized workloads side-by-side.</p><h4>Key Features</h4><ul><li><strong>Fine-grained resource allocation</strong> across diverse frameworks.</li><li>Supports running both <strong>Docker containers</strong> and <strong>big data frameworks</strong> like Apache Spark and Hadoop.</li><li>Works well in <strong>heterogeneous environments</strong>.</li></ul><h4>Use Cases</h4><ul><li><em>Managing </em><strong><em>mixed workloads</em></strong><em> (e.g., Spark jobs + containerized apps).</em></li><li><em>Large enterprise clusters with custom orchestration needs.</em></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*ri9ZI4v6wKoeQOK3NxvSDw.png" /></figure><h3>11. Hadoop / Spark / HDFS (Big Data)</h3><p>These technologies form the backbone of many modern big data ecosystems, enabling storage and large-scale distributed processing.</p><h3>Hadoop</h3><p>Hadoop is a big data framework that allows for <strong>distributed batch processing</strong> of large datasets across clusters of computers.</p><h4>Key Components</h4><ul><li><strong>MapReduce</strong>: Programming model for processing large datasets using map and reduce functions. Disk-based and designed for batch jobs.</li><li><strong>HDFS (Hadoop Distributed File System)</strong>: A fault-tolerant, distributed file system that stores data across multiple nodes. Designed for high throughput rather than low latency.</li></ul><h4>Use Cases</h4><ul><li><em>Batch analytics on massive datasets.</em></li><li><em>Log aggregation and processing.</em></li><li><em>ETL (Extract, Transform, Load) jobs.</em></li></ul><h3>Spark</h3><p>Apache Spark is a <strong>fast, in-memory</strong> data processing engine that builds upon Hadoop’s ideas but offers significant speedups.</p><h4>Key Features</h4><ul><li><strong>In-Memory Computation</strong>: Keeps intermediate data in memory, avoiding disk I/O bottlenecks.</li><li><strong>Rich APIs</strong>: Support for Java, Scala, Python, and R.</li><li><strong>Modules</strong>: Includes Spark SQL, Spark Streaming, MLlib (machine learning), and GraphX.</li></ul><h4>Use Cases</h4><ul><li><em>Real-time data processing and streaming analytics.</em></li><li><em>Machine learning and data science pipelines.</em></li><li><em>Faster alternatives to MapReduce for large-scale batch jobs.</em></li></ul><h3>HDFS</h3><p>HDFS (Hadoop Distributed File System) is the storage layer used by Hadoop and compatible with Spark.</p><h4>Key Features</h4><ul><li><strong>Distributed</strong>: Data is split into blocks and replicated across multiple nodes.</li><li><strong>Fault Tolerance</strong>: Replication provides resilience against hardware failures.</li><li><strong>Write-Once, Read-Many</strong>: Optimized for high-throughput access to large files.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*ri9ZI4v6wKoeQOK3NxvSDw.png" /></figure><h3><strong>Conclusion</strong></h3><p>System design is a <strong>vast and evolving field </strong>— but at its core, it’s about understanding trade-offs, making informed choices, and designing with scale, reliability, and performance in mind. This article has covered <strong>foundational principles</strong>,<strong> essential concepts</strong>, and <strong>widely-used tools</strong> that every engineer should be familiar with when approaching real-world system design challenges.</p><p>Whether you’re preparing for interviews, designing large-scale systems, or simply strengthening your architectural thinking, these fundamentals serve as your blueprint. <strong><em>Mastery begins with clarity, and clarity begins with fundamentals.</em></strong></p><p>Thanks for reading <em>System Design Fundamentals</em>. I hope this guide helped simplify complex topics and gave you a solid base to build upon. If you found it useful, share it with others, bookmark it for future reference, and stay tuned — more deep dives and <strong>real-world case studies are coming soon.</strong></p><p>Until then, keep designing, keep learning. 🛫</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d8ebae6e562e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Introduction to System Design Interview — PART 01]]></title>
            <link>https://medium.com/@avinashyadav16/introduction-to-system-design-interview-part-01-eee746501198?source=rss-570103f5c627------2</link>
            <guid isPermaLink="false">https://medium.com/p/eee746501198</guid>
            <category><![CDATA[system-design-concepts]]></category>
            <category><![CDATA[system-design-interview]]></category>
            <category><![CDATA[design-systems]]></category>
            <dc:creator><![CDATA[Avinash Yadav]]></dc:creator>
            <pubDate>Thu, 19 Jun 2025 22:07:17 GMT</pubDate>
            <atom:updated>2025-06-19T22:07:17.928Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction to System Design Interview — PART 01</h3><p>One of those articles that throws light at system design concepts in a comprehensive manner.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JXJugb5s937R_emID2Xytg.jpeg" /></figure><h3>Introduction</h3><p>System design interviews are very <strong>subjective topic — </strong>there is <em>no single correct answer</em>; solutions vary based on experience and context.</p><p>The goal is to assess <strong>problem-solving skills, scalability understanding, and trade-off analysis</strong>.</p><p>This article covers:</p><ul><li><strong><em>ABCDs of system design interviews</em></strong><em> (framework for approaching problems).</em></li><li><strong><em>Core aspects</em></strong><em> to consider in system design.</em></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*ri9ZI4v6wKoeQOK3NxvSDw.png" /></figure><h3>ABCDs of System Design Interviews</h3><h3>A. Ask Good Questions</h3><p>It is the candidate’s responsibility to A<strong>sk Good Question</strong> and <strong>Define the Minimum Viable Product (MVP)</strong> for the system design problem with the interviewer.</p><p>A problem could have many features and it is our responsibility to work with the interviewer to figure out which features he/she cares about the most. (Since we are working in a very limited timeframe so it is somehow good to make sure our feature set is small and we are going deeper into it.</p><p><strong>Example:</strong> If asked to design <strong>Facebook Messenger</strong>, prioritize:</p><ul><li><strong>Core features</strong>: <em>1:1 chat, read receipts.</em></li><li><strong>Exclude initially</strong>: <em>Group chat, video calls.</em></li></ul><p>The second thing to focus on is to ask for ‘<strong>How much to scale?</strong>’ and <strong>Clarify scalability requirements</strong> properly. For example:</p><ul><li>How much <strong>data storage</strong> is needed? (<em>e.g., 1TB vs. 1PB</em>).</li><li>What’s the expected <strong>requests per second (RPS)</strong>? (<em>e.g., 1K vs. 1M RPS</em>).</li><li>What’s the acceptable <strong>latency</strong>? (<em>e.g., &lt;100ms for real-time chat</em>).</li></ul><h3>B. Avoid Buzzwords</h3><p><strong>Do not</strong> mention very fancy and good-looking terms (e.g., “consistent hashing,” “Kafka”, etc.) unless you deeply understand them. Only mention technologies/concepts you understand deeply.</p><p>Superficial use of buzzwords might work sometimes but it can also <strong>backfire</strong> if you can’t explain how they work under the hood.</p><p>Make sure whatever technologies or the concepts you mention during the interview you have some sort of in-depth knowledge about it.</p><h3>C. Clear &amp; Organized Thinking</h3><p>Before jumping into the minor details of the problem, first try to define the 50,000 feet view of the problem.</p><p>Make sure you have defined all the APIs, draw the right boxes, understand the actors of the system.</p><p>Once all these done, then start diving deeper into the problem statement with the interviewer.</p><p><strong>STEPS:</strong></p><ol><li><strong>Start with a high-level diagram</strong> (e.g., draw boxes for components, define APIs, databases).</li><li><strong>Define actors</strong> (users, services).</li><li><strong>Then dive deeper</strong> into components (e.g., caching, load balancing).</li><li>Drill into details only after the broad structure is agreed upon.</li></ol><h3>D. Drive the Discussion (80/20 Rule)</h3><p>Follow the <strong>80/20 rule</strong>: You speak 80% of the <strong>conversation</strong> time; let the interviewer interject 20% of the discussion.</p><p><strong>Anticipate problems</strong>: Make sure you leave the discussion, anticipate and address potential flaws in your design proactively in pre-emptive way.</p><ul><li>“<em>If we use a cache, what happens on a cache miss?</em>”</li><li>“<em>How do we handle a database failure?</em>”</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*ri9ZI4v6wKoeQOK3NxvSDw.png" /></figure><p><strong>Obviously, The ABCDs of the system design problems are much easier said than done. But we can improve them in 3 aspects:</strong></p><ol><li><strong>Personal Experience</strong>: <em>If we are working on high scale systems, then it is much easier to improve on those stuffs.</em> <em>P.S.: Work on scalable systems (if possible).</em></li><li><strong>Practice</strong>: <em>Think about a system design problem, work with friends or peers and brainstorm the ideas and figure out what tech we can use to solve the problem.</em> <em>Mock interviews with peers.</em></li><li><strong>Study</strong>: <em>The last and important aspect is by gaining knowledge through reading blogs, posts and going through videos.</em></li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*ri9ZI4v6wKoeQOK3NxvSDw.png" /></figure><h3>Core Aspects of System Design Problems</h3><blockquote>Basic Features Required in System Design</blockquote><h3>1. Features (MVP Definition)</h3><p>Collaborate with the interviewer to <strong>scope the problem / define MVP</strong> (e.g., exclude low-priority features) by asking good questions.</p><p>Example:</p><ul><li><strong>Included</strong>: <em>User authentication, posting tweets.</em></li><li><strong>Excluded</strong>: <em>Analytics, trending topics.</em></li></ul><h3>2. API Design</h3><p>Now, we are set on the features. Next, we need to figure out designing APIs that align with the features (e.g., `<em>sendMessage</em>`, `<em>readReceipt</em>`).</p><p><em>What are required APIs, who is going to call those APIs, how they are going to call those APIs, etc.</em></p><p>Define <strong>endpoints, request/response formats</strong>.</p><p>Example for Twitter:</p><ul><li>POST /tweet – Create a tweet.</li><li>GET /feed – Retrieve user feed.</li></ul><h3>3. Availability</h3><p>Now we have come up with the service, next we need to how available the service is.</p><p>Ensuring the services remains operational during failures (e.g., if a single host went down, will the service still be going to available? vs. heck the entire data center went down, will the services be still available?).</p><p>Anyhow, this we have to figure out with the interviewer that how much availability he/she cares about in the system.</p><p><strong>Measure in “nines”</strong>:</p><ul><li>99.9% available = ~8.76 hours downtime/year.</li><li>99.99% = ~52 minutes/year.</li></ul><p><strong>Redundancy</strong>:</p><ul><li>Multi-region deployment.</li><li>Failover mechanisms.</li></ul><h3>4. Latency &amp; Performance</h3><p>So, if the job is the background task, then we don’t care much about the latency (Background jobs can tolerate higher latency) but on the other hand if it is customer facing request then obviously, we want our system to be superfast.</p><p>So, do optimization for customer-facing requests. Make sure c<strong>ustomer-facing apps</strong> have <strong>low latency</strong> (&lt;200ms).</p><p><strong>Optimizations</strong>:</p><ul><li>Caching (Redis, Memcached).</li><li>CDNs for static content.</li></ul><h3>5. Scalability</h3><p>So, if we design a system that works for 100 users, but the question is: Will it work for 1000 users or is it going to work for a million users?</p><p>That’s <strong>scalability.</strong></p><p><em>Is the system going to scale as we add more and more users or more requests?</em></p><p><em>Is it continuing to have the good latency performance?</em></p><p><em>Is it continuing to available as we get more users or requests for a service?</em></p><p>Therefore, consider these points while designing solution for the system.</p><p><strong>Design for growth</strong> (e.g., horizontal scaling, sharding).</p><p><strong>Vertical vs. Horizontal Scaling</strong>:</p><ul><li>Vertical: Upgrade server (CPU, RAM).</li><li>Horizontal: Add more servers (preferred for distributed systems).</li></ul><p><strong>Database Scaling</strong>:</p><ul><li><strong>Read replicas</strong> for read-heavy workloads.</li><li><strong>Sharding</strong> for write-heavy workloads.</li></ul><h3>6. Durability</h3><p>This might be important for some interviews and might not for some other.</p><p>So, durability is the fact that data can be stored in a database securely, it is not lost and it is not compromised.</p><blockquote><strong>Data must not be lost</strong>.</blockquote><p>Techniques:</p><ul><li>Replication (e.g., 3 copies of data).</li><li>Backups (snapshots, incremental backups).</li></ul><p>Sometimes it is ok to go with the database that you have used and you know that the mentioned database will do all the required job for you.</p><p>But on the other hand, in case you are designing the database then that’s the place where durability plays a central role and we need to make sure that our system is durable enough.</p><h3>7. Class Diagrams (OOP Focused Problems)</h3><p>For OOP-focused problems (e.g., parking lot, elevator systems) where interviewer might be focused on knowing how would you design the class and what are the some of the OOP&#39;s principles that you utilized.</p><p>Example: Design an <strong>Elevator System</strong>.</p><ul><li>Classes: Elevator, Floor, Button.</li><li>Methods: moveUp(), openDoor().</li></ul><h3>8. Security &amp; Privacy</h3><p>Most of the interviews, we probably not care much about the security and privacy but it becomes important for some scenes. For example:</p><p><strong>Critical for</strong>: Authentication system, payment systems.</p><p>Techniques:</p><ul><li>Encryption (TLS, AES).</li><li>Rate limiting (prevent brute force attacks).</li></ul><h3>9. Cost-Effectiveness</h3><p>So whatever solution we suggested, is it cost effective? is there an alternate solution which would be more cost effective.</p><p>So, discuss some pros and cons of different solutions as far as cost is concerned.</p><p>Compare solutions:</p><ul><li><strong>NoSQL vs. SQL</strong> (cost vs. consistency).</li><li><strong>On-premise vs. Cloud</strong> (CAPEX vs. OPEX).</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*ri9ZI4v6wKoeQOK3NxvSDw.png" /></figure><p>Now that we’ve covered some of the key fundamentals to keep in mind during an interview, it’s time to shift our focus to <strong>the essential topics and concepts you should be familiar with before stepping into a system design interview</strong>.</p><p>Understanding these core areas not only helps you approach problems more confidently but also enables you to communicate your ideas effectively — a crucial skill in any design discussion.</p><p>If you have any thoughts, suggestions, or feedback, feel free to share them — I’d love to hear from you!</p><p><strong>Stay tuned for the next article</strong>, where we’ll dive into the foundational concepts every candidate should know before tackling system design interviews.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=eee746501198" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Namaste JavaScript — PART 01]]></title>
            <link>https://medium.com/@avinashyadav16/namaste-javascript-part-01-930ac27a1213?source=rss-570103f5c627------2</link>
            <guid isPermaLink="false">https://medium.com/p/930ac27a1213</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[akshay-saini]]></category>
            <category><![CDATA[js]]></category>
            <dc:creator><![CDATA[Avinash Yadav]]></dc:creator>
            <pubDate>Thu, 12 Jun 2025 04:35:18 GMT</pubDate>
            <atom:updated>2025-06-25T16:55:58.291Z</atom:updated>
            <content:encoded><![CDATA[<p><em>PART 01: Namaste JavaScript will dive deep into JavaScript and the internals of how code is executed under the hood. By the end, we will exactly know how things are working and won’t run into unexpected errors.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pvAEzG7x7hdTUXPbrm4kzw.jpeg" /></figure><p>TABLE OF CONTENT:</p><p><a href="#01d4"><strong>CHAPTER 01: Execution Context</strong></a></p><p><a href="#ded8"><strong>CHAPTER 02: Execution &amp; Call Stack</strong></a></p><p><a href="#801f"><strong>CHAPTER 03: Hoisting</strong></a></p><p><a href="#8a8c"><strong>CHAPTER 04: Functions and Variable Environments</strong></a></p><p><a href="#4065"><strong>CHAPTER 05: Window and this keyword</strong></a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9gYsyBVE4Zjobeqm_T0asA.png" /></figure><h3>CHAPTER 01: Execution Context</h3><p>Everything in JS happens inside the <strong>execution context.</strong></p><p>Assume execution context to be a big box/container where everything takes place (whole JavaScript code is executed).</p><p>It has 2 components in it:</p><ol><li><strong>Memory:</strong> The place where all the variables and functions are stored as (key: value) pairs. Memory component is also known as <em>variable environment</em>.</li><li><strong>Code:</strong> The place where code is executed one line at a time. Code component is also known as <em>Thread of Execution.</em></li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/957/1*t1Q3IZE-pdSx5R3bIzA4IQ.png" /></figure><blockquote><strong>NOTE: </strong><em>JavaScript is not possible without this beautiful execution context.</em></blockquote><h3>JS is a synchronous single-threaded language.</h3><ul><li>By single threaded, we mean JS can only run 1 command at a time</li><li>By synchronous single threaded, we mean it can run 1 command at a time, in a specific order.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9gYsyBVE4Zjobeqm_T0asA.png" /></figure><h3>CHAPTER 02: Execution &amp; Call Stack</h3><p><strong><em>What happens when we run a js program?</em></strong></p><p>Every time we run a program, an execution context is created.</p><pre>var n = 2;<br><br>function square(num) {<br>    var ans = num * num;<br>    return ans;<br>}<br><br>var square2 = square(n);<br>var square4 = square(4);</pre><p>Now first, for this entire code a <strong>Global execution</strong> context is created.</p><p>So, this execution context is created in 2 phases:</p><ol><li><strong><em>CREATION PHASE / MEMORY CREATION PHASE</em></strong></li><li><strong><em>CODE EXECUTION PHASE</em></strong></li></ol><h4>In the first phase (memory creation)</h4><p>Here JS will allocate memory to all the variables and functions whenever they are encountered while running whole js program line by line. In case of variables, it stores a special value called <em>undefined</em>, and in case of functions it literally stores the whole code of the function inside this memory space.</p><ul><li>Memory is allocated to variables and functions.</li><li>For variable name (which is key) it assigns a value of <strong>undefined</strong>.</li><li>For the function name (which is key) it assigns the entire function code as value.</li></ul><pre>n: undefined<br>square: {…entire-code…}<br>square2: undefined<br>square4: undefined</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/699/1*YekmSdolOuIaTruyopAoBg.png" /></figure><h4>In the second phase (code execution)</h4><ul><li>The JavaScript, once again runs through the whole js program line by line and executes the code now. This is the point when all these functions and every calculation in the program is done.</li><li>The variable name is replaced with its actual assigned value from code. So now <strong>n:2</strong></li><li>Skips over function code as there is nothing to assign there. Functions are like a mini program in the JavaScript.</li><li>We encounter a function call in <strong>square2</strong>. So, whenever a new function is invoked, a mini program is invoked. So altogether a brand-new local EC is created inside the code part of global EC and this will have the same 2 components: Memory and Code and again 2 phases will be involved.</li><li>In the local EC, <strong>ans</strong> and <strong>num </strong>are both <strong>undefined</strong> (in first phase). Then, the <strong>n</strong> value in global EC is passed to <strong>num</strong>, replacing <strong>undefined</strong>. <strong>num</strong> is the parameter and <strong>n</strong> is the argument.</li><li><strong>ans = num*num</strong> (calculated in code part of local EC and returned) replaces <strong>undefined</strong> in local EC (memory part) and the final value is returned from local and is assigned to <strong>square2 </strong>var in global.</li><li>After returning, local EC is removed from global EC and control goes back to global.</li><li>One more function call is met. Same thing happens again here.</li><li>Once <strong>square4</strong> value is replaced from <strong>undefined</strong> to <strong>16</strong>, global EC will also be deleted.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1014/1*pgUfz8xmwGThIZGukvlBEQ.png" /></figure><p>To manage all these EC, a call stack is created. Every time code is run, the EC is pushed in. So first global EC is pushed. Then e1 EC (for<em> square2</em>) is pushed, and then after value returned, is popped. Similarly, e2 EC (for <em>square4</em>) is pushed, and then popped and finally Global is also popped and stack is empty.</p><blockquote>Call Stack maintains the order of execution of execution contexts</blockquote><p><strong><em>Call stack also known as Execution control stack, program stack, control stack, runtime stack and machine stack.</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9gYsyBVE4Zjobeqm_T0asA.png" /></figure><h3>CHAPTER 03: Hoisting</h3><h4>Code Example 01</h4><pre>var x = 7;<br><br>function getName() {<br>    console.log(&quot;Namaste JavaScript&quot;);<br>}<br><br>getName();<br><br>console.log(x);</pre><p><strong><em>Output:</em></strong></p><blockquote>Namaste JavaScript</blockquote><blockquote>7</blockquote><h4>Code Example 02</h4><pre>getName(); <br><br>console.log(x);<br><br>// in most languages, <br>// both the lines which are above their declaration will give error.<br>// But not in JS though.<br><br><br>var x = 7;<br><br>function getName() {<br>    console.log(&quot;Namaste JavaScript&quot;);<br>}</pre><p><strong><em>Output:</em></strong></p><blockquote>Namaste JavaScript</blockquote><blockquote>undefined</blockquote><h4>Code Example 03</h4><pre>getName();<br><br>console.log(x);<br><br>function getName() {<br>    console.log(&quot;Namaste JavaScript&quot;);<br>}</pre><p><strong><em>Output:</em></strong></p><blockquote>Namaste JavaScript</blockquote><blockquote>Error: x is not defined // note that not defined here and “undefined” in sample 2 are totally different.</blockquote><ul><li><strong>Not defined:</strong> We have not initialised the value for variable anywhere in the entire code and in memory space.</li><li><strong>Undefined:</strong> It is a placeholder that is assigned to a variable by the JavaScript Engine until the variable is assigned with some other value.</li></ul><p><strong>Hoisting is a concept which enables us to extract values of variables and functions even before initialising/assigning value without getting <em>error.</em></strong></p><h4>Code Example 04</h4><pre>function getName() {<br>    console.log(&quot;Namaste JavaScript&quot;);<br>}<br><br>console.log(getName);</pre><p><strong><em>Output:</em></strong></p><blockquote>f getName() {</blockquote><blockquote>console.log (“Namaste JavaScript);</blockquote><blockquote>}</blockquote><h4>Code Example 05</h4><pre>getName();<br><br>console.log(x);<br><br>console.log(getName);<br><br>var x = 7;<br><br>function getName() {<br>    console.log(&quot;Namaste JavaScript&quot;);<br>}</pre><p><strong><em>Output:</em></strong></p><blockquote>Namaste JavaScript</blockquote><blockquote>undefined</blockquote><blockquote>f getName(){</blockquote><blockquote>console.log(“Namaste JavaScript);</blockquote><blockquote>}</blockquote><h4>Code Example 06</h4><pre>console.log(getName);<br><br><br>var getName = function () {<br>    console.log(&quot;Namaste JavaScript&quot;);<br>};<br><br><br>var getName = () =&gt; {<br>    // use fat arrow function<br>    console.log(&quot;Namaste JavaScript&quot;);<br>};</pre><p><strong><em>Output:</em></strong></p><blockquote>undefined //it is because arrow function behaves as variable and not as a function.</blockquote><h4>REASON OF WEIRDNESS</h4><ul><li>The answer lies in the Global Execution Context.</li><li>In the memory phase, the variables will be initialized as <em>undefined</em> and functions will get the whole function code in their memory.</li><li>This is the reason why we are getting these outputs.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9gYsyBVE4Zjobeqm_T0asA.png" /></figure><h3>CHAPTER 04: Functions and Variable Environments</h3><pre>var x = 1;<br><br>a();<br><br>b(); <br>// we are calling the functions before defining them.<br>// This will work properly, as seen in Hoisting.<br><br><br><br>console.log(x);<br><br><br>function a() {<br>    var x = 10;<br>    console.log(x);<br>}<br><br><br>function b() {<br>    var x = 100;<br>    console.log(x);<br>}</pre><p><strong><em>Outputs:</em></strong></p><blockquote>10</blockquote><blockquote>100</blockquote><blockquote>1</blockquote><h4>Code Flow</h4><ul><li>The Global Execution Context (GEC) is created (the big box with Memory and Code subparts). Also, GEC is pushed into Call Stack</li></ul><blockquote>Call Stack: GEC</blockquote><ul><li>In first phase of GEC (memory phase), variable <strong><em>x: undefined</em></strong> and <strong><em>a</em></strong> and <strong><em>b </em></strong>have their entire function code as value initialized.</li><li>In second phase of GEC (execution phase), when the function is called, a new local EC is made. After <strong><em>x = 1</em></strong> assigned to GEC <strong><em>x</em></strong>, <strong><em>a() </em></strong>is called. So local EC for a is made inside code part of GEC.</li></ul><blockquote>Call Stack: [GEC, a()]</blockquote><ul><li>For local EC, a totally different <strong><em>x</em></strong> variable assigned <strong><em>undefined</em></strong> (<strong><em>x</em></strong> inside <strong><em>a()</em></strong>) in phase 1, and in phase 2 it is assigned <strong><em>10 </em></strong>and printed in console log. After printing, no more commands to run, so <strong><em>a()</em></strong> local EC is removed from both GEC and from Call stack</li></ul><blockquote>Call Stack: GEC</blockquote><ul><li>Cursor goes back to <strong><em>b()</em></strong> function call. Same steps repeat.</li></ul><blockquote>Call Stack: [GEC, b()] -&gt; GEC (after printing yet another totally different x value as 100 in console log)</blockquote><ul><li>Finally, GEC is deleted and also removed from call stack. Program ends.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9gYsyBVE4Zjobeqm_T0asA.png" /></figure><h3>CHAPTER 05: Window and this keyword</h3><blockquote>Everywhere JS is run, it is done with a JS execution engine. For Chrome: v8</blockquote><ul><li>The shortest JS program is nothing but an Empty JS file.</li><li>Even for this program, JS engine does a lot of the things behind the scenes.</li><li>It creates the GEC, the “window” and the <em>_this_</em> variable.</li><li>Window is a big global object that has a lot of functions and variables, which is created along with global execution context. These functions and variables are created by the JavaScript Engine and into the global scope. All of these can be accessed from anywhere in the program.</li><li>At the global level, <em>_this_</em> points to <em>_window_</em> object.</li></ul><blockquote><em>this === window -&gt; true (at global level)</em></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/593/1*4RKW_qZjivhg5HFoWNSozw.png" /><figcaption>Brower’s Console with ‘window’ and ‘this’ keyword</figcaption></figure><p><em>Whenever a JavaScript program is run, a </em><strong><em>global object</em></strong><em> is created, a </em><strong><em>global execution context</em></strong><em> is created and along with-it this variable is also created.</em></p><pre>var a = 10; // not inside any fxn. So global object<br><br>function b() {<br>// this fxn not inside any function. So global.<br>var x = 5; // not global<br>}<br><br>console.log(window.a); // gives us &quot;a&quot; value<br><br>console.log(this.a); // this points to window so it returns &quot;a&quot; value<br><br>console.log(a); // also gives same &quot;a&quot; value. (If we don&#39;t put any.<br>                // in front of variable, it **assumes variable is in global space**<br><br>console.log(x); // x is not defined. (Tries to find x inside global space,<br>                 // but it isn&#39;t there)</pre><ul><li>Global space is anything in JS which isn’t inside a function. All these global objects will be present inside the windows schema. But non global ones won’t be there (here, `x`).</li><li>When a GEC is made, <em>_this_</em> is also created with it (even for functional(local) EC). Global object provided by the browser engine is the window, so <em>_this_</em> points to window.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9gYsyBVE4Zjobeqm_T0asA.png" /></figure><h3>CONCLUSION</h3><p>So, here we covered a basic starting into JavaScript and the background handling task of each of the line and they are executed. This gives a beginning of basic glimpse of JS.</p><p>After all JS is ‘beautiful’ language — IFYYK.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=930ac27a1213" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[C++ STL Containers]]></title>
            <link>https://medium.com/@avinashyadav16/c-stl-containers-f8bdb4b64768?source=rss-570103f5c627------2</link>
            <guid isPermaLink="false">https://medium.com/p/f8bdb4b64768</guid>
            <category><![CDATA[cpp]]></category>
            <category><![CDATA[cpp-programming]]></category>
            <category><![CDATA[stl]]></category>
            <dc:creator><![CDATA[Avinash Yadav]]></dc:creator>
            <pubDate>Mon, 02 Jun 2025 05:17:54 GMT</pubDate>
            <atom:updated>2025-06-03T11:30:22.433Z</atom:updated>
            <content:encoded><![CDATA[<p>A quick view of CPP Standard Template Library.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aAvof_QmcITtZ0gHt059SQ.jpeg" /></figure><h3><strong>Containers library</strong></h3><h3><strong>1. SEQUENCE CONTAINERS</strong></h3><p>Sequence containers implement data structures which can be accessed sequentially.</p><ol><li><strong>array</strong>(C++11)<strong>:</strong> <em>Fixed size, static contiguous array</em></li><li><strong>vector:</strong> <em>dynamic contiguous array. Supports fast random access.</em></li><li><strong>deque:</strong> <em>double-ended queue, fast insert/delete at both ends.</em></li><li><strong>forward_list (C++11):</strong> <em>singly-linked list. More memory-efficient than list.</em></li><li><strong>list:</strong> <em>doubly-linked list. Allows fast insertion and deletion from anywhere.</em></li></ol><blockquote><strong>Vector Container:</strong></blockquote><p>Library: <em>#include &lt;vector&gt;</em></p><p>This is the most widely used STL container!</p><pre>// INITIALIZATION:<br><br>vector&lt;int&gt; v; // Empty <br>vector&lt;int&gt; v(size); // Vector with size &#39;size&#39; <br>vector&lt;int&gt; v(size, value); // Vector with size &#39;size&#39; &amp; all elements with value &#39;value&#39; <br><br>-----------------------------------------------------------------------------<br><br>// BASIC INBUILT FUNCTIONALITIES:<br><br>v.size(); // Get size of vector <br>v.resize(size, value); // Fills the additional size of vector with value &#39;value&#39; <br>v.empty(); // returns true if vector is empty <br>v.front(); or v[0]; // Accessing first element <br>v.back(); or v[v.size()-1]; // Accessing last element <br><br>-----------------------------------------------------------------------------<br><br>// INSERT AN ELEMENT OR A RANGE:<br>v.push_back(ele); // Add an element &#39;ele&#39; to the end <br>v.insert(v.begin() + i, ele); // Inserting element &#39;ele&#39; at index i | Use i = 0 for inserting at front <br>v.insert(v.end(), v2.begin(), v2.end()); // Inserting the elements of vector &#39;v2&#39; to the end of vector &#39;v&#39; <br><br>-----------------------------------------------------------------------------<br><br>// ERASE AN ELEMENT OR RANGE<br>v.pop_back(); // Remove element at end <br>v.erase(v.begin() + i); // Remove element at index i <br>v.erase(v.begin() + i, v.begin() + j); // Remove elements from index i to j-1 <br>v.erase(remove(v.begin(), v.end(), value), v.end()); // Removes all elements with value &#39;value&#39; from the vector </pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KxFv9qtNQ95Lb5e0E2_L1w.png" /><figcaption>vector container</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9gYsyBVE4Zjobeqm_T0asA.png" /></figure><h3><strong>2. ASSOCIATIVE CONTAINERS</strong></h3><p>It implements sorted data structures that can be quickly searched (O(log n) complexity).</p><ol><li><strong>set:</strong> <em>collection of </em><strong><em>unique keys</em></strong><em>, sorted by keys</em></li><li><strong>map:</strong> <em>collection of key-value pairs, sorted by keys, </em><strong><em>keys are unique</em></strong></li><li><strong>multiset:</strong> <em>collection of keys, sorted by keys</em></li><li><strong>multimap:</strong> <em>collection of key-value pairs, sorted by keys.</em></li></ol><blockquote><strong>Set container</strong></blockquote><p>Library:<em> #include &lt;set&gt;</em></p><p>Library to get your hands down with, because this library lets us handle trickier algorithms and data structures problems with grace.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*A_-KMI77r2s1Xd9e3gqGJg.png" /><figcaption>set container</figcaption></figure><blockquote><strong>Map container</strong></blockquote><p>Library: <em>#include &lt;map&gt;</em></p><p>Must know STL container and to be well rehearsed as well for getting interview ready if you choose C++ as your language!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*m_K03Ie4k3UxLn2UddvHVw.png" /><figcaption>map container</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9gYsyBVE4Zjobeqm_T0asA.png" /></figure><h3><strong>3. UNORDERED ASSOCIATIVE CONTAINERS</strong></h3><p>Unordered associative containers implement unsorted (hashed) data structures that can be quickly searched (O(1) amortized, O(n) worst-case complexity).</p><ol><li><strong>unordered_set:</strong> <em>collection of unique keys, hashed by keys</em></li><li><strong>unordered_map:</strong> <em>collection of key-value pairs, hashed by keys, keys are unique</em></li><li><strong>unordered_multiset:</strong> <em>collection of keys, hashed by keys</em></li><li><strong>unordered_multimap:</strong> <em>collection of key-value pairs, hashed by keys</em></li></ol><h3><strong>4. CONTAINER ADAPTORS</strong></h3><p>Container adaptors provide a different interface for sequential containers.</p><ol><li><strong>Stack:</strong> <em>LIFO (Last In First Out)</em></li><li><strong>Queue:</strong> <em>FIFO (First In First Out)</em></li><li><strong>priority_queue:</strong> <em>Max or Min heap implementation</em></li></ol><blockquote><strong>Stack container:</strong></blockquote><p>Library: <em>#include &lt;stack&gt;</em></p><p>Another popular container that makes our lives easier as many famous interview questions are based on the usage of stack.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*S-9zJo283umIwquPcW333g.png" /><figcaption>stack container</figcaption></figure><blockquote><strong>Queue container</strong></blockquote><p>Library: <em>#include&lt;queue&gt;</em></p><p>Queue can be greatly used in the problems related to graphs and hence this should not be ignored as well.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UpNxgpJMHMCz1aZTpVcgZQ.jpeg" /><figcaption>queue container</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9gYsyBVE4Zjobeqm_T0asA.png" /></figure><h3>Now, let’s look for the time complexities of containers</h3><ol><li>Priority Queue</li><li>Map: Time Complexities mentioned below are for Ordered Map.</li><li>Set: Time Complexities mentioned below are for Ordered Set.</li><li>Stack</li><li>Queue</li><li>Vector</li><li>List</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IoR7jlJ-XmavOz65iZd9qA.png" /><figcaption>STL</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9gYsyBVE4Zjobeqm_T0asA.png" /></figure><blockquote><strong>String container</strong></blockquote><p>Library: <em>#include &lt;string&gt;</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1pJnngTFaPTnPdw-Zh6xcQ.png" /><figcaption>string container</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*ri9ZI4v6wKoeQOK3NxvSDw.png" /></figure><h3>🧾 Conclusion on C++ STL Containers</h3><p>The <strong>C++ Standard Template Library (STL)</strong> provides a rich collection of pre-built <strong>container classes</strong>, enabling us to store and manipulate data efficiently. Understanding the types, use cases, and complexities of these containers is essential for writing optimized and clean code.</p><h4>Key Takeaways:</h4><ul><li><strong>Sequence Containers</strong> (e.g., vector, list, deque) maintain the order of insertion and are ideal for linear data structures.</li><li><strong>Associative Containers</strong> (e.g., set, map) are sorted and allow fast lookup, insert, and delete operations using trees (typically Red-Black Trees).</li><li><strong>Unordered Associative Containers</strong> (e.g., unordered_map, unordered_set) provide average constant-time complexity using hash tables.</li><li><strong>Container Adaptors</strong> (e.g., stack, queue, priority_queue) offer restricted interfaces tailored for specific use cases like LIFO/FIFO data handling.</li></ul><h4>Efficiency Insight:</h4><ul><li>Choose <strong>vector</strong> for dynamic array use where random access is frequent.</li><li>Use <strong>list</strong> or <strong>deque</strong> if frequent insertions/removals at both ends are needed.</li><li>For fast key-based lookup, use <strong>map</strong> or <strong>unordered_map</strong> depending on whether ordering is needed.</li><li><strong>priority_queue</strong> is ideal for scenarios where you always need to access the largest or smallest element.</li></ul><h4>Best Practices:</h4><ul><li>Always select the container best suited for your data access pattern.</li><li>Understand the <strong>underlying data structure</strong> (e.g., Red-Black Tree vs. Hash Table) to predict performance in edge cases.</li><li>Use <strong>emplace functions</strong> (like emplace_back) for in-place construction and improved performance.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9gYsyBVE4Zjobeqm_T0asA.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f8bdb4b64768" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Bit Manipulation Cheat Sheet]]></title>
            <link>https://medium.com/@avinashyadav16/bit-manipulation-cheat-sheet-166f6c9dfc16?source=rss-570103f5c627------2</link>
            <guid isPermaLink="false">https://medium.com/p/166f6c9dfc16</guid>
            <category><![CDATA[bit]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[bit-manipulation]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[computer-science]]></category>
            <dc:creator><![CDATA[Avinash Yadav]]></dc:creator>
            <pubDate>Sat, 31 May 2025 04:21:26 GMT</pubDate>
            <atom:updated>2025-05-31T04:21:26.469Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>A quick introduction to the fundamentals of manipulating binary data at the bit level. Understanding the key operators (&amp;, |, ^, ~, &lt;&lt;, &gt;&gt;) and how they function on binary numbers. This is a cheat sheet for quick review of bit manipulation.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dVC4kAqc9D1lGtnZxBmMyA.jpeg" /></figure><p><strong>Bitwise</strong> <em>refers to operations or manipulations that are carried out directly on individual bits of a number, usually in binary representations. </em><strong>Bitwise operations</strong> <em>are used to work with data at the level of its binary components i.e. to the level of bits (0s and 1s). Bitwise operations let us interact directly with the binary digits that represent each integer.</em></p><h3>Table Of Contents:</h3><p><a href="#224a">Why Do We Use Bitwise Operations?</a><br><a href="#89b1">Bitwise Operators</a><br><a href="#57b5">Arithmetic Operations</a><br><a href="#7a29">Bitwise Operations</a><br><a href="#da0b">Useful Bitwise Tricks for Problem Solving</a><br><a href="#3f81">C++ STL Built-in Functions</a><br><a href="#09d8">Conclusion</a></p><h3>Why Do We Use Bitwise Operations?</h3><ol><li><strong>Performance</strong>: Bitwise operations are incredibly fast and are often used in performance-critical codes.</li><li><strong>Memory Efficiency</strong>: Working with individual bits allows for compact data representation.</li><li><strong>Low-Level Hardware Access</strong>: In systems programming, bitwise operations are crucial for interacting with hardware devices that represent data using binary.</li></ol><h3>Bitwise Operators:</h3><ul><li><strong>Bitwise AND (</strong><strong>&amp;): </strong><br>Compares each bit of two numbers and returns 1 if both bits are 1; otherwise, returns 0.<br>Example: 1110 &amp; 0111 = 0110</li></ul><pre> A | B | A &amp; B<br>---|---|-------<br> 0 | 0 |   0<br> 0 | 1 |   0<br> 1 | 0 |   0<br> 1 | 1 |   1</pre><ul><li><strong>Bitwise OR (</strong><strong>|)</strong>: <br>Compares each bit of two numbers and returns 1 if at least one of the bits is 1; otherwise, returns 0.<br>Example: 0011 | 1110 = 1111</li></ul><pre> A | B | A | B<br>---|---|-------<br> 0 | 0 |   0<br> 0 | 1 |   1<br> 1 | 0 |   1<br> 1 | 1 |   1</pre><ul><li><strong>Bitwise XOR (</strong><strong>^)</strong>: <br>Compares each bit of two numbers and returns 1 if the bits are different; otherwise, returns 0.<br>Example: 1110 ^ 0110 = 1000</li></ul><pre> A | B | A ^ B<br>---|---|-------<br> 0 | 0 |   0<br> 0 | 1 |   1<br> 1 | 0 |   1<br> 1 | 1 |   0</pre><ul><li><strong>Bitwise NOT (</strong><strong>~)</strong>: <br>Complements or inverts all the bits (i.e. all the 1&#39;s become 0&#39;s and all the 0’s become 1&#39;s).<br>Example: ~1100 = 0011</li></ul><pre>| A (Decimal) | A (Binary) | ~A (Binary) | ~A (Decimal) |<br>|-------------|------------|-------------|--------------|<br>|     0       |   0000     |    1111     |     -1       |<br>|     1       |   0001     |    1110     |     -2       |<br>|     2       |   0010     |    1101     |     -3       |<br>|     3       |   0011     |    1100     |     -4       |<br>|     4       |   0100     |    1011     |     -5       |<br><br><em>Note: Binary shown in 4-bit representation for clarity.</em></pre><ul><li><strong>Left Shift (</strong><strong>&lt;&lt;)</strong>: <br>Shifts the bits of a number to the left by a specified number of positions, filling the right side’s empty places with 0s. And very important observation that each left shift multiplies the number by 2.<br>Example: 1010 &lt;&lt; 1 = 10100 (<em>Here we are having </em><em>1010 i.e. </em><em>10 which is being left shifter by </em><em>1, converting it into </em><em>20 and it is also equivalent to </em><em>10 * 2 = 20</em>)</li><li><strong>Right Shift (</strong><strong>&gt;&gt;)</strong>: <br>Shifts the bits of a number to the right by a specified number of positions, filling the left side’s empty places with the sign bit (for signed integers) or 0 (for unsigned integers). Here each right shift divides the number by 2.<br>Example: 1010 &gt;&gt; 1 = 0101 (which is also equivalent to 10 / 2 = 5)</li></ul><h3><strong>Arithmetic Operations:</strong></h3><ul><li><strong>y = x &lt;&lt; n</strong> Same as writing y = x * (2 ** n): Multiply by power of 2</li><li><strong>y = x &gt;&gt; n</strong> Same as writing y = x / (2 ** n): Divide by power of 2</li><li><strong>(x &amp; 1)</strong> Checks if x is odd</li><li><strong>(x &amp; 1) ^ 1</strong> Checks if x is even</li><li><strong>x &amp;&amp; !(x &amp; (x — 1))</strong> Checks if x is power of 2</li><li><strong>(x &amp; ((1 &lt;&lt; n) - 1)) == 0</strong> Checks if x is divisible by a power of 2</li><li><strong>x ^ y</strong> Checks if x == y</li></ul><h3><strong>Bitwise Operations:</strong></h3><p>1. <strong>x &amp; (1 &lt;&lt; n)</strong>: Checks if the nth bit is set</p><ul><li><strong>Example</strong>: <br><em>Check if the 3rd bit (zero-indexed) is set in </em><em>x = 5 (binary: </em><em>101).</em></li></ul><pre>// Binary: 101<br>x = 5<br><br>n = 2<br><br>Answer = x &amp; (1 &lt;&lt; n)<br>// 101 &amp; 100 = 100 (true, because the 3rd bit is set)</pre><p>2. <strong>(x &gt;&gt; n) &amp; 1</strong>: Checks if the nth bit is set</p><ul><li><strong>Example</strong>: <br><em>Check if the 2nd bit (zero-indexed) is set in </em><em>x = 9 (binary: </em><em>1001).</em></li></ul><pre>// Binary: 1001<br>x = 9<br><br>n = 3<br><br>Answer = (x &gt;&gt; n) &amp; 1<br>// (1001 &gt;&gt; 3) &amp; 1 = 0001 &amp; 1 = 1 (true, because the 3rd bit is set)</pre><p>3. <strong>x = (x ^ (1 &lt;&lt; n))</strong>: Toggles the nth bit of x</p><ul><li><strong>Example</strong>: <br><em>Toggle the 1st bit in </em><em>x = 5 (binary: </em><em>101).</em></li></ul><pre>// Binary: 101<br>x = 5<br><br>n = 1<br><br>x = x ^ (1 &lt;&lt; n)<br>// 101 ^ 010 = 111 (new value is 7)</pre><p>4. <strong>x = (x | (1 &lt;&lt; n))</strong>: Set the nth bit of x</p><ul><li><strong><em>Example</em></strong><em>: <br>Set the 2nd bit in </em><em>x = 1 (binary: </em><em>001).</em></li></ul><pre>// Binary: 001<br>x = 1<br><br>n = 2<br><br>x = x | (1 &lt;&lt; n)<br>// 001 | 100 = 101 (new value is 5)</pre><p>5. <strong>x = (x &amp; ~(1 &lt;&lt; n))</strong>: Unset the nth bit of x</p><ul><li><strong>Example</strong>: <br><em>Unset the 2nd bit in </em><em>x = 7 (binary: </em><em>111).</em></li></ul><pre>// Binary: 111<br>x = 7<br><br>n = 2<br><br>x = x &amp; ~(1 &lt;&lt; n)<br>// 111 &amp; ~(100) = 011 (new value is 3)</pre><p>6. <strong>x = x &amp; (x - 1)</strong>: Turn off the rightmost 1 bit</p><ul><li><strong>Example</strong>: <br><em>Turn off the rightmost 1 bit in </em><em>x = 6 (binary: </em><em>110).</em></li></ul><pre>// Binary: 110<br>x = 6<br><br>x = x &amp; (x - 1)<br>// 110 &amp; 101 = 100 (new value is 4)</pre><p>7. <strong>x = x &amp; -x</strong>: Isolate the rightmost 1 bit</p><ul><li><strong>Example</strong>: <br><em>Isolate the rightmost 1 bit in </em><em>x = 6 (binary: </em><em>110).</em></li></ul><pre>// Binary: 110<br>x = 6<br><br>x = x &amp; -x<br>// 110 &amp; 010 = 010 (isolates rightmost 1 bit, value is 2)</pre><p>8. <strong>x = x | (x - 1)</strong>: Right propagate the rightmost 1 bit</p><ul><li><strong>Example</strong>: <br><em>Propagate the rightmost 1 bit in </em><em>x = 10 (binary: </em><em>1010).</em></li></ul><pre>// Binary: 1010<br>x = 10<br><br>x = x | (x - 1)<br>// 1010 | 1001 = 1011 (new value is 11)</pre><p>9. <strong>x = x | (x + 1)</strong>: Turn on the rightmost 0 bit</p><ul><li><strong>Example</strong>: <br><em>Turn on the rightmost 0 bit in </em><em>x = 10 (binary: </em><em>1010).</em></li></ul><pre>// Binary: 1010<br>x = 10<br><br>x = x | (x + 1)<br>// 1010 | 1011 = 1011 (new value is 11)</pre><p>10. <strong>x = ~x &amp; (x + 1)</strong>: Isolate the rightmost 0 bit</p><ul><li><strong>Example</strong>: <br><em>Isolate the rightmost 0 bit in </em><em>x = 13 (binary: </em><em>1101).</em></li></ul><pre>// Binary: 1101<br>x = 13<br><br>x = ~x &amp; (x + 1)<br>// ~1101 &amp; 1110 = 0010 (new value is 2)</pre><h3><strong>Useful Bitwise Tricks for Problem Solving</strong></h3><ol><li><strong>Loop until </strong><strong><em>i == 0</em></strong></li></ol><pre>for (int i=n; ~i; i--) <br>{<br>    // do something<br>}</pre><p>2. <strong>Number of set bits in a number </strong><strong><em>n</em></strong></p><pre>// First Approach (Shifting Right):<br>int countSetBits(int n)<br>{<br>    int numberOfSetBits = 0;<br><br>    while (n)<br>    {<br>        // Check if the least significant bit is 1<br>        numberOfSetBits += (n &amp; 1);<br>        <br>        // Right shift to check the next bit<br>        n &gt;&gt;= 1;<br>    }<br><br>    return numberOfSetBits;<br>}</pre><pre>// Second Approach (Brian Kernighan’s Algorithm):<br>int countSetBits(int n)<br>{<br>    int numberOfSetBits = 0;<br><br>    while (n)<br>    {<br>        // This removes the rightmost set bit<br>        n &amp;= (n - 1);<br>        numberOfSetBits++;<br>    }<br><br>    return numberOfSetBits;<br>}</pre><h4><strong>3. Swapping two numbers without a temporary variable</strong>:</h4><blockquote><strong>x = x ^ y<br>y = x ^ y<br></strong>x = x ^ y</blockquote><pre>int x = 5;  // x == 0101<br>int y = 10; // y == 1010<br><br>cout &lt;&lt; x &lt;&lt; &quot; &quot; &lt;&lt; y &lt;&lt; endl;<br><br>x = x ^ y; <br>// x = 0101 (5)<br>// y = 1010 (10)<br>// x ^ y = 1111 (15)<br>// x becomes 1111 (15), y remains 1010 (10)<br><br><br>y = x ^ y; <br>// New x = 1111 (15)<br>// y = 1010 (10)<br>// x ^ y = 0101 (5)<br>// y becomes 0101 (5), x is still 1111 (15)<br><br><br>x = x ^ y; <br>// New x = 1111 (15)<br>// New y = 0101 (5)<br>// x ^ y = 1010 (10)<br>// x becomes 1010 (10), y is still 0101 (5)<br><br>cout &lt;&lt; x &lt;&lt; &quot; &quot; &lt;&lt; y &lt;&lt; endl;</pre><h4>4. Checking if a number is a power of 2:</h4><pre>bool isPowerOfTwo(int n)<br>{<br>    // First, check if n is not zero (n != 0).<br>    // The expression &#39;n&#39; evaluates to true if n is non-zero.<br>    // If n is zero, the whole expression will return false,<br>    // because 0 is not a power of two.<br>    return (n &amp;&amp;<br>            // Then, check if &#39;n&#39; is a power of two by using the expression &#39;!(n &amp; (n - 1))&#39;.<br>            // The expression (n &amp; (n - 1)) clears the least significant set bit of n.<br>            // For powers of two, the binary representation has exactly one set bit.<br>            // For example, 4 (100 in binary) &amp; 3 (011 in binary) = 000.<br>            // This would result in zero, meaning n is a power of two.<br>            // If &#39;n&#39; is not a power of two, this expression will not be zero.<br>            !(n &amp; (n - 1)));<br>}</pre><h4>5. Rounding Down to the Nearest Power of 2:</h4><pre>// x &amp; ~(x - 1)<br><br>int lowestPowerOfTwo(int x)<br>{<br>    // x = 12; (x = 1100)<br><br>    // Step 1: Subtract 1 from x<br>    // x = 12 (1100 in binary), then<br>    // x - 1 = 11 (1011 in binary)<br>    int xMinus1 = x - 1;<br><br>    // Step 2: Bitwise NOT (~) of (x - 1)<br>    // xMinus1 = 1011 (binary), then <br>    // ~(x - 1) = 0100 (binary)<br>    int notXMinus1 = ~xMinus1;<br><br>    // Step 3: Bitwise AND (&amp;) of x and ~(x - 1)<br>    // x = 1100 (binary) and ~(x - 1) = 0100 (binary)<br>    // x &amp; ~(x - 1) results in 0100 (binary), which is 4<br>    return (x &amp; notXMinus1);<br>}</pre><h3><strong>Some Useful C++ STL Functions:</strong></h3><ul><li><strong><em>__builtin_popcount(n)</em></strong>: Counts the number of bits in <em>n</em></li><li><strong><em>__builtin_clz(n)</em></strong>: Counts the number of leading zeros in <em>n</em></li><li><strong><em>__builtin_ctz(n)</em></strong>: Counts the number of trailing zeros in <em>n</em></li><li><strong><em>__builtin_parity(n)</em></strong>: Checks the parity of a number (odd number of set bits)</li></ul><p><strong><em>NOTE:</em></strong><em> To make these work for </em><strong><em>long long</em></strong><em> data type, just put </em><strong><em>ll</em></strong><em> in the end of the functions, e.g. </em><strong><em>__builtin_popcountll(n)</em></strong><em> will return the number of set bits in </em><strong><em>n</em></strong><em> of </em><strong><em>long long</em></strong><em> data type</em></p><h3>Conclusion:</h3><p>Bit manipulation is a powerful and efficient tool in a programmer’s arsenal, especially for low-level programming, algorithm optimization, and solving complex computational problems. Mastering bitwise operators not only improves the understanding of how computers represent and process data, but also helps us to write faster and more memory-efficient code.</p><p>With consistent practice and use, these patterns and techniques will become second nature, enabling you to solve problems more creatively and efficiently. Keep this sheet as a handy reference and in case of any feedback drop it in the comment section — and happy coding! 💻</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=166f6c9dfc16" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Introduction To Version Control with Git]]></title>
            <link>https://medium.com/@avinashyadav16/introduction-to-version-control-with-git-9956e05c17da?source=rss-570103f5c627------2</link>
            <guid isPermaLink="false">https://medium.com/p/9956e05c17da</guid>
            <category><![CDATA[github-actions]]></category>
            <category><![CDATA[github-community]]></category>
            <category><![CDATA[git]]></category>
            <category><![CDATA[commands]]></category>
            <category><![CDATA[github]]></category>
            <dc:creator><![CDATA[Avinash Yadav]]></dc:creator>
            <pubDate>Fri, 18 Oct 2024 21:54:59 GMT</pubDate>
            <atom:updated>2024-10-20T19:42:51.758Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>In this article we will try to get an introduction to version control, and Git. <br>Git can seem a little cryptic at first, and it can even be frustrating at times. But if we learn it step by step, we’ll find that there’s a reason Git has become the world’s most popular version control system — not just for software developers, but also for teams that write documentation and collaborate on other works.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*CeH0Fa6Qaxsgpj5N" /><figcaption>Image Credit: Google</figcaption></figure><h3><strong>What is version control?</strong></h3><p>A version control system (VCS) is a <strong><em>program or set of programs that tracks changes to a collection of files</em></strong>. Another name for a VCS is a <strong>software configuration management</strong> (SCM) system. Technically, version control is just one of the practices involved in SCM.</p><ul><li>One goal of a VCS is to <strong>easily recall earlier versions</strong> of individual files or of the entire project by keeping track of code changes.</li><li>Another goal is to <strong>allow several team members to</strong> <strong>collaborate on a project</strong>, even on the same files, at the same time without affecting each other’s work.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9gYsyBVE4Zjobeqm_T0asA.png" /></figure><h3>What we can do with a VCS:</h3><ul><li><strong>See all the changes</strong> made to our project, when the changes were made, and who made them.</li><li><strong>Include a message</strong> with each change to explain the reasoning behind it.</li><li><strong>Retrieve past versions</strong> of the entire project or of individual files.</li><li><strong>Create <em>branches</em></strong>, where changes can be made experimentally. This feature allows several different sets of changes to be worked on at the same time without affecting the main branch. Later, we can merge the changes that you want to keep back into the main branch.</li><li><strong>Attach a tag</strong> to a version — for example, to mark a new release.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9gYsyBVE4Zjobeqm_T0asA.png" /></figure><h3><strong>Some Git terminology:</strong></h3><p>To understand Git, we have to understand the terminology. Here’s a short list of terms that Git users frequently use.</p><ul><li><strong>Working tree</strong>: It is the set of nested directories and files that contain the project that’s being worked on.</li><li><strong>Repository (repo)</strong>: The directory, located at the top level of a working tree, where Git keeps all the history and metadata for a project. Repositories are almost always referred to as <em>repos</em>. A <em>bare repository</em> is one that isn’t part of a working tree; it’s used for sharing or backup. A bare repo is usually a directory with a name that ends in <em>.git</em> — for example, <em>project.git</em>.</li><li><strong>Commit</strong>: <em>commit</em> means to make a commit object. This action takes its name from commits to a database. It means you are committing the changes you have made so that others can eventually see them, too.</li><li><strong>Branch</strong>: A branch is a named series of linked commits. The most recent commit on a branch is called the <em>head</em>. The default branch, which is created when you initialize a repository, is called main, often named master in Git. The head of the current branch is named HEAD. Branches are an incredibly useful feature of Git because they allow developers to work independently (or together) in branches and later merge their changes into the default branch.</li><li><strong>Remote</strong>: A remote is a named reference to another Git repository. When you create a repo, Git creates a remote named origin that is the default remote for push and pull operations.</li><li><strong>Commands, subcommands</strong>, and <strong>options</strong>: Git operations are performed by using commands like git push and git pull. git is the command, and push or pull is the subcommand. The subcommand specifies the operation you want Git to perform. Commands frequently are accompanied by options, which use hyphens (-) or double hyphens (--). For example, <em>git reset --hard</em>.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9gYsyBVE4Zjobeqm_T0asA.png" /></figure><p><em>Finally, now we are here to begin the journey with Giiiittttttt :)</em></p><h3><strong>Git and GitHub</strong></h3><p>As we work with Git, we might wonder about differences between the features it offers and the features offered on GitHub.</p><p>As we saw earlier, Git is a Distributed Version Control System (DVCS) that multiple developers and other contributors can use to work on a project. It provides a way to work with one or more local branches and then push them to a remote repository.</p><p>GitHub is a cloud platform that uses Git as its core technology. GitHub simplifies the process of collaborating on projects and provides a website, more command-line tools, and overall flow that developers and users can use to work together. GitHub act s as the remote repository mentioned earlier.</p><p>Some of the Key features provided by GitHub include:</p><ul><li><em>Issues</em></li><li><em>Discussions</em></li><li><em>Pull requests</em></li><li><em>Notifications</em></li><li><em>Labels</em></li><li><em>Actions</em></li><li><em>Forks</em></li><li><em>Projects, …, etc.</em></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9gYsyBVE4Zjobeqm_T0asA.png" /></figure><h3>Let’s Begin:</h3><p>First visit <a href="https://github.com/"><strong><em>https://github.com</em></strong></a> and sign-up with your e-mail id and choose a password and username as per your preference. Verify the email address with the verification code and now you can sign-in with the created username as password.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HRdvCNkFCuVZPRhBRrHlBg.png" /></figure><p>Hurrey after successful login, you can see your GitHub dashboard:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zah3FGjL5NcksKc9mfSPmw.png" /></figure><p>So, now let’s proceed setting up the git on our local system.</p><p><strong>Step-01:</strong> Download Git from <a href="https://git-scm.com/downloads"><strong><em>https://git-scm.com/downloads</em></strong></a> as per your system and its configurations.</p><p><strong>Step-02:</strong> Install the downloaded Git Bash to your local system by keeping all the default selections(recommended) or you can also customize as per your wish.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*imdS0u5IT0fZ6mK6C71owg.jpeg" /></figure><h4>Let’s Start with a Fresh Project:</h4><h3><strong>1. Configuring git for the first time in the terminal:</strong></h3><pre>git config --global user.name &#39;ENTER YOUR USERNAME HERE&#39;<br>git config --global user.email &#39;ENTER YOUR E-MAIL HERE&#39;</pre><p>NOTE: <em>Make sure when you’re coping &amp; pasting the commands, the syntax matches the given above commands.</em></p><h3><strong>2. Working With Git:</strong></h3><h4><strong>2.1. Initializing Git to a New Project:</strong></h4><pre>git init</pre><p><em>Git now knows that it should watch the folder you initiated it on. Git creates a hidden folder ( </em><em>.git )to keep track of changes.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/396/1*LelSdFVoIVB6zLnMXpt04Q.png" /></figure><h4><strong>2.2. Checking Git Status:</strong></h4><ul><li><strong>Purpose</strong>: It shows up the current state of the working directory and the staging area, what are the status of files in our local repository as compared to Git repository.</li><li>It helps to know which files are staged for commit, which are not tracked, and any changes that have been made.</li></ul><pre>git status<br><br><br>// File status in a more compact way:<br>git status --short</pre><figure><img alt="Checking Git Status" src="https://cdn-images-1.medium.com/max/400/1*vtayL-WRMfvqKzWU2OFjxQ.png" /><figcaption>Checking Git Status</figcaption></figure><h4><strong>2.3. Staging files / Adding files to Git repo:</strong></h4><p>Staged files are files that are ready to be committed to the repository you are working on. When you first add files to an empty repository, they are all untracked. To get Git to track them, you need to stage them, or add them to the staging environment. It adds changes from the working directory to the staging area (also known as the index), preparing them for a commit.</p><pre>git add &lt;FILENAME_WITH_EXTENSION&gt;</pre><p><strong><em>Staging all files in a folder:</em></strong></p><pre>git add .<br><br>OR<br><br>git add --all<br><br>OR<br><br>git add -A</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/403/1*ym-93OdYpFm3dooD0DerWA.png" /></figure><h4>2.4. <strong>Committing Changes:</strong></h4><p>Adding commits keep track of our progress and changes as we work. Git considers each commit change point or “save point”. It is a point in the project you can go back to if you find a bug, or want to make a change. When we commit, we should always include a message. The commit message should describe what the changes are.</p><pre> git commit -m “ENTER THE MESSAGE HERE”</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/401/1*StSZtWlUPPeggbd4fpXtTw.png" /></figure><p><strong><em>Git Commit without Stage:</em></strong></p><p>Sometimes, when we make small changes, using the staging environment seems like a waste of time. It is possible to commit changes directly, skipping the staging environment.</p><pre>git commit -a -m &quot;ENTER THE MESSAGE HERE&quot;</pre><h4>2.5. <strong>Viewing Commit History:</strong></h4><p>Log is used to view the history of commits for a repo.</p><ul><li>We can also use options like --oneline or --graph for a more concise or visual view.</li></ul><pre>git log<br><br>OR<br><br>git log --oneline<br><br>OR<br><br>git log --graph</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/402/1*ICPGkytfdVLLYO5OAvy-sQ.png" /></figure><h4>2.6. <strong>Creating and Switching Branches:</strong></h4><p>In Git, a branch is a <em>new/separate version of the main repository</em>. Branches allow you to work on different parts of a project without impacting the main branch. When the work is complete, a branch can be merged with the main project. We can even switch between branches and work on different projects without them interfering with each other.</p><pre># Checking All The Available Branches:<br>git branch</pre><pre># Create A New Branch:<br>git branch &lt;BRANCH-NAME&gt;<br><br><br># Switch To An Existing Branch:<br>git checkout &lt;BRANCH-NAME&gt;<br><br><br># Create And Switch To A New Branch In One Step:<br>git checkout -b &lt;BRANCH-NAME&gt;</pre><pre># Deleting A Branch:<br>git branch -d &lt;BRANCH-NAME&gt;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/399/1*XGP99aBy-sfrb9IIOxx8RA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/403/1*MPg1seoEpWwz68gd6rPKNQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/402/1*XaRtAE3bjnPp7ybW3fVvOQ.png" /></figure><h3>2.7. <strong>Merging Branches:</strong></h3><p>Merges changes from one branch into another, incorporating the changes and creating a new commit representing the merge.</p><p>It’s preferred to change/switch to master branch before any branch needs to be merged with it.</p><pre># Switch to the branch you want to merge into (often &quot;main&quot; or &quot;master&quot;)<br>git checkout &lt;TARGET-BRANCH&gt;<br><br><br># Merge another specified branch into the current branch<br>git merge &lt;SOURCE-BRANCH&gt;      </pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/401/1*oygtOiaD31k8i2J4RDwOrQ.png" /></figure><h4>2.8. <strong>Checking Differences:</strong></h4><p>Displays differences between files in different stages, allowing you to see what has changed before committing.</p><pre># Show changes between working directory and staging area<br>git diff<br><br><br># Show changes between the staging area and the last commit<br>git diff --staged<br><br><br># Compare two commits<br>git diff &lt;commit1&gt; &lt;commit2&gt;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/403/1*qr3ySSX58VSUXEl2S7l3gg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/402/1*xohmF2b6RBrZnmoXoDbHbg.png" /></figure><h4>2.9. <strong>Undoing Changes:</strong></h4><ul><li><strong>Undo changes in working directory</strong>:</li></ul><pre>git checkout -- &lt;filename&gt;</pre><p>This command discards changes in the working directory (<em>restores the file to the last committed state</em>).</p><ul><li><strong>Unstage changes</strong>:</li></ul><pre>git reset &lt;filename&gt;</pre><p>This removes a file from the staging area, but leaves the changes in the working directory.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/403/1*Cu2o4OFHamwP6TnI1AgDwQ.png" /></figure><ul><li><strong>Amend a commit</strong>:</li></ul><pre>git commit --amend</pre><p>This allows you to modify the most recent commit (e.g., <em>to change the commit message or include additional changes</em>).</p><h4>2.10. Deleting Files and Folders:</h4><ul><li>Removes files or directories from the working directory and stages the removal for commit.</li></ul><pre># Remove a file from both the working directory and staging area<br>git rm &lt;filename&gt;<br><br><br># Remove a folder<br>git rm -r &lt;folder&gt;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/402/1*xxq2JjM6xtkAiJdt0NVecA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9gYsyBVE4Zjobeqm_T0asA.png" /></figure><h3><strong>3. Working with GitHub:</strong></h3><p>Now let’s create a new repository where we will be uploading our files from local repository.</p><p><strong>Note: </strong><em>Local repository (repo) means the repository which is on our system whereas, remote repository means the one which is on another remote system/server, for e.g. — GitHub, GitLab, etc.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*smmKK_xplCCKWc_fCWMSNQ.png" /></figure><h4>3.1. <strong>Push Local Repository to GitHub:</strong></h4><ul><li>Copy the URL or the link of the repo that we just created. As an example, it should look like this: <em>https://github.com/avinashyadav16/sample.git</em></li></ul><pre>git remote add origin &lt;PASTE-COPIED-REPO-URL&gt;</pre><p>‘<em>git remote add origin &lt;URL&gt;</em>’ specifies that we are adding a remote repository, with the specified URL, as an origin to our local Git repo.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/401/1*CqELxs3mglStwV7XsdMoDw.png" /></figure><p>Finally, pushing our master branch to the origin URL (remote repo) and set it as the default remote branch.</p><pre>git push -- set-upstream origin master</pre><p>Go back into GitHub and refresh it to see that the repository will be updated with the new files.</p><h4>3.2. Pushing Changes to a Remote Repository:</h4><p><em>Pushing local repo to GitHub after doing the above process at least once.</em><strong> </strong>First commit all the changes. Then push all the changes to our remote origin i.e. remote repo on GitHub i.e. uploading our local commits to a remote repository.</p><pre>git push origin &lt;BRANCH-NAME&gt;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/401/1*fpiNtvDrAxcfgdFGncYC8Q.png" /></figure><h4><strong>3.3. Pushing A Branch To GitHub (</strong>Remote Repository)<strong>:</strong></h4><p>First, let’s create a new local branch which we will be pushing to GitHub. Enter the command as:</p><blockquote>git checkout -b &lt;NEW-BRANCH-NAME&gt;</blockquote><p>You can check the status of the files in this current branch using ‘<strong><em>git status</em></strong>’. Commit all the uncommitted changes for all the files in this branch using:</p><blockquote>git commit -a -m “COMMIT-MESSAGE”</blockquote><p>Now push this branch from our local repo to GitHub using:</p><blockquote>git push origin &lt;NEW-BRANCH-NAME&gt;</blockquote><h4>3.4. Resetting Commits:</h4><p>Resets the current branch to a specific commit, discarding all subsequent changes.</p><p><strong>Warning</strong>: <strong><em>--hard</em></strong> will remove all changes after the reset point.</p><pre>git reset --hard &lt;COMMIT-ID&gt;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/397/1*FT1V1LepkT6JfFxnEXAOYQ.png" /></figure><h4>3.5. Fetching and Pulling Changes from a Remote Repository:</h4><p>Retrieves changes from a remote repository and applies them to our local repository.</p><ul><li><strong>Fetch changes</strong> (<em>doesn’t apply them to our working directory</em>):</li></ul><pre>git fetch</pre><ul><li><strong>Pull changes</strong> (<em>fetch + merge changes into our current branch</em>): Git pull is used to pull all changes from a remote repository into the branch we are working on. It is a combination of fetch and merge. Use it to update your local Git.</li></ul><pre>git pull<br><br>OR<br><br>git pull origin<br><br>OR<br><br>git pull main</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/398/1*eGDbIgENfd8GkQp9ocCptQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/401/1*mKpvbMWYMFdCVhomGrlICw.png" /></figure><h4><strong>3.6. Pulling Branch From GitHub:</strong></h4><p>First, check which branches we have and where are we working at the moment by ‘<strong><em>git branch</em></strong>’ command. Since we do not have the new branch on out local Git which is to be pulled from the GitHub. So, to see all local and remote branches, use:</p><blockquote>git branch -a</blockquote><p><strong>For viewing only remote branches:</strong></p><blockquote>git branch -r</blockquote><p>Now, the new branch is seen in the console but it is not available on our local repo. So, let’s check it out using ‘<strong><em>git checkout &lt;branch name&gt;</em></strong>’. Now run ‘<strong><em>git pull</em></strong>’ to pull that branch on our local repo. We can now check the available branches using ‘<strong><em>git branch</em></strong>’.</p><h4>3.7. Stashing Changes:</h4><p>Temporarily stores changes, modified or tracked files in the working directory that are not ready to be committed. Useful when you want to switch branches but keep your changes for later.</p><pre>git stash</pre><ul><li><strong>Apply stashed changes</strong>:</li></ul><pre>git stash apply</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/397/1*oXGetkSuQGxCZjPKruZucw.png" /></figure><h4>3.8. Cloning a Repository:</h4><p>We can clone a forked repo from GitHub on our local repo. A clone is a full copy of a repository, including all logging and versions of files. In other way, it creates a local copy of a remote repository. This is the starting point for contributing to an existing project.</p><ol><li><em>Move back to the original repository, and click the green “</em><strong><em>Code</em></strong><em>” button to get the URL to clone.</em></li><li><em>Copy the URL.</em></li><li><em>Now in the git bash, enter the following command to clone the copied repo onto your local machine:</em></li></ol><pre>git clone &lt;COPIED-REPOSITORY-URL&gt;</pre><ul><li><em>To specify a specific folder to clone to, add the name of the folder <br>after the repository URL, like this:</em></li></ul><pre>git clone &lt;COPIED-REPOSITORY-URL&gt; &lt;FOLDER-NAME-TO-CLONE&gt;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/402/1*HxOQizKQtn91l2ukLbP7Qw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*ri9ZI4v6wKoeQOK3NxvSDw.png" /></figure><h3>Ignoring Patterns:</h3><p>In Git, a <strong><em>.gitignore</em></strong> file is used to specify which files or directories should be <strong>ignored</strong> by Git. This means that those files won’t be tracked, staged, or committed to the repository. This is useful for excluding unnecessary or sensitive files such as logs, temporary files, or build artifacts from being included in version control.</p><pre>logs/          # Specific files: This pattern will ignore the entire logs/ directory.<br>*.notes        # Wildcard globs: This pattern ignores all files ending with .notes (e.g., meeting.notes).<br>pattern*/      # Directories: The trailing slash / indicates that all directories starting with pattern will be ignored (e.g., pattern1/, pattern2/).<br><br>...<br>...<br>...</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/399/1*9bjkM3S-niG_SCzo8Mj9zw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*ri9ZI4v6wKoeQOK3NxvSDw.png" /></figure><h3>Some Of the Top Bad Practices When Using Git:</h3><p>❌ <strong>Large Commits: </strong><br><em>Making large, uniform commits that are hard to review and understand.</em></p><p>⛓️‍💥 <strong>Infrequent Commits: </strong><br><em>Making infrequent commits, which makes it harder to track progress and identify issues.</em></p><p>🗨️ <strong>Poor Commit Messages: </strong><br><em>Writing vague or uninformative commit messages like “fixed bug” or “update code”.</em></p><p><strong>😏 Ignoring Conflicts: </strong><br><em>Not resolving merge conflicts promptly, leading to complicated conflicts later.</em></p><p><strong>🌿 Not Using Branches: </strong><br><em>Working directly on the main branch, increasing the risk of unstable code.</em></p><p><strong>🪠 Frequent Force Pushes: </strong><br><em>Using </em><em>git push --force carelessly, which can overwrite others’ work.</em></p><p><strong>😏 Ignoring .gitignore: </strong><br><em>Not using or mismanaging the .gitignore file, leading to unnecessary files being tracked.</em></p><p><strong>🔐 Committing Secrets: </strong><br><em>Including sensitive information like passwords or API keys in the repository.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9gYsyBVE4Zjobeqm_T0asA.png" /></figure><h3>Conclusion:</h3><p>In conclusion, Git is an essential tool for version control, enabling developers to track changes, collaborate seamlessly, and manage projects efficiently. By understanding key commands and workflows, such as branching, merging, and using GitHub for remote collaboration, you can streamline development processes and maintain project integrity. With practice, Git becomes an invaluable asset in any development environment.</p><blockquote>Please do like and share if it benefits you in anyway and all suggestions are gratefully appreciated and accepted. ❤</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9956e05c17da" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>