<?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 Kapil Bhardwaj on Medium]]></title>
        <description><![CDATA[Stories by Kapil Bhardwaj on Medium]]></description>
        <link>https://medium.com/@kapilbhardwaj850?source=rss-bc8547726b7d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*2w-5GW2VOidMKPX0</url>
            <title>Stories by Kapil Bhardwaj on Medium</title>
            <link>https://medium.com/@kapilbhardwaj850?source=rss-bc8547726b7d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 02:30:24 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@kapilbhardwaj850/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[Let’s easily learn DP]]></title>
            <link>https://python.plainenglish.io/lets-easily-learn-dp-67c2f3e619c2?source=rss-bc8547726b7d------2</link>
            <guid isPermaLink="false">https://medium.com/p/67c2f3e619c2</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[dynamic-programming]]></category>
            <dc:creator><![CDATA[Kapil Bhardwaj]]></dc:creator>
            <pubDate>Wed, 26 Nov 2025 16:41:11 GMT</pubDate>
            <atom:updated>2025-11-26T16:41:11.762Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*W97XBM_Akp2r7RpB4Mziew.jpeg" /></figure><p>Okay, so before we start talking about DP (Dynamic Programming), I personally want to thank you for starting and committing to learning. Only one percent have the capability to learn without getting distracted. So, clap for yourself. Let’s dig into the concept now.</p><p>We all know about this programming stuff where we write some lines using English and math to make a computer do something. It is still doing well, but according to me, most code written today is just a working module. It just works, and whenever something comes up, we have to rewrite or fine-tune it to handle pressure. So, companies started to hire people who not only code but people who can code better — who can write an efficient program that takes less time and produces more output. So, how can you be that programmer who codes better? In this scenario, DP comes to light. Let’s learn about this DP thing now in deep detail.</p><h3>What Is Dynamic Programming?</h3><p>Think of Dynamic Programming like this: You are in school, and you know tables from 2 to 5, and now you have to learn from 6. So whenever someone asks you to recite the tables, you will start from 6, not 2, because you already know the tables from 2 to 5. Also, it is a waste of time to calculate that thing again and again. This is Dynamic Programming, where you don’t compute the same calculation again and again. You just store them and reuse them. Whenever there is a new calculation, you also store it for future reuse. This will save a lot of time, because now you don’t have to compute again and again.</p><p>Let me give you a proper definition too, as college students are also reading:<br>“<strong>Dynamic Programming (DP)</strong> is an algorithmic technique for solving complex problems by breaking them down into simpler <strong>subproblems</strong>. It’s primarily applicable to problems exhibiting two key properties: <strong>optimal substructure</strong> and <strong>overlapping subproblems</strong>.”</p><h3>Basics Of Dynamic Programming.</h3><p>DP solutions are typically implemented using one of two methods, both of which use extra memory (a “table”) to store results.</p><ol><li>Top-Down Approach with <strong>memoization</strong>.</li><li>Bottom-Up approach with <strong>tabulation</strong>.</li></ol><h4>Top-Down Approach with <strong>memoization</strong></h4><p>Top-Down Approach = “Solve when needed + Remember the answer”</p><p>Imagine you’re cooking a complicated dish for the first time.</p><ol><li>You start cooking the final recipe.</li><li>You reach a step where you need a <strong>sauce</strong> → you quickly check how to make it.</li><li>While making the sauce, you realize you need a <strong>spice mix</strong> → you check that too.</li><li>Once you learn the sauce + spice mix, you write them down.</li></ol><p>Next time:</p><ul><li>Instead of relearning the sauce &amp; spice mix,</li><li>You look at your notes and complete the dish faster.</li></ul><p>This is <strong>Top-Down DP</strong>.<br> You solve the final task → go down into smaller tasks → store answers.</p><p>Proper Definition “<strong>Top-Down DP is a recursive approach that solves a problem by breaking it into smaller subproblems and storing their results so that repeated subproblems are computed only once.”</strong></p><p>Let’s Learn this with the help of code now:</p><p>The most basic problem to understand this is the Fibonacci problem, where each upcoming number is the sum of the previous two numbers.</p><pre>0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...</pre><p>First, let&#39;s code it without DP:</p><pre>def fib(n):<br>    if n &lt;= 1:      <br>        return n<br>    return fib(n-1) + fib(n-2)</pre><p>So in this code, we are calculating the Fibonacci of a number, suppose the number is 5. So, we have to find the Fibonacci of 5. How can we do it?</p><pre>                         fib(5)<br>                       /        \<br>                 fib(4)        fib(3)<br>                /      \        /     \<br>          fib(3)     fib(2)  fib(2)  fib(1)<br>          /    \      /   \    /   \<br>    fib(2)  fib(1) fib(1) fib(0) fib(1) fib(0)<br>     /   \<br> fib(1) fib(0)</pre><p>Okay, can you see the pattern here?<br>On careful observation, <strong>you will observe that fib(3), fib(2), and fib(1) appear multiple times</strong>. So in the above code snippet, we are calculating some calculations again and again. This gonna make this program slow and time-consuming. What if have something that memorizes each calculation, and we can use that in the future if the same calculation is needed? Yes, we can do this using DP, especially withthe Top-Down approach with memoization.</p><p><strong>Same Problem using DP</strong></p><pre>def fib(n, memo={}):<br>    if n in memo:<br>        return memo[n]<br>    <br>    if n &lt;= 1:<br>        return n<br>    <br>    memo[n] = fib(n-1, memo) + fib(n-2, memo)<br>    return memo[n]  </pre><p>Can you see this <strong>memo{}</strong> thing? This is memoization we used in the above code snippet. Let me visualize this for you.</p><pre>fib(5)<br> ├── fib(4)<br> │     ├── fib(3)<br> │     │     ├── fib(2)   (stored)<br> │     │     └── fib(1)<br> │     └── fib(2) (from memo)<br> └── fib(3) (from memo)</pre><p>In this case, we are not computing the same calculation again. We store every calculation and use it wherever it is needed to save time.</p><p>Like we stored fib(3), fib(2) in memo and when we need that again, we check the memo if the calculation is there? Yes, it is there, so we are using it.</p><p>I hope till now you at least understand the basics of DP.</p><p>Let’s understand the Bottom-Up approach now.</p><h4>Bottom-Up approach with Tabulation</h4><p>The Bottom-Up approach is the opposite of the Top-Down approach. In this, we first solve the smallest problems first, then we proceed with the bigger problems.</p><p>Think of it like this, suppose you have to save 5000$ for something. You can’t save 5000$ in one instead, you first save your first 100$ and record it in a notebook. Then you save 200$, 300$, and so on…. now after a time you pick that notebook and start calculating it. Bang, you saved 5000$. So, instead of solving big you start solving small problems.</p><p>Let’s solve Fibonacci using the Bottom-Up approach. As we know, we have to solve small problems first. So, first, create a table where we will create an entry.</p><pre>dp = [0] * (n+1)</pre><p>Now we will declare the base cases, we know Fibonacci starts with 0 and 1. So fix this.</p><pre>dp[0] = 0<br>dp[1] = 1</pre><p>Fill the table from bottom to top</p><pre>dp[2] = dp[1] + dp[0] = 1<br>dp[3] = dp[2] + dp[1] = 2<br>dp[4] = dp[3] + dp[2] = 3<br>dp[5] = dp[4] + dp[3] = 5</pre><p><strong>Keep in mind that Fibonacci is the sum of the previous 2 numbers.</strong></p><p>So, what does the table look like now?</p><pre>n   dp[n]<br>--------------<br>0 → 0<br>1 → 1<br>2 → 1<br>3 → 2<br>4 → 3<br>5 → 5  ← final answer</pre><p><strong>Let’s Code</strong></p><pre>def fib(n):<br>    if n &lt;= 1:<br>        return n<br>    <br>    dp = [0] * (n+1) #create the table<br>    dp[0] = 0 #base case <br>    dp[1] = 1 #base case<br>    <br>    for i in range(2, n+1):<br>        dp[i] = dp[i-1] + dp[i-2]  #filling the table<br>    <br>    return dp[n]</pre><h3>Top-Down vs Bottom-Up</h3><h4>Real-Life Example: Ordering Pizza</h4><p><strong>Top-Down (Memoization) → “Order only when needed.</strong>”</p><p>Imagine you want to calculate how many pizza slices you’ll need for your friends:</p><ul><li>You ask: “How many slices does the 10th friend need?”</li><li>If you don’t know → you ask again for the 9th friend → then for the 8th → … until you reach the base cases.</li><li>You <strong>only compute when required</strong>.</li><li>Once you compute a friend’s requirement, you <strong>store it</strong> in memory.</li><li>Next time someone asks for the 8th friend → <strong>you directly check your note</strong> (memo).</li></ul><p>✔ Efficient<br> ✔ Lazy (compute only when required)<br> ✔ Uses recursion<br> ❌ Needs extra space for recursive call stack</p><p><strong>Bottom-Up (Tabulation) → “Prepare all slices beforehand.”</strong></p><p>Instead of waiting for someone to ask:</p><ul><li>You prepare slice requirements for friends 0, 1, 2, … up to 10.</li><li>You build the table step by step.</li><li>By the time someone asks for the 10th friend → <strong>you already have the answer</strong>.</li></ul><p>✔ Iterative<br> ✔ No recursion overhead<br> ✔ Very fast<br> ❌ Computes even values you may not need (sometimes unnecessary work)</p><p><strong>Did you understand? In the Top-down, we calculate when needed and check if it&#39;s there. If yes, use it. If not, calculate and store.</strong></p><p><strong>In the Bottom-up, calculate everything and present whenever needed.</strong></p><h3>Code For Real</h3><p>In this section, we will encounter 2 problems using DP. Their difficulty level will be easy to medium, so you can understand how to implement DP.</p><h4>Problem 1. Climbing Stairs</h4><h3>Problem Statement</h3><p>You are standing at the bottom of a staircase with <strong>n steps</strong>.</p><ul><li>You can climb <strong>1 step</strong> or <strong>2 steps</strong> at a time.</li><li>How many <strong>distinct ways</strong> can you reach the <strong>top</strong>?</li></ul><p>Okay lets solve it.</p><p><em>We know there must be strais more than 2 to start the problem.</em></p><p><em>We can either climb 1 or 2 staris at a same time.</em></p><pre>def climb_stairs(n):<br>    if n &lt;= 2:<br>        return n<br><br>    dp = [0] * (n+1) #table creation<br>    dp[1], dp[2] = 1, 2 #base cases <br><br>    for i in range(3, n+1):  #recusive calls<br>        dp[i] = dp[i-1] + dp[i-2]<br><br>    return dp[n]<br></pre><p>Visualization</p><pre>dp[1] = 🟦<br>dp[2] = 🟦🟦<br><br>dp[3] = (dp[2] + dp[1]) = 🟦🟦 + 🟦 = 🟦🟦🟦<br>dp[4] = (dp[3] + dp[2]) = 🟦🟦🟦 + 🟦🟦 = 🟦🟦🟦🟦🟦</pre><p>So we had base cases, we know either we can take 1 step of two. Suppose there aren numbers of staris one way can be (n-1) other can be (n-2). Thats why we have<strong> dp[i] = dp[i-1] + dp[i-2].</strong></p><p>So if n=4 we know there are 5 ways to climb the stairs.</p><p>now question for you tell me which approach i followed here? comment pleas.</p><p><strong>Problem 2. Robbing House</strong></p><h3>Problem Statement</h3><p>You are a thief who wants to rob houses along a street.</p><ul><li>Each house has some amount of money.</li><li>You <strong>cannot rob two adjacent houses</strong>, or the police will catch you.</li><li>Find the <strong>maximum amount of money</strong> you can rob.</li></ul><p>Example:</p><pre>nums = [2, 7, 9, 3, 1]<br>output=12</pre><p>How?</p><p>Rob houses:</p><ul><li>Rob house 1 → 2</li><li>Rob house 3 → 9</li><li>Rob house 5 → 1<br> Total = <strong>12</strong></li></ul><p>You couldn’t rob adjacent houses like 7 and 9.</p><p><strong>Lets Code it.</strong></p><pre>def rob(nums):<br>    if len(nums) == 1:<br>        return nums[0]<br><br>    dp = [0] * len(nums)<br>    dp[0] = nums[0]<br>    dp[1] = max(nums[0], nums[1])<br><br>    for i in range(2, len(nums)):<br>        dp[i] = max(nums[i] + dp[i-2], dp[i-1])<br><br>    return dp[-1]<br></pre><p>let nums = [2, 7, 9, 3, 1]</p><p><strong>Step-by-step filling:</strong></p><p>i = 2 (House value = 9)</p><ul><li>Rob → 9 + dp[0] = 9 + 2 = 11</li><li>Skip → dp[1] = 7</li></ul><p>👉 dp[2] = 11<br> Now dp → [2, 7, 11, 0, 0]</p><p>i = 3 (House value = 3)</p><ul><li>Rob → 3 + dp[1] = 3 + 7 = 10</li><li>Skip → dp[2] = 11</li></ul><p>👉 dp[3] = 11<br> Now dp → [2, 7, 11, 11, 0]</p><p>i = 4 (House value = 1)</p><ul><li>Rob → 1 + dp[2] = 1 + 11 = 12</li><li>Skip → dp[3] = 11</li></ul><p>👉 dp[4] = 12<br> Now dp → [2, 7, 11, 11, 12]</p><p>dp[-1] = last value in dp = <strong>12</strong>, which is maximum money you can steal.</p><h3>Conclusion</h3><p>Dynamic Programming may look intimidating at first, but once you understand its heart — <strong>breaking big problems into smaller subproblems and reusing results</strong> — everything becomes clearer. Whether you use <strong>Top-Down (Memoization)</strong> or <strong>Bottom-Up (Tabulation)</strong>, the goal is the same: avoid repeated work and think smarter.</p><p>Real-life situations like saving money, climbing stairs, choosing tasks, or even planning your day follow the same DP mindset. And once you learn to recognize patterns like <strong>overlapping subproblems</strong> and <strong>optimal choices</strong>, even medium or hard problems start to feel manageable.</p><p>Just remember:</p><ul><li>Start with the simplest base cases</li><li>Build up step by step</li><li>Reuse what you’ve already solved</li><li>Think of DP as solving today using yesterday’s work</li></ul><p>With practice, DP becomes less of a “scary topic” and more of a powerful skill in your toolkit. You’ve already taken the first big step — keep practicing, keep breaking things down, and DP will start making perfect sense!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=67c2f3e619c2" width="1" height="1" alt=""><hr><p><a href="https://python.plainenglish.io/lets-easily-learn-dp-67c2f3e619c2">Let’s easily learn DP</a> was originally published in <a href="https://python.plainenglish.io/">Python in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Kafka Installation and Functions]]></title>
            <link>https://blog.stackademic.com/kafka-installation-and-functions-b0a222b991b4?source=rss-bc8547726b7d------2</link>
            <guid isPermaLink="false">https://medium.com/p/b0a222b991b4</guid>
            <category><![CDATA[messaging]]></category>
            <category><![CDATA[kafka]]></category>
            <category><![CDATA[distributed-systems]]></category>
            <category><![CDATA[community]]></category>
            <category><![CDATA[data-science]]></category>
            <dc:creator><![CDATA[Kapil Bhardwaj]]></dc:creator>
            <pubDate>Thu, 08 May 2025 15:26:55 GMT</pubDate>
            <atom:updated>2025-05-08T15:26:55.366Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*tskTzlsdjUcrblVn" /><figcaption>Photo by <a href="https://unsplash.com/@imozart?utm_source=medium&amp;utm_medium=referral">Seongtaek Chee</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p><strong>In this tutorial, we’ll learn how to install Kafka and run our first Kafka program. So, let’s get started!</strong></p><h3>Kafka Installation</h3><p><strong>Prerequisites:</strong> Docker, Python</p><p>There are several ways to install Kafka, but I recommend using a Docker-based setup. It’s simpler, easier to manage, and more beginner-friendly.</p><p><strong>Step 1:</strong> create a docker-compose.yaml file inside your root project and paste below code:</p><pre>services:<br>  zookeeper:<br>    image: bitnami/zookeeper:3.8<br>    container_name: zookeeper<br>    ports:<br>      - &quot;2181:2181&quot;<br>    environment:<br>      - ALLOW_ANONYMOUS_LOGIN=yes<br><br>  kafka:<br>    image: bitnami/kafka:3.5<br>    container_name: kafka<br>    ports:<br>      - &quot;9092:9092&quot;<br>    environment:<br>      - KAFKA_BROKER_ID=1<br>      - KAFKA_CFG_ZOOKEEPER_CONNECT=zookeeper:2181<br>      - KAFKA_CFG_LISTENERS=PLAINTEXT://:9092<br>      - KAFKA_CFG_ADVERTISED_LISTENERS=PLAINTEXT://localhost:9092<br>      - ALLOW_PLAINTEXT_LISTENER=yes<br>      - KAFKA_CFG_AUTO_CREATE_TOPICS_ENABLE=true<br>    depends_on:<br>      - zookeeper</pre><h4>services:</h4><p>This is the top-level key that declares all the containers (services) Docker will run in this setup.</p><h4>zookeeper:</h4><p>This service starts a <strong>Zookeeper</strong> container. Kafka uses Zookeeper to manage broker metadata and distributed coordination (like topic configuration, partitions, leader election).</p><ul><li>image: bitnami/zookeeper:3.8: Uses Bitnami’s Zookeeper image, version 3.8.</li><li>container_name: zookeeper: The container will be named zookeeper for easier reference.</li><li>ports: &quot;2181:2181&quot;: Exposes Zookeeper&#39;s default port 2181 on your local machine.</li><li>environment:</li></ul><p>ALLOW_ANONYMOUS_LOGIN=yes: Allows Zookeeper to start without. authentication (suitable for local development).</p><h4>kafka:</h4><p>This service runs <strong>Kafka</strong>, configured to work with the Zookeeper container.</p><ul><li>image: bitnami/kafka:3.5: Uses Bitnami’s Kafka image, version 3.5.</li><li>container_name: kafka: Names the container kafka.</li><li>ports: &quot;9092:9092&quot;: Exposes Kafka’s port 9092 so you can connect from your host machine.</li><li>environment: – These environment variables configure how Kafka behaves:</li></ul><p>KAFKA_BROKER_ID=1: Unique ID for the Kafka broker in a cluster (just 1 broker here).</p><p>KAFKA_CFG_ZOOKEEPER_CONNECT=zookeeper:2181: Tells Kafka where to find Zookeeper. Since the service is named zookeeper, Docker’s internal DNS resolves this name correctly.</p><p>KAFKA_CFG_LISTENERS=PLAINTEXT://:9092: Kafka listens on port 9092 with PLAINTEXT (no encryption).</p><p>KAFKA_CFG_ADVERTISED_LISTENERS=PLAINTEXT://localhost:9092: Tells Kafka what address to advertise to clients (this is what consumers/producers will use to connect). Here it&#39;s localhost, meaning the host machine.</p><p>ALLOW_PLAINTEXT_LISTENER=yes: Bitnami safety flag to allow PLAINTEXT connections (not suitable for production, fine for dev).</p><p>KAFKA_CFG_AUTO_CREATE_TOPICS_ENABLE=true: Automatically creates a topic when a producer sends data to a topic that doesn’t exist.</p><p>depends_on: zookeeper: Ensures that Docker starts Zookeeper before Kafka, since Kafka needs it to initialise.</p><h3><strong>Start Kafka + Zookeeper</strong>:</h3><pre>docker-compose up -d</pre><p>this command will pull the kafka and zookepper image and build a container to use.</p><h3>Write your first producer function</h3><pre>from kafka import KafkaProducer<br><br>producer = KafkaProducer(bootstrap_servers=&#39;localhost:9092&#39;)  # ①<br><br>topic = &#39;my-topic&#39;  # ②<br><br>for i in range(10):  # ③<br>    message = f&quot;hello from kafka {i}&quot;  # ④<br>    producer.send(topic, value=message.encode(&#39;utf-8&#39;))  # ⑤<br><br>producer.flush()  # ⑥</pre><h4>① KafkaProducer(bootstrap_servers=&#39;localhost:9092&#39;)</h4><ul><li>This creates a <strong>Kafka producer client</strong>.</li><li>bootstrap_servers is the address of the Kafka broker (where it’s listening for clients).</li><li>&#39;localhost:9092&#39; assumes Kafka is running locally on port 9092.</li><li>You can also pass a list of brokers: [&#39;broker1:9092&#39;, &#39;broker2:9092&#39;].</li></ul><h4>② topic = &#39;my-topic&#39;</h4><ul><li>The name of the Kafka <strong>topic</strong> where messages will be sent.</li><li>If the topic doesn’t exist and <strong>auto-creation is disabled</strong>, this will silently fail or raise an error depending on the broker config.</li></ul><h4>③ for i in range(10):</h4><ul><li>A loop that runs <strong>10 times</strong>, generating 10 messages.</li></ul><h4>④ message = f&quot;hello from kafka {i}&quot;</h4><ul><li>Builds a message string using f-string formatting.</li><li>Example output: &quot;hello from kafka 0&quot;, &quot;hello from kafka 1&quot;, etc.</li></ul><h4>⑤ producer.send(topic, value=message.encode(&#39;utf-8&#39;))</h4><ul><li>Sends the message to the Kafka topic.</li><li>Kafka only accepts <strong>byte strings</strong>, so you must .encode(&#39;utf-8&#39;) the message.</li><li>send() is asynchronous — it queues the message to be sent.</li></ul><h4>⑥ producer.flush()</h4><ul><li>Waits for all messages in the queue to be sent to Kafka before the program exits.</li><li>Without flush(), messages might be lost if the script ends before the producer finishes sending.</li></ul><h3>Write your first Consumer function</h3><pre>from kafka import KafkaConsumer<br><br>consumer = KafkaConsumer(<br>    &#39;my-topic&#39;,<br>    bootstrap_servers=&#39;localhost:9092&#39;,<br>    auto_offset_reset=&#39;earliest&#39;,<br>    group_id=&#39;test-group&#39;<br>)<br><br>print(&quot;Listening for messages...&quot;)<br>for message in consumer:<br>    print(f&quot;Received: {message.value.decode(&#39;utf-8&#39;)}&quot;)</pre><p>The code starts by importing KafkaConsumer from the kafka library, which allows you to consume messages from a Kafka topic. A KafkaConsumer instance is then created with several key parameters. First, &#39;my-topic&#39; is the name of the Kafka topic that the consumer will subscribe to. This should be the same topic that your Kafka producer is sending messages to.</p><p>The bootstrap_servers=&#39;localhost:9092&#39; parameter tells the consumer where the Kafka broker is located — in this case, it’s assumed to be running on the local machine on port 9092. The auto_offset_reset=&#39;earliest&#39; parameter is important because it defines what the consumer should do when there is no previously stored offset for the group. By setting this to &#39;earliest&#39;, the consumer will start reading messages from the beginning of the topic. If this were set to &#39;latest&#39;, it would only consume new messages arriving after the consumer starts.</p><p>The group_id=&#39;test-group&#39; parameter defines the consumer group. Kafka tracks the read offset per topic per group ID, so if another consumer in the same group has already processed messages, this consumer will pick up where the group left off. If no such offset exists, the auto_offset_reset setting determines the behavior.</p><p>The script prints “Listening for messages…” to indicate that it’s now waiting for messages. Then, using a for loop, it begins consuming messages from the Kafka topic. The loop is blocking and runs indefinitely, receiving messages as they arrive. Each message object includes a value attribute, which contains the message content in bytes. This value is decoded using UTF-8 to convert it into a human-readable string, which is then printed with the prefix “Received:”.</p><h3>Conclusion</h3><p>In conclusion, this Kafka consumer script is a simple yet effective way to read messages from a Kafka topic. It connects to a local Kafka broker, joins a consumer group, and listens indefinitely for new or existing messages depending on the offset settings. This setup is ideal for basic use cases like debugging, testing message flow, or building real-time data processing applications. For production systems, you may want to extend it with logging, error handling, graceful shutdown logic, or message deserialization based on your application’s needs.</p><h3>Thank you for being a part of the community</h3><p><em>Before you go:</em></p><ul><li>Be sure to <strong>clap</strong> and <strong>follow</strong> the writer ️👏<strong>️️</strong></li><li>Follow us: <a href="https://x.com/inPlainEngHQ"><strong>X</strong></a> | <a href="https://www.linkedin.com/company/inplainenglish/"><strong>LinkedIn</strong></a> | <a href="https://www.youtube.com/@InPlainEnglish"><strong>YouTube</strong></a> | <a href="https://newsletter.plainenglish.io/"><strong>Newsletter</strong></a> | <a href="https://open.spotify.com/show/7qxylRWKhvZwMz2WuEoua0"><strong>Podcast</strong></a> | <a href="https://differ.blog/inplainenglish"><strong>Differ</strong></a> | <a href="https://twitch.tv/inplainenglish"><strong>Twitch</strong></a></li><li><a href="https://differ.blog/"><strong>Start your own free AI-powered blog on Differ</strong></a> 🚀</li><li><a href="https://discord.gg/in-plain-english-709094664682340443"><strong>Join our content creators community on Discord</strong></a> 🧑🏻‍💻</li><li>For more content, visit <a href="https://plainenglish.io/"><strong>plainenglish.io</strong></a> + <a href="https://stackademic.com/"><strong>stackademic.com</strong></a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b0a222b991b4" width="1" height="1" alt=""><hr><p><a href="https://blog.stackademic.com/kafka-installation-and-functions-b0a222b991b4">Kafka Installation and Functions</a> was originally published in <a href="https://blog.stackademic.com/">Stackademic</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Kafka For Beginners]]></title>
            <link>https://blog.stackademic.com/kafka-for-beginners-483bcbda7c4e?source=rss-bc8547726b7d------2</link>
            <guid isPermaLink="false">https://medium.com/p/483bcbda7c4e</guid>
            <category><![CDATA[systems-thinking]]></category>
            <category><![CDATA[kafka]]></category>
            <category><![CDATA[networking]]></category>
            <category><![CDATA[interview]]></category>
            <category><![CDATA[computer-science]]></category>
            <dc:creator><![CDATA[Kapil Bhardwaj]]></dc:creator>
            <pubDate>Thu, 08 May 2025 15:26:49 GMT</pubDate>
            <atom:updated>2025-05-08T15:26:49.315Z</atom:updated>
            <content:encoded><![CDATA[<p>In this guide, we’ll explore the fundamentals of Kafka and work toward building a stronger understanding of the topic. Let’s get started</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/625/0*oH7QiZoaTyUGgDZD" /></figure><p>“Let’s begin the tutorial with two of the most commonly asked and fundamental questions: ‘<strong>What is Kafka?</strong>’ and ‘<strong>Why do we need Kafka?</strong>’</p><h3>What is Kafka?</h3><p>Think of <strong>Kafka</strong> as a <strong>middleman between your microservices</strong>. It receives input or data from various components and manages that data efficiently. This ensures that if one microservice fails, the others can continue operating independently without interruption.</p><p>Let’s go deeper with a real-world example — imagine a <strong>food delivery app</strong>.</p><p>Whenever a user places an order, a <strong>chain of microservices</strong> is triggered. For instance:</p><ul><li>The <strong>payment service</strong> processes the transaction.</li><li>The <strong>invoice management</strong> service generates the receipt.</li><li><strong>Bank alerts</strong> are sent to the user.</li><li>The <strong>restaurant</strong> is notified that the order is paid.</li><li><strong>Real-time tracking</strong> is initialized.</li><li>Order data is sent to the <strong>dashboard/analytics</strong> system.</li></ul><p>All these services are interconnected and often depend on each other.</p><p>Now imagine:</p><ul><li>One of the services <strong>fails</strong> (e.g., the invoice service goes down).</li><li>Or your user base <strong>grows rapidly</strong>, making your synchronous architecture <strong>struggle under load</strong>.</li></ul><p>In either case, your system could break or become unstable.</p><p>This is where <strong>Apache Kafka</strong> comes in.</p><p>Kafka helps by <strong>decoupling</strong> these microservices. Instead of calling each service directly, they all communicate through Kafka. If one service fails or slows down, Kafka <strong>stores the events</strong> and delivers them when the service is back or ready, ensuring <strong>fault tolerance, scalability, and real-time processing</strong>.</p><h3>Why Use Kafka?</h3><p>Let’s take the same example again — imagine a food delivery app like Zomato. When you place an order, you can track its status, which updates every 10 seconds. At the same time, thousands of other orders are also being processed. It’s not feasible for the app to create a separate notification system for each user.</p><p>Instead, they use a centralized system that collects real-time updates from all delivery agents and sends relevant information only to the users who need it. This approach efficiently manages data flow and reduces system load.</p><p>For scenarios like this, we use Kafka.</p><h3>Kafka Architecture</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*jt9jDFO2vR1Sig56" /></figure><h4>1. Producer</h4><p>A <strong>producer</strong> is the entry point of data into Kafka. In the Zomato example, the mobile app used by delivery riders acts as the producer. Every time a rider’s location updates or an order changes status (like “Order Picked”, “Out for Delivery”, “Delivered”), that event is captured by the app and sent to Kafka. Producers send this data to a specific <strong>topic</strong> within Kafka.</p><p>Now, the producer doesn’t just push data randomly — it must <strong>know which topic to send data to</strong>. Think of a topic as a category or a label for data. For instance, a topic could be named order_tracking_updates. So, the producer relies on the Kafka broker being available and the topic existing or being automatically created. The producer may also be configured to assign the data to a specific <strong>partition</strong> within that topic, either randomly or using some logic (like order ID as a key).</p><h4>2. Topic</h4><p>A <strong>topic</strong> is a logical grouping of messages. All producers send data to one or more topics. In our Zomato example, the order_tracking_updates topic might be where all location and status updates are streamed. It acts like a message board where messages are posted and later read by consumers.</p><p>A topic on its own doesn’t do anything unless it’s <strong>partitioned</strong>. Kafka automatically splits a topic into <strong>partitions</strong> for performance and scalability. This brings us to the next component.</p><h4>3. Partition</h4><p>Each topic in Kafka is divided into one or more <strong>partitions</strong>. A partition is essentially a log where records are appended in order. This partitioning is what allows Kafka to scale horizontally and support high throughput. For instance, if you have 1,000 riders sending updates every few seconds, and the topic has 5 partitions, Kafka can distribute the load across these partitions evenly or based on a key like order_id.</p><p>Partitions also introduce ordering at the level of each partition. This means for a given order, if all updates go to the same partition, Kafka guarantees that they are received in the order they were sent. This is critical for a food delivery app where status updates like “Picked up” → “Out for delivery” → “Delivered” must arrive in order.</p><p>But partitions can’t exist without a <strong>broker</strong> to host them.</p><h4>4. Broker</h4><p>A <strong>Kafka broker</strong> is a Kafka server that stores data and serves read and write requests from producers and consumers. Each broker can host multiple topic partitions. In a Kafka <strong>cluster</strong>, there are multiple brokers, and partitions are distributed among them for fault tolerance and load balancing.</p><p>So, when a producer sends a message to a topic, Kafka (via the broker) assigns it to a partition. That partition physically resides on one or more brokers, depending on replication settings. This way, even if one broker goes down, the partition is still available from another broker.</p><p>Producers depend on brokers to <strong>acknowledge</strong> that their data was received and written successfully. And consumers, as we’ll see next, rely on brokers to fetch this data.</p><h4>5. Consumer</h4><p>A <strong>consumer</strong> reads messages from a Kafka topic. In our Zomato scenario, the user’s mobile app is the consumer. When the user places an order, the app subscribes to updates related to that order. The app doesn’t want all messages in the topic — only those relevant to their specific order.</p><p>This is where <strong>Kafka’s design shines</strong>. Kafka doesn’t push messages to consumers. Instead, consumers <strong>pull</strong> data at their own pace and Kafka keeps the data available for a configurable retention period (like 7 days). This means if the consumer app crashes or the phone goes offline, it can still resume reading messages from where it left off.</p><p>To manage load and coordination among consumers, Kafka introduces something called a <strong>consumer group</strong>.</p><h4>6. Consumer Group</h4><p>A <strong>consumer group</strong> is a set of consumers that work together to consume messages from a topic. Kafka ensures that each partition is consumed by only one consumer in a group at a time. So, if you have a topic with 3 partitions and a consumer group with 3 consumers, each one handles a different partition, reading messages independently but in parallel.</p><p>In a real-world app like Zomato, backend services might have a group of consumers reading order updates to trigger notifications, update the database, and alert restaurants — all in parallel using separate partitions.</p><p>This design ensures scalability and fault tolerance. If one consumer fails, another in the group will take over its assigned partition. The consumer group depends on Kafka brokers to coordinate partition assignments and to maintain <strong>offsets</strong> (i.e., how far a consumer has read).</p><h4>7. ZooKeeper or KRaft</h4><p>Traditionally, Kafka used <strong>ZooKeeper</strong> for managing cluster metadata — keeping track of brokers, leader election for partitions, and access control. However, newer versions of Kafka now support a built-in consensus mechanism called <strong>KRaft</strong> (Kafka Raft). This removes the need for ZooKeeper altogether.</p><p>Regardless of which is used, the metadata system plays a crucial behind-the-scenes role in helping producers, brokers, and consumers discover each other, manage topics and partitions, and recover from failures.</p><h4>Dependency Chain Summary (with Zomato analogy):</h4><ol><li><strong>Producer (Delivery app)</strong> sends updates → <strong>needs Kafka broker and topic</strong> to exist.</li><li><strong>Kafka broker</strong> receives and stores the message → <strong>manages partitions within the topic</strong>.</li><li><strong>Partitions</strong> divide the topic’s data → allow multiple producers and consumers to work efficiently.</li><li><strong>Consumers (User’s app)</strong> read from specific partitions → must coordinate via <strong>consumer groups</strong>.</li><li><strong>Broker and metadata system (ZooKeeper/KRaft)</strong> maintain cluster health and data availability.</li></ol><p>This is how Kafka manages real-time, scalable, and fault-tolerant data pipelines. In the Zomato-style case, Kafka makes sure millions of order updates can be produced and consumed efficiently — without separate systems for each user or order.</p><h3>Can Kafka Be Used as a Database?</h3><p>While Kafka is primarily a distributed streaming platform, it can behave <em>like</em> a database in certain scenarios — but it is not a traditional database. Kafka stores messages in durable logs (called topics), where data is persisted for a configurable period (e.g., 7 days, or even indefinitely). This means consumers can re-read the same data multiple times, a behavior not typically possible with message queues like RabbitMQ, where data is deleted after consumption. For example, in a food delivery app like Zomato, every order status change (e.g., “Order Placed”, “Being Prepared”, “Out for Delivery”) can be pushed into a Kafka topic. Any service — like the user tracking screen — can subscribe to this topic and fetch the latest status updates, even replaying past ones if needed. This makes Kafka valuable for systems that need an audit trail or want to rebuild state from events. However, unlike real databases, Kafka lacks features like indexing, rich querying, updates, and random access. So while Kafka can store and replay data reliably, it’s best seen as a durable event log rather than a substitute for a traditional relational or NoSQL database.</p><h3>Conclusion</h3><p>Kafka is much more than just a message queue — it’s a powerful, distributed event streaming platform built to handle real-time data at scale. In this beginner’s introduction, we explored its core components like producers, consumers, topics, brokers, and how Kafka differs from traditional message brokers like RabbitMQ. With its durability, high throughput, and ability to replay events, Kafka forms the backbone of many modern data-driven applications — from order tracking in food delivery apps to log aggregation and real-time analytics. As you get comfortable with the basics and start experimenting hands-on, you’ll begin to appreciate Kafka’s full potential. In the next article, we’ll dive into <strong>intermediate topics</strong> such as consumer groups, offset management, message retention, and integrating Kafka into real-world systems. Stay tuned!</p><h3>Thank you for being a part of the community</h3><p><em>Before you go:</em></p><ul><li>Be sure to <strong>clap</strong> and <strong>follow</strong> the writer ️👏<strong>️️</strong></li><li>Follow us: <a href="https://x.com/inPlainEngHQ"><strong>X</strong></a> | <a href="https://www.linkedin.com/company/inplainenglish/"><strong>LinkedIn</strong></a> | <a href="https://www.youtube.com/@InPlainEnglish"><strong>YouTube</strong></a> | <a href="https://newsletter.plainenglish.io/"><strong>Newsletter</strong></a> | <a href="https://open.spotify.com/show/7qxylRWKhvZwMz2WuEoua0"><strong>Podcast</strong></a> | <a href="https://differ.blog/inplainenglish"><strong>Differ</strong></a> | <a href="https://twitch.tv/inplainenglish"><strong>Twitch</strong></a></li><li><a href="https://differ.blog/"><strong>Start your own free AI-powered blog on Differ</strong></a> 🚀</li><li><a href="https://discord.gg/in-plain-english-709094664682340443"><strong>Join our content creators community on Discord</strong></a> 🧑🏻‍💻</li><li>For more content, visit <a href="https://plainenglish.io/"><strong>plainenglish.io</strong></a> + <a href="https://stackademic.com/"><strong>stackademic.com</strong></a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=483bcbda7c4e" width="1" height="1" alt=""><hr><p><a href="https://blog.stackademic.com/kafka-for-beginners-483bcbda7c4e">Kafka For Beginners</a> was originally published in <a href="https://blog.stackademic.com/">Stackademic</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[RAG-Retrieval-Augmented Generation]]></title>
            <link>https://python.plainenglish.io/rag-retrieval-augmented-generation-7be63e906115?source=rss-bc8547726b7d------2</link>
            <guid isPermaLink="false">https://medium.com/p/7be63e906115</guid>
            <dc:creator><![CDATA[Kapil Bhardwaj]]></dc:creator>
            <pubDate>Tue, 22 Apr 2025 04:06:37 GMT</pubDate>
            <atom:updated>2025-04-28T00:43:05.503Z</atom:updated>
            <content:encoded><![CDATA[<p>Powerful technique in modern AI that <strong>combines retrieval (search) with generation (text output)</strong> to answer questions more accurately and with better context.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*OeehDy39J4zqYWu6" /><figcaption>Photo by <a href="https://unsplash.com/@steve_j?utm_source=medium&amp;utm_medium=referral">Steve Johnson</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>What is RAG?</h3><p><strong>RAG</strong> stands for <strong>Retrieval-Augmented Generation</strong>. It is a technique that combines two components:</p><ol><li><strong>Retriever</strong> — fetches relevant external information based on the user query.</li><li><strong>Generator (LLM)</strong> — uses the retrieved content and the user query to generate a grounded, accurate response.</li></ol><p>It’s an AI technique where a <strong>language model (like ChatGPT or Gemini)</strong> is <strong>“augmented” with real-world knowledge</strong> by retrieving relevant documents <strong>before generating a response</strong>.</p><h3>Why do we need it?</h3><blockquote><em>Language models are mostly trained on static datasets, which means they often lack access to specific, updated, or domain-specific information. This limitation can lead to hallucinations or incorrect predictions. </em><strong><em>RAG (Retrieval-Augmented Generation)</em></strong><em> solves this by providing the model with real, relevant information from external sources before generating a response.</em></blockquote><h3>RAG Architecture</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/736/0*YYfDGaOhY5mIOvY7" /><figcaption><a href="https://docs.databricks.com/aws/en/assets/images/rag-agent-workflow-3b3c6a8f86f0f8b8e38f5196cbe8a8b1.png"><strong><em>RAG Architecture</em></strong></a></figcaption></figure><h4>1. Query Preprocessing</h4><p>This is the initial stage where the system receives the user’s question. The orchestration framework (responsible for data cleaning ) may clean the text, remove stopwords, or normalize certain parts of the query. This step prepares the input so it can be processed efficiently downstream.</p><h4>2. Query Embedding</h4><p>The cleaned query is passed to an embedding model. This model converts the query into a vector representation — a numerical format that captures the meaning or semantic intent of the query. This format is necessary to perform similarity searches in the vector database.</p><h4>3. Retrieval</h4><p>The vectorized query is used to perform a similarity search in a vector database. The database contains vector representations of documents, passages, or chunks of content. The search returns a set of the most relevant documents or content pieces based on how similar they are to the query.</p><p>These documents can come from sources like knowledge bases, internal docs, customer support logs, or FAQs.</p><h4>4. Augmentation</h4><p>The retrieved documents are combined with the original user query to form an “augmented query.” This enriched input gives the language model both the user’s intent and the relevant background information it needs to produce a meaningful and accurate response.</p><p>This is the core of RAG: instead of answering blindly, the model now has context to ground its output.</p><h4>5. Generation</h4><p>The augmented query is sent to the large language model (LLM). The LLM uses this combined information to generate a response. Since it has access to both the user’s question and the related knowledge, its output tends to be more accurate, contextual, and specific.</p><p>This also significantly reduces hallucinations.</p><h4>6. Post-processing</h4><p>Once the response is generated, the orchestration framework may further clean, reformat, or validate it. This step might include removing unwanted content, formatting the output, or even ranking multiple responses. Finally, the response is returned to the user.</p><h3>When to Use RAG?</h3><p>Use RAG when:</p><ul><li>You want your LLM to answer questions based on <strong>up-to-date or proprietary knowledge</strong> (docs, product catalogs, support articles, etc.).</li><li>You want to <strong>reduce hallucinations</strong> from the model by grounding its responses in real data.</li><li>You don’t want to <strong>fine-tune a large model</strong> but still want domain-specific answers.</li></ul><h3>How to Set Up a Simple RAG System in Python</h3><p>using langchain here as it is beginner friendly</p><h4>Prerequisites:</h4><pre>pip3 install langchain openai faiss-cpu</pre><h4>Demo For Full RAG Pipeline (Gemini + LangChain + FAISS)</h4><pre># Imports<br>from langchain.document_loaders import TextLoader<br>from langchain.text_splitter import CharacterTextSplitter<br>from langchain.embeddings import GoogleGenerativeAIEmbeddings<br>from langchain.vectorstores import FAISS<br>from langchain.chat_models import ChatGoogleGenerativeAI<br>from langchain.chains import RetrievalQA<br><br># ----------------- Step 1: Load Data -----------------<br>loader = TextLoader(&quot;your_docs.txt&quot;)  # Replace with your file<br>docs = loader.load()<br><br># ----------------- Step 2: Split Data -----------------<br>splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=100)<br>documents = splitter.split_documents(docs)<br><br># ----------------- Step 3: Embeddings &amp; Vector DB -----------------<br>embeddings = GoogleGenerativeAIEmbeddings(model=&quot;models/embedding-001&quot;)<br>db = FAISS.from_documents(documents, embeddings)<br><br># ----------------- Step 4: Setup Retriever -----------------<br>retriever = db.as_retriever()<br><br># ----------------- Step 5: Gemini LLM + RAG Chain -----------------<br>qa_chain = RetrievalQA.from_chain_type(<br>    llm=ChatGoogleGenerativeAI(<br>        model=&quot;gemini-pro&quot;,<br>        temperature=0.3<br>    ),<br>    chain_type=&quot;stuff&quot;,<br>    retriever=retriever<br>)<br><br># ----------------- Step 6: Ask a Question -----------------<br>query = &quot;What is the refund policy?&quot;<br>response = qa_chain.run(query)<br><br>print(&quot;Question:&quot;, query)<br>print(&quot;Answer:&quot;, response)</pre><h3>How the LLM Works in a RAG Setup</h3><p>A <strong>Language Model (LLM)</strong> like Gemini or GPT is very powerful — but it has <strong>two limitations</strong>:</p><ol><li><strong>Trained on static data</strong> — it doesn’t know your custom documents.</li><li><strong>Can hallucinate</strong> — it may make up facts if it doesn’t have context.</li></ol><h4>Retrieval-Augmented Generation (RAG) fixes this:</h4><p><strong>RAG = Search + Generate</strong><em>It </em><strong><em>retrieves relevant data</em></strong><em> from your custom database and </em><strong><em>feeds it to the LLM</em></strong><em> so the answer is </em><strong><em>grounded in facts</em></strong><em>, not guesses.</em></p><h4>How it flows:</h4><pre>[User Question]<br>      ⬇<br>[Retriever]  ←— searches in vector DB<br>      ⬇<br>[Relevant chunks (docs)]<br>      ⬇<br>[LLM (Gemini)] ←— uses context + prompt<br>      ⬇<br>[Final Answer]</pre><h3>2. DB Architecture (Vector Store)</h3><p>In RAG, the <strong>database is not a traditional SQL or NoSQL DB</strong> — it’s a <strong>vector store</strong>.</p><h4>What’s stored in the vector DB?</h4><p>Item | Description</p><p>Document Text | Your actual content (chunked)</p><p>Embeddings | Numerical representation of meaning</p><p>Metadata (optional) | Page number, tags, file source, etc.</p><p>You’re using <strong>FAISS</strong> for this. Here’s what happens:</p><ol><li><strong>Documents are split</strong> into smaller chunks.</li><li>Each chunk is <strong>converted into an embedding</strong> (like a fingerprint of the meaning).</li><li>Those embeddings are stored in FAISS, allowing <strong>fast similarity search</strong>.</li></ol><h3>Conclusion</h3><p>Retrieval-Augmented Generation (RAG) represents a powerful evolution in how we interact with language models. By combining the generative abilities of LLMs with the factual grounding of vector-based retrieval, RAG offers more accurate, context-aware, and reliable responses. Vector databases play a crucial role in this architecture, enabling fast and meaningful search through embedded knowledge.</p><p>As the need for dynamic, real-time, and domain-specific AI systems grows, RAG stands out as a scalable and intelligent solution — ideal for chatbots, customer support, internal knowledge assistants, and more. With tools like LangChain, FAISS, and Gemini, developers can easily build their own RAG pipelines, bringing the best of search and generation into one seamless system.</p><p>Whether you’re just starting out or building production-grade AI apps, RAG is a must-have in your modern AI toolkit.</p><h3>Thank you for being a part of the community</h3><p><em>Before you go:</em></p><ul><li>Be sure to <strong>clap</strong> and <strong>follow</strong> the writer ️👏<strong>️️</strong></li><li>Follow us: <a href="https://x.com/inPlainEngHQ"><strong>X</strong></a> | <a href="https://www.linkedin.com/company/inplainenglish/"><strong>LinkedIn</strong></a> | <a href="https://www.youtube.com/@InPlainEnglish"><strong>YouTube</strong></a> | <a href="https://newsletter.plainenglish.io/"><strong>Newsletter</strong></a> | <a href="https://open.spotify.com/show/7qxylRWKhvZwMz2WuEoua0"><strong>Podcast</strong></a> | <a href="https://differ.blog/inplainenglish"><strong>Differ</strong></a> | <a href="https://twitch.tv/inplainenglish"><strong>Twitch</strong></a></li><li><a href="https://cofeed.app/"><strong>Check out CoFeed, the smart way to stay up-to-date with the latest in tech</strong></a> <strong>🧪</strong></li><li><a href="https://differ.blog/"><strong>Start your own free AI-powered blog on Differ</strong></a> 🚀</li><li><a href="https://discord.gg/in-plain-english-709094664682340443"><strong>Join our content creators community on Discord</strong></a> 🧑🏻‍💻</li><li>For more content, visit <a href="https://plainenglish.io/"><strong>plainenglish.io</strong></a> + <a href="https://stackademic.com/"><strong>stackademic.com</strong></a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7be63e906115" width="1" height="1" alt=""><hr><p><a href="https://python.plainenglish.io/rag-retrieval-augmented-generation-7be63e906115">RAG-Retrieval-Augmented Generation</a> was originally published in <a href="https://python.plainenglish.io/">Python in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Greedy Algorithm]]></title>
            <link>https://blog.stackademic.com/greedy-algorithm-54758568513e?source=rss-bc8547726b7d------2</link>
            <guid isPermaLink="false">https://medium.com/p/54758568513e</guid>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[education]]></category>
            <category><![CDATA[students]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Kapil Bhardwaj]]></dc:creator>
            <pubDate>Thu, 31 Oct 2024 08:10:59 GMT</pubDate>
            <atom:updated>2024-10-31T08:10:59.357Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>Learn Greedy Algorithm to boost your coding abilities</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/626/0*SQSgjAo81uG-jpwH.jpg" /></figure><h3>Definition</h3><p>A <em>greedy algorithm</em> is a method used in problem-solving where, at each step, a locally optimal choice is made with the hope that <strong><em>these choices will lead to a globally optimal solution. </em></strong>The algorithm continuously selects the best option at each decision point without reconsidering choices once they’re made.</p><h3>Core Idea</h3><p>The fundamental principle behind a greedy algorithm is straightforward: at each stage, select the option that seems best at the moment, or “greediest” in terms of the desired outcome. By making these locally optimal choices, the algorithm aims to construct an overall solution that is as close as possible to the best possible solution for the problem as a whole. However, <strong><em>greedy algorithms are effective only when the problem has specific properties that guarantee that local optima lead to a global optimum.</em></strong></p><p>For example, in a task scheduling problem, a greedy approach might prioritize tasks with the earliest end time to maximize the number of non-overlapping tasks. In cases where local decisions align with an optimal global solution, the greedy algorithm is highly efficient.</p><h3>Common Applications</h3><p>Greedy algorithms are widely used in various domains, especially in scenarios requiring optimal choices under constraints. Some of the common areas include:</p><ul><li><strong>Scheduling Problems</strong>: Selecting tasks or jobs based on certain constraints, such as deadlines or times, to maximize resource utilization or minimize delays.</li><li><strong>Graph Theory</strong>: Many graph-related problems, such as finding the Minimum Spanning Tree (using Kruskal’s or Prim’s algorithm) or the shortest path in a weighted graph (Dijkstra’s algorithm), employ greedy methods.</li><li><strong>Optimization Problems</strong>: Problems such as the coin change problem, knapsack problem, and activity selection problem are well-suited to greedy strategies, where the aim is to maximize or minimize a certain parameter under constraints.</li></ul><p>The effectiveness of greedy algorithms often depends on the problem’s structure, as<strong><em> not all problems can be optimally solved with a greedy approach. </em></strong>In cases where they are effective, greedy algorithms provide a highly efficient solution with minimal computation compared to more exhaustive approaches.</p><h3>Key Characteristics of Greedy Algorithms</h3><h4>Greedy Choice Property</h4><p>The <em>greedy choice property</em> is a defining characteristic of greedy algorithms. This property states that a global optimum can be reached by making a series of locally optimal choices — each choice at each step is the one that seems the best according to some criterion, without reconsidering previous choices.</p><p>For example, in the <em>activity selection problem</em>, where we want to maximize the number of non-overlapping activities in a schedule, the greedy approach chooses the activity with the earliest end time at each step. This local optimum (choosing the earliest ending activity) turns out to be part of the globally optimal solution, ensuring we maximize the number of activities without conflicts.</p><h4>Optimal Substructure</h4><p><em>Optimal substructure</em> is another crucial property for a problem to be solvable by a greedy algorithm. This means that an optimal solution to the entire problem can be constructed from optimal solutions to its subproblems.</p><p>For instance, in the <em>fractional knapsack problem</em>, the solution involves taking items (or parts of items) that provide the highest value per weight. By solving subproblems — selecting parts of items one by one in a locally optimal way — the greedy algorithm arrives at a globally optimal solution, filling the knapsack to maximize value under the weight limit.</p><h4>Greedy vs. Dynamic Programming</h4><p>Both greedy algorithms and dynamic programming rely on the concept of optimal substructure, but they differ fundamentally in approach:</p><ul><li><strong>Greedy Algorithms</strong>: Greedy algorithms make a single pass through the problem, making the best choice at each step. They are simpler and often more efficient in terms of time and space complexity but work only when the greedy choice property holds.</li><li><strong>Dynamic Programming</strong>: Dynamic programming considers all possible choices and breaks the problem into overlapping subproblems, which are solved and stored to avoid redundant calculations. Dynamic programming is used when there are multiple choices to evaluate, and we need to account for future consequences of each choice (e.g., problems where earlier choices affect later outcomes).</li></ul><p><strong>Examples of When to Use Each Approach</strong>:</p><ul><li><strong>Greedy Algorithm Example</strong>: <em>Coin Change Problem</em> (when using a limited coin denomination system): Choosing the largest coin value possible at each step minimizes the total number of coins.</li><li><strong>Dynamic Programming Example</strong>: <em>Fibonacci Sequence</em> or <em>0/1 Knapsack Problem</em>: These problems require evaluating multiple subproblems, where each choice affects future steps, making dynamic programming a more suitable solution.</li></ul><h3>Steps to Design a Greedy Algorithm</h3><p>Designing a greedy algorithm involves carefully analyzing the problem to ensure that a greedy approach will yield an optimal solution. Here are the steps to follow:</p><h4>Step 1: Identify the Problem Constraints and Requirements</h4><ul><li>Start by thoroughly understanding the problem, its constraints, and what constitutes an optimal solution. Identify all relevant parameters, such as limits on input size, specific conditions, and any objective function that needs to be maximized or minimized.</li><li>Example: In the <em>coin change problem</em>, the objective is to use the fewest coins to reach a target amount, given specific coin denominations. Constraints include the available denominations and the target amount.</li></ul><h4>Step 2: Determine if the Problem Exhibits the Greedy Choice Property</h4><ul><li>Check if making a series of locally optimal choices will lead to a globally optimal solution. This involves confirming that each choice made along the way contributes directly to an optimal solution, without needing to reconsider previous decisions.</li><li>Example: In the <em>activity selection problem</em>, choosing the activity with the earliest end time satisfies the greedy choice property because it maximizes the number of non-overlapping activities.</li></ul><h4>Step 3: Develop the Locally Optimal Strategy</h4><ul><li>Formulate a strategy that makes the “greedy” choice at each step. This strategy should consistently select the best local option based on the problem’s criteria.</li><li>Example: For the <em>fractional knapsack problem</em>, the greedy strategy is to choose items based on the highest value-to-weight ratio at each step, adding as much of the item as possible until the knapsack reaches its weight limit.</li></ul><h4>Step 4: Prove that the Greedy Choice Leads to an Optimal Solution</h4><ul><li>This is often the most challenging step. Prove that the greedy approach yields an optimal solution for the entire problem by showing that local optimality implies global optimality.</li><li>Mathematical induction or proof by contradiction can sometimes be useful to demonstrate the optimality of the greedy choice.</li><li>Example: For the <em>minimum spanning tree (MST)</em> problem, we can prove that using algorithms like Kruskal’s or Prim’s, which make locally optimal choices to add edges with the least weight, will always yield an MST.</li></ul><h4>Step 5: Implement and Test the Solution</h4><ul><li>Implement the greedy algorithm and thoroughly test it with various inputs, including edge cases, to ensure it performs correctly and optimally within the problem’s constraints.</li><li>Analyze the time and space complexity to ensure that it’s efficient enough for large inputs.</li><li>Example: For the <em>coin change problem</em>, implement the greedy algorithm and test it with different target amounts and coin denominations to verify it minimizes the coin count.</li></ul><h3>Examples of Greedy Algorithms</h3><p>Let’s explore some classic problems where the greedy approach is effective.</p><h4>Example 1: Coin Change Problem</h4><p><strong>Problem Statement</strong>: Given a set of coin denominations and a target amount, the goal is to find the minimum number of coins needed to make up that amount.</p><p><strong>Greedy Solution</strong>:</p><ul><li>Sort the coins in descending order by denomination.</li><li>Start with the largest coin and use as many of it as possible without exceeding the target amount.</li><li>Subtract the value of these coins from the target amount.</li><li>Move to the next largest denomination and repeat until the target amount is reached.</li></ul><p><strong>Step-by-Step Breakdown</strong>:</p><p>Suppose the target amount is 67, and the available denominations are [25, 10, 5, 1].</p><p>Start with the largest coin, 25, using 2 coins to get as close to 67 as possible (50).</p><p>The remaining amount is 67–50 = 17.</p><p>Move to the next coin, 10, and use 1 coin (total is now 60).</p><p>The remaining amount is 17–10 = 7.</p><p>Use 1 coin of 5 (total is now 65), leaving 2.</p><p>Use 2 coins of 1 to reach exactly 67.</p><p><strong>Result</strong>: The minimum number of coins required is 2 (25) + 1 (10) + 1 (5) + 2 (1) = 6 coins.</p><blockquote><em>Note: The greedy approach works well if the denominations allow a greedy solution (such as common U.S. coins). However, it may not always yield the optimal solution with arbitrary coin systems.</em></blockquote><h4>Example 2: Fractional Knapsack Problem</h4><p><strong>Problem Statement</strong>: Given a set of items, each with a weight and a value, and a knapsack with a weight limit, the objective is to maximize the total value in the knapsack. Unlike the 0/1 knapsack problem, the fractional knapsack problem allows taking fractional parts of items.</p><p><strong>Greedy Solution</strong>:</p><ul><li>Sort items by their value-to-weight ratio in descending order.</li><li>Start with the item with the highest value-to-weight ratio, adding as much as possible to the knapsack until reaching the weight limit.</li><li>If the weight of an item exceeds the remaining weight capacity, add a fraction of it instead.</li></ul><p><strong>Step-by-Step Breakdown</strong>:</p><p>Suppose the knapsack capacity is 50 units, with three items having the following properties:</p><ul><li>Item A: weight = 10, value = 60, ratio = 6</li><li>Item B: weight = 20, value = 100, ratio = 5</li><li>Item C: weight = 30, value = 120, ratio = 4</li></ul><p>Sort items by value-to-weight ratio: A &gt; B &gt; C.</p><p>Add Item A entirely (10 units, 60 value).</p><p>Add Item B entirely (20 units, 100 value).</p><p>Add as much of Item C as possible with remaining 20 units of capacity.</p><p>Since Item C’s total weight is 30, take 20/30 of Item C, which gives a value of 120×2030=80120 \times \frac{20}{30} = 80120×3020​=80.</p><p><strong>Result</strong>: The maximum value in the knapsack is 60 (A) + 100 (B) + 80 © = 240.</p><h4>Example 3: Activity Selection Problem</h4><p><strong>Problem Statement</strong>: Given a set of activities, each with a start and end time, select the maximum number of non-overlapping activities.</p><p><strong>Greedy Solution</strong>:</p><ul><li>Sort activities by their end times in ascending order.</li><li>Select the first activity (which has the earliest end time).</li><li>For each subsequent activity, if it starts after or when the previous activity ends, select it.</li></ul><p><strong>Step-by-Step Breakdown</strong>:</p><p>Suppose we have activities with start and end times as follows:</p><ul><li>Activity 1: (start = 1, end = 4)</li><li>Activity 2: (start = 3, end = 5)</li><li>Activity 3: (start = 0, end = 6)</li><li>Activity 4: (start = 5, end = 7)</li><li>Activity 5: (start = 8, end = 9)</li></ul><p>Sort activities by end time: Activity 1 (end = 4), Activity 2 (end = 5), Activity 4 (end = 7), Activity 5 (end = 9), Activity 3 (end = 6).</p><p>Select Activity 1 as it ends first.</p><p>Skip Activity 2, as it starts before Activity 1 ends.</p><p>Select Activity 4, as it starts after Activity 1 ends.</p><p>Select Activity 5, as it starts after Activity 4 ends.</p><p><strong>Result</strong>: The selected activities are 1, 4, and 5, allowing the maximum number of non-overlapping activities.</p><h3>Greedy Algorithm Practice Problems</h3><p>Here’s a list of popular greedy algorithm problems along with brief tips on how to approach each. Practicing these problems can help build an understanding of the greedy choice property and optimal substructure.</p><h4>1. Coin Change Problem</h4><ul><li><strong>Problem</strong>: Given a set of coin denominations and a target amount, find the minimum number of coins needed to make up the amount.</li><li><strong>Approach</strong>: Sort the coins in descending order, use the largest denomination as much as possible, and then proceed with the next largest. Note that this approach works effectively if the denominations allow a greedy solution (like in U.S. currency).</li></ul><pre>def coin_change(coins, amount):<br>    coins.sort(reverse=True)  # Sort coins in descending order<br>    count = 0<br>    for coin in coins:<br>        count += amount // coin  # Use as many coins of this denomination as possible<br>        amount %= coin  # Reduce the remaining amount<br>    return count if amount == 0 else -1  # Return -1 if it&#39;s not possible<br><br># Example usage:<br>coins = [25, 10, 5, 1]<br>amount = 67<br>print(coin_change(coins, amount))  # Output: 6</pre><h4>2. Fractional Knapsack Problem</h4><ul><li><strong>Problem</strong>: Given a set of items, each with a weight and value, and a maximum weight capacity, maximize the total value by taking fractional amounts of items if needed.</li><li><strong>Approach</strong>: Calculate the value-to-weight ratio of each item, then sort items by this ratio in descending order. Add items or fractions of them in this order until reaching the weight limit.</li></ul><pre>class Item:<br>    def __init__(self, value, weight):<br>        self.value = value<br>        self.weight = weight<br>        self.ratio = value / weight<br><br>def fractional_knapsack(items, capacity):<br>    items.sort(key=lambda x: x.ratio, reverse=True)  # Sort by value-to-weight ratio<br>    total_value = 0.0<br>    for item in items:<br>        if capacity &gt;= item.weight:<br>            total_value += item.value<br>            capacity -= item.weight<br>        else:<br>            total_value += item.ratio * capacity<br>            break<br>    return total_value<br><br># Example usage:<br>items = [Item(60, 10), Item(100, 20), Item(120, 30)]<br>capacity = 50<br>print(fractional_knapsack(items, capacity))  # Output: 240.0</pre><h4>3. Activity Selection Problem</h4><ul><li><strong>Problem</strong>: Given a set of activities with start and end times, select the maximum number of non-overlapping activities.</li><li><strong>Approach</strong>: Sort activities by end times, then select the earliest finishing activity and continue with the next compatible activity that starts after the previous one ends.</li></ul><pre>def activity_selection(activities):<br>    activities.sort(key=lambda x: x[1])  # Sort by end time<br>    selected_activities = [activities[0]]  # Always pick the first activity<br>    for i in range(1, len(activities)):<br>        if activities[i][0] &gt;= selected_activities[-1][1]:  # Check for non-overlapping<br>            selected_activities.append(activities[i])<br>    return selected_activities<br><br># Example usage:<br>activities = [(1, 4), (3, 5), (0, 6), (5, 7), (8, 9)]<br>print(activity_selection(activities))  # Output: [(1, 4), (5, 7), (8, 9)]</pre><h4>4. Minimum Spanning Tree (MST)</h4><ul><li><strong>Problem</strong>: Given a graph, find the subset of edges that connects all vertices with the minimum possible total edge weight, without any cycles.</li><li><strong>Approach</strong>: Use Kruskal’s or Prim’s algorithm, both of which are greedy. Kruskal’s adds the smallest edge while avoiding cycles, and Prim’s starts from a vertex, expanding the MST by the smallest adjacent edge.</li></ul><pre>class Edge:<br>    def __init__(self, u, v, weight):<br>        self.u = u<br>        self.v = v<br>        self.weight = weight<br><br>def kruskal_mst(edges, num_nodes):<br>    edges.sort(key=lambda x: x.weight)  # Sort edges by weight<br>    parent = list(range(num_nodes))<br><br>    def find(u):<br>        if u != parent[u]:<br>            parent[u] = find(parent[u])<br>        return parent[u]<br><br>    mst_weight = 0<br>    for edge in edges:<br>        u_root = find(edge.u)<br>        v_root = find(edge.v)<br>        if u_root != v_root:<br>            mst_weight += edge.weight<br>            parent[u_root] = v_root<br>    return mst_weight<br><br># Example usage:<br>edges = [Edge(0, 1, 10), Edge(0, 2, 6), Edge(0, 3, 5), Edge(1, 3, 15), Edge(2, 3, 4)]<br>print(kruskal_mst(edges, 4))  # Output: MST weight</pre><h4>5. Huffman Coding</h4><ul><li><strong>Problem</strong>: Given frequencies of characters, construct an optimal prefix-free binary code for data compression.</li><li><strong>Approach</strong>: Use a priority queue to combine the two least frequent characters repeatedly until only one tree remains. The greedy choice at each step ensures the optimal encoding length.</li></ul><pre>import heapq<br><br>class Node:<br>    def __init__(self, char, freq):<br>        self.char = char<br>        self.freq = freq<br>        self.left = self.right = None<br><br>    def __lt__(self, other):<br>        return self.freq &lt; other.freq<br><br>def huffman_code(frequencies):<br>    heap = [Node(char, freq) for char, freq in frequencies.items()]<br>    heapq.heapify(heap)<br><br>    while len(heap) &gt; 1:<br>        left = heapq.heappop(heap)<br>        right = heapq.heappop(heap)<br>        merged = Node(None, left.freq + right.freq)<br>        merged.left, merged.right = left, right<br>        heapq.heappush(heap, merged)<br><br>    def generate_codes(node, current_code, codes):<br>        if node:<br>            if node.char is not None:<br>                codes[node.char] = current_code<br>            generate_codes(node.left, current_code + &#39;0&#39;, codes)<br>            generate_codes(node.right, current_code + &#39;1&#39;, codes)<br>        return codes<br><br>    return generate_codes(heap[0], &quot;&quot;, {})<br><br># Example usage:<br>frequencies = {&#39;a&#39;: 5, &#39;b&#39;: 9, &#39;c&#39;: 12, &#39;d&#39;: 13, &#39;e&#39;: 16, &#39;f&#39;: 45}<br>print(huffman_code(frequencies))  # Output: Huffman codes for each character</pre><h4>6. Job Sequencing Problem</h4><ul><li><strong>Problem</strong>: Given a set of jobs with deadlines and profits, schedule the jobs to maximize total profit, ensuring each job is completed before its deadline.</li><li><strong>Approach</strong>: Sort jobs by profit in descending order, and schedule each job in the latest available slot before its deadline to maximize profit.</li></ul><pre>def job_sequencing(jobs, max_deadline):<br>    jobs.sort(key=lambda x: -x[1])  # Sort jobs by profit in descending order<br>    result = [-1] * max_deadline<br>    max_profit = 0<br><br>    for job in jobs:<br>        for j in range(min(max_deadline, job[0]) - 1, -1, -1):<br>            if result[j] == -1:<br>                result[j] = job[1]<br>                max_profit += job[1]<br>                break<br><br>    return max_profit<br><br># Example usage:<br>jobs = [(2, 100), (1, 50), (2, 10), (1, 20), (3, 30)]  # (deadline, profit)<br>max_deadline = 3<br>print(job_sequencing(jobs, max_deadline))  # Output: Maximum profit</pre><h4>7. Egyptian Fraction Problem</h4><ul><li><strong>Problem</strong>: Express a given fraction as a sum of unique unit fractions.</li><li><strong>Approach</strong>: Repeatedly take the largest possible unit fraction (starting with 1/ceil(denominator/numerator)) until the fraction is reduced to zero.</li></ul><pre>from math import ceil<br><br>def egyptian_fraction(numerator, denominator):<br>    result = []<br>    while numerator != 0:<br>        x = ceil(denominator / numerator)<br>        result.append(f&quot;1/{x}&quot;)<br>        numerator = x * numerator - denominator<br>        denominator *= x<br>    return result<br><br># Example usage:<br>print(egyptian_fraction(6, 14))  # Output: Egyptian fraction representation</pre><h4>8. Largest Number Problem</h4><ul><li><strong>Problem</strong>: Given a list of non-negative integers, arrange them to form the largest possible number.</li><li><strong>Approach</strong>: Sort numbers as strings, choosing a custom comparator that arranges pairs (a, b) so that concatenating a + b results in a larger number than b + a.</li></ul><pre>from functools import cmp_to_key<br><br>def largest_number(nums):<br>    nums = list(map(str, nums))<br>    nums.sort(key=cmp_to_key(lambda x, y: 1 if x + y &lt; y + x else -1))<br>    return &#39;&#39;.join(nums).lstrip(&#39;0&#39;) or &#39;0&#39;<br><br># Example usage:<br>nums = [3, 30, 34, 5, 9]<br>print(largest_number(nums))  # Output: &quot;9534330&quot;</pre><h4>9. Maximum Number of Platforms Required (Train Arrival and Departure)</h4><ul><li><strong>Problem</strong>: Given train arrival and departure times, find the minimum number of platforms needed so no two trains overlap on the same platform.</li><li><strong>Approach</strong>: Sort both arrival and departure times, and use two pointers to track overlapping intervals, incrementing the count of platforms as needed.</li></ul><pre>def min_platforms(arrivals, departures):<br>    arrivals.sort()<br>    departures.sort()<br>    platforms_needed, max_platforms = 0, 0<br>    i = j = 0<br><br>    while i &lt; len(arrivals):<br>        if arrivals[i] &lt; departures[j]:<br>            platforms_needed += 1<br>            i += 1<br>            max_platforms = max(max_platforms, platforms_needed)<br>        else:<br>            platforms_needed -= 1<br>            j += 1<br>    return max_platforms<br><br># Example usage:<br>arrivals = [900, 940, 950, 1100, 1500, 1800]<br>departures = [910, 1200, 1120, 1130, 1900, 2000]<br>print(min_platforms(arrivals, departures))  # Output: Minimum number of platforms required</pre><h4>10. Gas Station Problem</h4><ul><li><strong>Problem</strong>: Given a circular route with gas stations, each providing gas and requiring gas to reach the next station, determine if there’s a starting point that completes the circuit.</li><li><strong>Approach</strong>: Use a greedy approach to track cumulative gas balance and restart from the next station whenever a station cannot be reached.</li></ul><pre>def can_complete_circuit(gas, cost):<br>    total_gas = total_cost = start = tank = 0<br>    for i in range(len(gas)):<br>        total_gas += gas[i]<br>        total_cost += cost[i]<br>        tank += gas[i] - cost[i]<br>        if tank &lt; 0:<br>            start = i + 1<br>            tank = 0<br>    return start if total_gas &gt;= total_cost else -1<br><br># Example usage:<br>gas = [1, 2, 3, 4, 5]<br>cost = [3, 4, 5, 1, 2]<br>print(can_complete_circuit(gas, cost))  # Output: Starting index for completing the circuit</pre><h3>Conclusion</h3><p><strong>Summary</strong>: In this article, we explored greedy algorithms in depth, covering their core characteristics, the process of designing a greedy algorithm, and examples of some classic greedy problems, each with an illustrative Python solution. Greedy algorithms excel at solving optimization problems by making the best immediate choice at each step, aiming to lead to an overall optimal solution. With a range of applications across fields like scheduling, resource allocation, and graph theory, understanding the principles of greedy algorithms is essential for solving various real-world problems efficiently.</p><p><strong>Final Tips</strong>: Mastering greedy algorithms requires a blend of theoretical knowledge and practical problem-solving skills. Here are a few tips for advancing your understanding:</p><ol><li><strong>Practice Diverse Problems</strong>: Try a variety of greedy algorithm problems to recognize patterns and scenarios where the greedy approach works best, especially problems involving sorting, resource allocation, and selection.</li><li><strong>Analyze Problem Constraints</strong>: Some problems appear suited for a greedy solution but require dynamic programming or another approach. Carefully analyze problem constraints and check for the <em>greedy choice property</em> and <em>optimal substructure</em> to confirm that a greedy solution is appropriate.</li><li><strong>Optimize Through Code</strong>: As you practice, focus on implementing your greedy solutions efficiently. Aim to use data structures like heaps, sorted lists, and priority queues to ensure your solution is both optimal and performs well on large inputs.</li></ol><p><strong>~~Thanks for reading and supporting.</strong></p><h3>Stackademic 🎓</h3><p>Thank you for reading until the end. Before you go:</p><ul><li>Please consider <strong>clapping</strong> and <strong>following</strong> the writer! 👏</li><li>Follow us <a href="https://twitter.com/stackademichq"><strong>X</strong></a> | <a href="https://www.linkedin.com/company/stackademic"><strong>LinkedIn</strong></a> | <a href="https://www.youtube.com/c/stackademic"><strong>YouTube</strong></a> | <a href="https://discord.gg/in-plain-english-709094664682340443"><strong>Discord</strong></a> | <a href="https://newsletter.plainenglish.io/"><strong>Newsletter</strong></a> | <a href="https://open.spotify.com/show/7qxylRWKhvZwMz2WuEoua0"><strong>Podcast</strong></a></li><li><a href="https://differ.blog/"><strong>Create a free AI-powered blog on Differ.</strong></a></li><li>More content at <a href="https://stackademic.com/"><strong>Stackademic.com</strong></a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=54758568513e" width="1" height="1" alt=""><hr><p><a href="https://blog.stackademic.com/greedy-algorithm-54758568513e">Greedy Algorithm</a> was originally published in <a href="https://blog.stackademic.com/">Stackademic</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[I Can Teach You ‘Priority Queue’]]></title>
            <link>https://python.plainenglish.io/i-can-teach-you-priority-queue-2799fb832e02?source=rss-bc8547726b7d------2</link>
            <guid isPermaLink="false">https://medium.com/p/2799fb832e02</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[data]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[education]]></category>
            <category><![CDATA[coding]]></category>
            <dc:creator><![CDATA[Kapil Bhardwaj]]></dc:creator>
            <pubDate>Tue, 29 Oct 2024 03:39:11 GMT</pubDate>
            <atom:updated>2024-10-29T03:39:11.309Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/626/0*3jGjRWv9V5_VWTOF.jpg" /></figure><p>A Priority Queue is a specialized data structure in computer science that operates similarly to a standard queue but with a unique twist: then each of them is associated with a priority level, at which indicates the order in which the components are considered. A Priority Queue is used for a situation where elements have to be served dependent on priority and not by their arrival time which is done for a typical queue. Utilized in most algorithms as well as in a number of applications, this data structure has its primary usage in scheduling, path search and data compression algorithms. Priority Queue is one of the most fundamental data structures out there, and in this article, we will look at the principles behind the Priority Queue as well as at examples of its usage in program’s functionality.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*DJVAk5oJZeaiV7Bo9uMntA.png" /></figure><h3>Why, Where, When priority queues?</h3><p><strong>Why Priority Queues?</strong> Priority queues are essential when you need efficient access to the highest (or lowest) priority element among many. <strong>Their unique structure allows constant-time access</strong> to the highest priority item and efficient insertion and deletion. This makes them indispensable in tasks where managing priorities is crucial, such as job scheduling, network packet management, and simulations.</p><p><strong>Where are Priority Queues Used?</strong> Priority queues are commonly used in algorithms that require prioritized processing:</p><ul><li><em>Dijkstra’s and A Algorithms</em>*: Priority queues help in finding the shortest path by prioritizing nodes with the smallest current distance.</li><li><strong>Operating System Scheduling</strong>: They help in managing processes and tasks based on priority.</li><li><strong>Data Compression</strong>: Huffman encoding uses priority queues to construct an optimal binary tree for encoding symbols.</li><li><strong>Real-Time Systems</strong>: For tasks that require timely responses, priority queues ensure that critical operations are processed first.</li></ul><p><strong>When to Use Priority Queues?</strong> Priority queues are ideal when you have elements that need to be processed based on urgency rather than order of arrival. Some examples include:</p><ul><li><strong>Job Scheduling</strong>: Use a priority queue to handle jobs based on importance, not arrival time.</li><li><strong>Event-Driven Simulation</strong>: When simulating events with different timestamps, a priority queue helps by ensuring the earliest events are processed first.</li><li><strong>Network Routers</strong>: Packets with higher priority (like emergency data) are handled before others.</li></ul><h3><strong>Key Operations of a Priority Queue</strong></h3><p>In a priority queue, several key operations allow for efficient handling of elements based on priority levels. These operations ensure that high-priority elements are accessible for processing as needed. Here are the main operations:</p><h4>1. Insert (Enqueue)</h4><ul><li><strong>Description</strong>: This operation adds an element with a given priority to the queue.</li><li><strong>Process</strong>: The new element is inserted into the appropriate position to maintain the priority order.</li><li><strong>Time Complexity</strong>: O(log n) in a heap-based priority queue, as reordering is required to maintain the priority structure.</li><li><strong>Example</strong>: Inserting a task with a high priority ensures it is positioned near the front of the queue for quick access.</li></ul><pre>import heapq<br><br># Create an empty priority queue (min-heap)<br>priority_queue = []<br><br>def enqueue(priority_queue, element):<br>    # Insert element into the heap<br>    heapq.heappush(priority_queue, element)<br><br># Example usage<br>enqueue(priority_queue, 5)<br>enqueue(priority_queue, 2)<br>enqueue(priority_queue, 8)<br>print(&quot;Priority Queue after insertion:&quot;, priority_queue)</pre><h4>2. Delete or Pop (Dequeue)</h4><ul><li><strong>Description</strong>: Removes the element with the highest priority in the case of a max-priority queue or the lowest priority in a min-priority queue.</li><li><strong>Process</strong>: Typically, the root element (highest or lowest priority, depending on the type) is removed, and the queue is then restructured to maintain order.</li><li><strong>Time Complexity</strong>: O(log n), as elements must be reordered after deletion.</li><li><strong>Example</strong>: Removing the task with the highest priority, like a critical process in a CPU scheduler, to make room for the next highest-priority task.</li></ul><pre>def dequeue(priority_queue):<br>    if priority_queue:<br>        # Remove the highest-priority element (smallest in min-heap)<br>        return heapq.heappop(priority_queue)<br>    return None<br><br># Example usage<br>dequeued_element = dequeue(priority_queue)<br>print(&quot;Dequeued Element:&quot;, dequeued_element)<br>print(&quot;Priority Queue after deletion:&quot;, priority_queue)</pre><h4>3. Peek</h4><ul><li><strong>Description</strong>: Retrieves the element with the highest or lowest priority without removing it from the queue.</li><li><strong>Process</strong>: Accesses the root element of a heap-based priority queue, which is the highest-priority element.</li><li><strong>Time Complexity</strong>: O(1), as the top element is accessed directly.</li><li><strong>Example</strong>: Quickly checking the most urgent task without dequeuing it, useful in systems that may need to re-evaluate task priorities before proceeding.</li></ul><pre>def peek(priority_queue):<br>    if priority_queue:<br>        # Access the smallest element in a min-heap<br>        return priority_queue[0]<br>    return None<br><br># Example usage<br>top_element = peek(priority_queue)<br>print(&quot;Top element (highest priority):&quot;, top_element)</pre><h4>4. Heapify (Rebuild)</h4><ul><li><strong>Description</strong>: Ensures the priority queue maintains the correct ordering of elements after bulk operations, such as inserting multiple items.</li><li><strong>Process</strong>: The heapify operation adjusts the structure by moving elements up or down to maintain the heap property.</li><li><strong>Time Complexity</strong>: O(n) when performed on an array of unsorted elements.</li><li><strong>Example</strong>: After loading multiple tasks into a priority queue, heapify orders them by priority, readying them for immediate access.</li></ul><pre>def build_heap(elements):<br>    # Transform the list into a heap<br>    heapq.heapify(elements)<br>    return elements<br><br># Example usage<br>elements = [10, 3, 5, 1, 4]<br>priority_queue = build_heap(elements)<br>print(&quot;Priority Queue after heapify:&quot;, priority_queue)</pre><h4>5. Extract-Max or Extract-Min</h4><ul><li><strong>Description</strong>: Specifically removes the highest-priority (max) or lowest-priority (min) element from the queue.</li><li><strong>Process</strong>: Removes the root of a max- or min-priority heap, followed by restructuring to maintain the priority order.</li><li><strong>Time Complexity</strong>: O(log n), similar to delete, as it requires reordering the queue.</li><li><strong>Example</strong>: Used in algorithms like heap sort, where repeatedly extracting the max element results in a sorted list.</li></ul><p>. <strong>Extract-Min</strong></p><pre>def extract_min(priority_queue):<br>    return heapq.heappop(priority_queue) if priority_queue else None<br><br># Example usage<br>min_element = extract_min(priority_queue)<br>print(&quot;Extracted Min Element:&quot;, min_element)<br>print(&quot;Priority Queue after Extract-Min:&quot;, priority_queue)</pre><p><strong>.Extract-Max</strong></p><p>For max-heap behavior, we insert negative values and invert them back.</p><pre>def extract_max(max_priority_queue):<br>    return -heapq.heappop(max_priority_queue) if max_priority_queue else None<br><br># Example usage for max-heap<br>max_priority_queue = []<br>heapq.heappush(max_priority_queue, -10)<br>heapq.heappush(max_priority_queue, -3)<br>heapq.heappush(max_priority_queue, -5)<br><br>max_element = extract_max(max_priority_queue)<br>print(&quot;Extracted Max Element:&quot;, max_element)<br>print(&quot;Priority Queue after Extract-Max:&quot;, [-x for x in max_priority_queue])</pre><h3>Implementing a Priority Queue</h3><h4>1. Using an Unordered Array or List</h4><p>In this approach, elements are added to the end of an array or list without sorting. The priority is not maintained in any specific order during insertion, making it straightforward to add new elements.</p><p><strong>Benefits</strong>:</p><ul><li><strong>Simple and Fast Insertion</strong>: Adding elements is efficient because each insertion operation is O(1).</li></ul><p><strong>Drawbacks</strong>:</p><ul><li><strong>Slow Deletion</strong>: To remove the highest-priority element, we must search through the entire list to find it, resulting in a time complexity of O(n).</li></ul><p><strong>Code Example</strong>:</p><pre>class UnorderedPriorityQueue:<br>    def __init__(self):<br>        self.queue = []<br>    <br>    def enqueue(self, element):<br>        self.queue.append(element)  # Insert element at the end (O(1))<br>    <br>    def dequeue(self):<br>        # Find the highest priority element (largest) and remove it<br>        if not self.queue:<br>            return None<br>        max_element = max(self.queue)  # Find max (O(n))<br>        self.queue.remove(max_element)  # Remove max (O(n))<br>        return max_element</pre><h4>2. Using a Sorted Array or List</h4><p>With a sorted array or list, each new element is inserted in a position that maintains the overall order of priorities. This keeps the list sorted in descending order so that the highest-priority element is always at the start.</p><p><strong>Benefits</strong>:</p><ul><li><strong>Fast Deletion</strong>: Since the list is already sorted, the highest-priority element is always at the beginning, making the deletion operation O(1).</li></ul><p><strong>Drawbacks</strong>:</p><ul><li><strong>Slow Insertion</strong>: To maintain the sorted order, each insertion requires finding the correct position and shifting elements, resulting in O(n) time complexity for each insertion.</li></ul><p><strong>Code Example</strong>:</p><pre>class SortedPriorityQueue:<br>    def __init__(self):<br>        self.queue = []<br>    <br>    def enqueue(self, element):<br>        # Insert element in sorted order<br>        self.queue.append(element)<br>        self.queue.sort(reverse=True)  # Sort in descending order (O(n log n))<br>    <br>    def dequeue(self):<br>        # Remove the first element (highest priority)<br>        if not self.queue:<br>            return None<br>        return self.queue.pop(0)  # Remove max (O(1))</pre><h4>3. Using a Binary Heap (Optimal Approach)</h4><p>A <strong>Binary Heap</strong> is the most efficient data structure for implementing priority queues. It allows both insertion and deletion in logarithmic time, making it ideal for scenarios requiring frequent priority adjustments. Binary heaps come in two types: <strong>min-heaps</strong> and <strong>max-heaps</strong>. In a min-heap, the smallest element (highest priority) is at the root, while in a max-heap, the largest element is at the root.</p><p><strong>Why Heaps are Common for Priority Queues</strong>:</p><ul><li><strong>Efficient Operations</strong>: Both insertion and deletion are O(log n), making heaps well-suited for managing dynamically changing priority lists.</li><li><strong>Memory Efficiency</strong>: Heaps are stored in arrays, which is both memory-efficient and fast for indexed access.</li></ul><p><strong>How Heaps Maintain Priority</strong>:</p><ul><li>The heap structure ensures that the highest-priority element is always at the root (for min-heap, the smallest element; for max-heap, the largest element).</li><li>Insertion and deletion operations are followed by “heapify” steps that rearrange elements to maintain the heap property, ensuring that the highest-priority element remains at the root.</li></ul><p><strong>Code Example Using </strong><strong>heapq for Min-Heap</strong>:</p><pre>import heapq<br><br>class HeapPriorityQueue:<br>    def __init__(self):<br>        self.queue = []<br>    <br>    def enqueue(self, element):<br>        # Insert element into the heap (min-heap by default)<br>        heapq.heappush(self.queue, element)  # O(log n)<br>    <br>    def dequeue(self):<br>        # Remove and return the smallest element (highest priority in min-heap)<br>        if not self.queue:<br>            return None<br>        return heapq.heappop(self.queue)  # O(log n)<br>     <br>    def peek(self):<br>        # Access the smallest element without removing it<br>        return self.queue[0] if self.queue else None</pre><p><strong>Code Example Using </strong><strong>heapq for Max-Heap</strong>: To implement a max-heap, we store elements as negatives so that the largest element has the smallest negative value, which heapq interprets as the “smallest.”</p><pre>import heapq<br><br>class MaxHeapPriorityQueue:<br>    def __init__(self):<br>        self.queue = []<br>    <br>    def enqueue(self, element):<br>        # Insert element as a negative to simulate max-heap<br>        heapq.heappush(self.queue, -element)  # O(log n)<br>    <br>    def dequeue(self):<br>        # Remove and return the largest element (invert sign back to positive)<br>        if not self.queue:<br>            return None<br>        return -heapq.heappop(self.queue)  # O(log n)<br>    <br>    def peek(self):<br>        # Access the largest element without removing it<br>        return -self.queue[0] if self.queue else None</pre><h3>Conclusion</h3><p>In conclusion, priority queues are essential data structures that play a critical role in efficient data handling across various applications, from scheduling tasks in operating systems to optimizing network traffic and implementing algorithms like Dijkstra’s shortest path. By understanding different implementations — such as unordered lists, sorted arrays, and binary heaps — you can choose the best approach for your specific requirements. Building a priority queue from scratch using a binary heap provides insight into how priority management works internally, helping you appreciate the advantages of this data structure in time-sensitive applications. Practicing with these implementations and understanding their nuances will strengthen your data structure skills, setting you up for success in both technical interviews and real-world software development scenarios.</p><p>Priority queues are valuable in both theory and practice, and mastering them opens doors to more advanced topics in algorithmic efficiency and resource optimization. Keep experimenting with different problems and applications to solidify your understanding and see the full potential of priority queues!</p><h3>In Plain English 🚀</h3><p><em>Thank you for being a part of the </em><a href="https://plainenglish.io/"><strong><em>In Plain English</em></strong></a><em> community! Before you go:</em></p><ul><li>Be sure to <strong>clap</strong> and <strong>follow</strong> the writer ️👏<strong>️️</strong></li><li>Follow us: <a href="https://x.com/inPlainEngHQ"><strong>X</strong></a> | <a href="https://www.linkedin.com/company/inplainenglish/"><strong>LinkedIn</strong></a> | <a href="https://www.youtube.com/channel/UCtipWUghju290NWcn8jhyAw"><strong>YouTube</strong></a> | <a href="https://discord.gg/in-plain-english-709094664682340443"><strong>Discord</strong></a> | <a href="https://newsletter.plainenglish.io/"><strong>Newsletter</strong></a> | <a href="https://open.spotify.com/show/7qxylRWKhvZwMz2WuEoua0"><strong>Podcast</strong></a></li><li><a href="https://differ.blog/"><strong>Create a free AI-powered blog on Differ.</strong></a></li><li>More content at <a href="https://plainenglish.io/"><strong>PlainEnglish.io</strong></a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2799fb832e02" width="1" height="1" alt=""><hr><p><a href="https://python.plainenglish.io/i-can-teach-you-priority-queue-2799fb832e02">I Can Teach You ‘Priority Queue’</a> was originally published in <a href="https://python.plainenglish.io/">Python in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Lets Learn Heap In an Easy Way]]></title>
            <link>https://python.plainenglish.io/lets-learn-heap-in-an-easy-way-0275c6d32769?source=rss-bc8547726b7d------2</link>
            <guid isPermaLink="false">https://medium.com/p/0275c6d32769</guid>
            <category><![CDATA[data-structures]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[education]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Kapil Bhardwaj]]></dc:creator>
            <pubDate>Mon, 28 Oct 2024 03:54:12 GMT</pubDate>
            <atom:updated>2024-10-28T03:54:12.576Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/723/1*iFqm456Cyxg5d_XMaysItw.png" /></figure><p>In the case of data structures, heaps may look complicated but don’t be fooled, heaps are pretty cool and useful data structures. Heap is a type of binary tree which makes it possible to resolve such operations, as finding the greatest and the smallest numbers as well as sorting data and working with the so-called priority tasks. Heaps have many applications in algorithms ranging from shortest paths such as Dijkstra’s algorithm, and Huffman encoding which make them important for both novice and advance development.</p><p>In this article, you will learn What are heaps, how they are built, the kinds of heaps, what operations can be done on heaps, and other useful information regarding heaps. Okay, let’s go deeper and find out how winning heaps can enrich your problem practicing and make difficult problems easier to solve!</p><h3><strong>Where Do Heaps Fit In?</strong></h3><p><strong>Heaps are primarily used in scenarios that require quick access to the largest or smallest element in a dataset.</strong> This makes them extremely valuable for operations involving priority queues, sorting, and task scheduling. Unlike other binary trees, heaps don’t need to be completely sorted; instead, they adhere to specific ordering properties that simplify access to the “top” element.</p><h3><strong>Importance and Real-World Applications of Heaps</strong></h3><p>Heaps play a significant role in many real-world applications because of their efficiency in handling prioritized data. Some common uses include:</p><ul><li><strong>Priority Queues</strong>: Heaps make it easy to implement priority queues, where each element has a priority and elements with higher priority are processed first.</li><li><strong>Graph Algorithms</strong>: In algorithms like Dijkstra’s shortest path, heaps enable quick access to the smallest distance nodes, optimizing performance.</li><li><strong>Task Scheduling</strong>: Operating systems often use heaps to prioritize tasks based on urgency, ensuring that critical tasks receive immediate attention.</li><li><strong>Memory Management</strong>: Heaps are used in memory management within various systems to allocate and deallocate memory efficiently.</li><li><strong>Data Compression</strong>: In Huffman coding, used for data compression, heaps assist in quickly identifying the most frequent elements for optimal encoding.</li></ul><h3><strong>Types of Problems Heaps Can Solve</strong></h3><p>Heaps are perfect for a wide range of tasks that involve ordered data processing. Some classic problems include:</p><ul><li><strong>Finding the Maximum or Minimum Element</strong>: In constant time, heaps can provide the largest or smallest element.</li><li><strong>Kth Largest or Smallest Element</strong>: Useful in scenarios where we need to find specific ranking elements without fully sorting the data.</li><li><strong>Heap Sort</strong>: A popular sorting algorithm that uses heaps to sort data in an efficient, in-place manner.</li><li><strong>Median in Data Streams</strong>: Heaps help manage incoming data and maintain an ongoing median calculation, particularly useful in real-time analytics.</li></ul><h3><strong>Definition and Properties of a Heap</strong></h3><p>A heap is generally represented as a binary tree and has the following properties:</p><ul><li><strong>Complete Binary Tree</strong>: All levels of the tree are fully filled, except possibly for the last level, which is filled from left to right.</li><li><strong>Heap Property</strong>: Every node follows the min-heap or max-heap order with respect to its parent:</li><li>In a <strong>min-heap</strong>, each parent node is smaller than (or equal to) its children.</li><li>In a <strong>max-heap</strong>, each parent node is larger than (or equal to) its children.</li></ul><p>This structure ensures that the root node always contains the smallest element in a min-heap or the largest element in a max-heap, making it efficient to retrieve the minimum or maximum value without sorting the entire dataset.</p><h3><strong>Binary Heap Structure</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/486/1*V2zBsphl5jrA25cxaBRktw.png" /></figure><p>A binary heap is the most common type of heap. It’s structured as a complete binary tree, which has significant benefits when implemented as an array. For a given node at index i in a zero-indexed array:</p><ul><li><strong>Parent node</strong> is at index (i - 1) / 2.</li><li><strong>Left child</strong> is at index 2 * i + 1.</li><li><strong>Right child</strong> is at index 2 * i + 2.</li></ul><p>By organizing the heap in an array, we avoid the overhead of pointers typical in tree structures, making it both memory-efficient and simple to navigate.</p><h3><strong>Min-Heaps vs. Max-Heaps</strong></h3><p>The two primary types of heaps, min-heaps and max-heaps, differ only in their ordering:</p><ul><li><strong>Min-Heap</strong>:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/467/1*OgpqPLJWG7iF8yfsxN4bNQ.png" /></figure><p>The root node contains the smallest element in the heap. Every parent node is smaller than or equal to its child nodes. This makes the min-heap ideal for priority queues where the smallest element is always processed first. Insertion and deletion operations ensure that the heap property remains intact.</p><ul><li><strong>Max-Heap</strong>:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/468/1*ewtQV6xISyZAm2HK9kl1MQ.png" /></figure><p>The root node contains the largest element in the heap. Every parent node is larger than or equal to its child nodes. This setup is useful for applications where the highest-priority element should be accessed immediately, such as task scheduling or managing maximum values.</p><h3>Heap Types</h3><p>Heaps come in various forms, each tailored for specific use cases and optimized for certain types of operations. Here are the most common types of heaps and their unique characteristics:</p><h4>1. Binary Heap</h4><p><strong>Structure</strong>: A binary heap is a complete binary tree, where all levels are fully filled except the last level, which fills from left to right.</p><p><strong>Variants</strong>: Comes in two types: min-heap and max-heap.</p><p><strong>Properties</strong>:</p><ul><li><strong>Min-Heap</strong>: The root is the minimum element, and each parent node is less than or equal to its children.</li><li><strong>Max-Heap</strong>: The root is the maximum element, and each parent node is greater than or equal to its children.</li></ul><p><strong>Applications</strong>: Widely used for priority queues, heap sort, and algorithms like Dijkstra’s shortest path.</p><p><strong>Time Complexity</strong>:</p><ul><li>Insertion: O(log⁡n)</li><li>Deletion of root: O(log⁡n)</li><li>Peek (getting min or max): O(1)</li></ul><h4>2. Fibonacci Heap</h4><p><strong>Structure</strong>: A collection of heap-ordered trees, where each tree is a min-heap or max-heap.</p><p><strong>Properties</strong>:</p><ul><li>More flexible than a binary heap with amortized time complexity improvements.</li><li>Supports decrease-key and delete operations more efficiently than a binary heap.</li></ul><p><strong>Applications</strong>: Used in advanced algorithms, particularly for graph-related algorithms like Dijkstra’s and Prim’s, where frequent decrease-key operations are required.</p><p><strong>Time Complexity</strong>:</p><ul><li>Insertion: O(1) (amortized)</li><li>Delete-min (or max): O(log⁡n (amortized)</li><li>Decrease key: O(1) (amortized)</li></ul><h4>3. Binomial Heap</h4><p><strong>Structure</strong>: A collection of binomial trees that satisfy the min-heap or max-heap property.</p><p><strong>Properties</strong>:</p><ul><li>Efficient merging of two heaps, making it useful for union operations.</li><li>Each binomial tree in the heap has a specific order, with one tree of each order in the heap.</li></ul><p><strong>Applications</strong>: Used in scenarios requiring frequent merging of heaps, like distributed computing and network optimization.</p><p><strong>Time Complexity</strong>:</p><ul><li>Insertion: O(log⁡n)</li><li>Delete-min (or max): O(log⁡n)</li><li>Merge: O(log⁡n)</li></ul><h4>4. Pairing Heap</h4><p><strong>Structure</strong>: A heap of pairs or subtrees, often represented as a forest-like structure.</p><p><strong>Properties</strong>:</p><ul><li>Easy to implement and has excellent amortized time complexity.</li><li>Good performance for decrease-key operations, making it competitive with Fibonacci heaps in practice.</li></ul><p><strong>Applications</strong>: Commonly used in graph algorithms where decrease-key operations are frequent and merging heaps is advantageous.</p><p><strong>Time Complexity</strong>:</p><ul><li>Insertion: O(1)</li><li>Delete-min (or max): O(log⁡n) (amortized)</li><li>Decrease key: O(1) (amortized)</li></ul><h4>5. D-ary Heap</h4><p><strong>Structure</strong>: A generalization of the binary heap where each node has ddd children instead of just two.</p><p><strong>Properties</strong>:</p><ul><li>Reduces tree height, making it efficient for insertions and deletions.</li><li>Useful in performance tuning for heaps with a larger number of children per node.</li></ul><p><strong>Applications</strong>: Used in applications requiring higher fan-out to improve cache locality, like real-time task scheduling.</p><p><strong>Time Complexity</strong>:</p><ul><li>Insertion: O(log⁡dn)</li><li>Delete-min (or max): O(d⋅log⁡dn)</li></ul><h4>6. Skew Heap</h4><p><strong>Structure</strong>: A binary tree with no specific shape constraints but maintains heap order.</p><p><strong>Properties</strong>:</p><p>Self-adjusting, with nodes rearranged in a way that favors merge operations.</p><p>Similar in concept to a splay tree for heaps, it adjusts dynamically with each merge.</p><p><strong>Applications</strong>: Used in scenarios with frequent merging, especially in memory-intensive applications.</p><p><strong>Time Complexity</strong>:</p><ul><li>Merge: O(log⁡n) (amortized)</li><li>Insertion and deletion complexities depend on the dynamic structure.</li></ul><h3>Heap Representation</h3><p>Heaps are often represented as arrays due to the simplicity and efficiency this approach offers. By using an array, we can store heap elements without needing to use pointers or complex node structures, which is typical in tree implementations. This structure, especially for binary heaps, allows for easy navigation of parent and child relationships using simple arithmetic.</p><h4>Representing a Heap as an Array</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/433/1*LgFXT30G0QQCddDw9mPzTQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/281/1*4AmyTAxSspYRGI1jDa5w6A.png" /></figure><p>In an array-based heap:</p><ul><li>The root element of the heap is stored at the first index (usually index 0 in a zero-based index system).</li><li>Each element in the array represents a node in the heap, and the position of each node in the array determines its relationship with other nodes (parent and children).</li></ul><p>This layout makes it easy to add new elements or remove the root by simply modifying elements within the array, keeping the structure intact.</p><h4>How Array Indexing Works in Heaps</h4><p>For a given element at index i in a zero-indexed array:</p><ul><li><strong>Parent Node</strong>: Located at index (i - 1) / 2</li><li><strong>Left Child Node</strong>: Located at index 2 * i + 1</li><li><strong>Right Child Node</strong>: Located at index 2 * i + 2</li></ul><p>These formulas allow us to navigate the heap’s structure without any additional pointers, simply by performing basic arithmetic operations on array indices.</p><h4>Benefits of Using an Array for Heap Storage</h4><ol><li><strong>Memory Efficiency</strong>: Arrays don’t require extra memory for pointers, as would be necessary in a linked-node tree structure. This makes arrays more memory-efficient, especially for large heaps.</li><li><strong>Easy Navigation and Computation</strong>: Parent-child relationships are derived directly from index calculations, making navigation between levels straightforward and efficient.</li><li><strong>Efficient Resizing</strong>: Heaps stored in dynamic arrays can be resized easily, allowing elements to be added or removed without restructuring the entire heap.</li><li><strong>Fast Access to Root</strong>: The root element is always at the first index (0), providing constant-time access to the minimum or maximum element (in min-heap or max-heap, respectively).</li><li><strong>Simplicity of Implementation</strong>: Array-based heaps avoid the complexity of pointer-based trees, simplifying both the heap implementation and debugging process.</li></ol><h3>Heap Operations</h3><p>Here’s an in-depth look at the primary operations for a binary heap: Insert, Delete, Heapify, Extract-Max/Extract-Min, and Peek. Each code example will demonstrate the operation separately, with comments to explain the steps involved.</p><h4>1. Insert Operation</h4><p>When inserting a new element into a heap, it is added at the end of the array to maintain the complete tree structure. Then, we “bubble up” this element until the heap property is restored.</p><pre>def insert(heap, element):<br>    heap.append(element)  # Step 1: Add the element to the end of the heap<br>    index = len(heap) - 1  # Current index of the newly added element<br><br>    # Step 2: Bubble up to maintain the heap property<br>    while index &gt; 0:<br>        parent = (index - 1) // 2<br>        if heap[parent] &gt; heap[index]:  # Min-heap condition: parent &gt; child<br>            heap[parent], heap[index] = heap[index], heap[parent]  # Swap<br>            index = parent  # Move up to the parent<br>        else:<br>            break  # Stop if the heap property is satisfied<br><br># Example usage:<br>min_heap = []<br>insert(min_heap, 10)<br>insert(min_heap, 5)<br>insert(min_heap, 3)<br>insert(min_heap, 2)<br># min_heap should now be a valid min-heap: [2, 5, 3, 10]</pre><h4>2. Delete Operation</h4><p>Deleting the root element (min or max) requires removing the root and restructuring the heap to maintain the heap property.</p><pre>def delete_root(heap):<br>    if len(heap) == 0:<br>        return None  # Return if heap is empty<br><br>    root = heap[0]  # The root element to be deleted (min or max)<br>    heap[0] = heap.pop()  # Move the last element to the root and remove the last<br><br>    # Step 2: Heapify down from the root to restore the heap property<br>    index = 0<br>    while index &lt; len(heap):<br>        left = 2 * index + 1<br>        right = 2 * index + 2<br>        smallest = index<br><br>        # Check if the left child is smaller<br>        if left &lt; len(heap) and heap[left] &lt; heap[smallest]:<br>            smallest = left<br><br>        # Check if the right child is smaller<br>        if right &lt; len(heap) and heap[right] &lt; heap[smallest]:<br>            smallest = right<br><br>        # If the smallest element is not the current index, swap<br>        if smallest != index:<br>            heap[index], heap[smallest] = heap[smallest], heap[index]<br>            index = smallest  # Move down to the child<br>        else:<br>            break  # Stop if the heap property is restored<br><br>    return root  # Return the deleted root element<br><br># Example usage:<br>min_heap = [2, 5, 3, 10]<br>delete_root(min_heap)<br># min_heap should now be [3, 5, 10]</pre><h4>3. Heapify Operation</h4><p>Heapify is used to adjust a subtree to maintain the heap property. This is useful during insertion, deletion, and building a heap from an unsorted array.</p><pre>def heapify(heap, index):<br>    size = len(heap)<br>    smallest = index<br>    left = 2 * index + 1<br>    right = 2 * index + 2<br><br>    # Check if the left child is smaller<br>    if left &lt; size and heap[left] &lt; heap[smallest]:<br>        smallest = left<br><br>    # Check if the right child is smaller<br>    if right &lt; size and heap[right] &lt; heap[smallest]:<br>        smallest = right<br><br>    # Swap and continue heapifying if the smallest element is not the current index<br>    if smallest != index:<br>        heap[index], heap[smallest] = heap[smallest], heap[index]<br>        heapify(heap, smallest)  # Recursive call to heapify the subtree<br><br># Example usage:<br>min_heap = [10, 5, 3, 2]<br>heapify(min_heap, 0)<br># min_heap should now be adjusted if needed</pre><h4>4. Extract-Max or Extract-Min Operation</h4><p>Extracting the minimum (for min-heap) or maximum (for max-heap) involves removing the root and restructuring the heap.</p><pre>def extract_min(heap):<br>    if len(heap) == 0:<br>        return None  # Return if heap is empty<br><br>    min_element = heap[0]  # Root element (minimum in min-heap)<br>    heap[0] = heap.pop()  # Move the last element to the root and remove the last<br><br>    # Restore the heap property by heapifying down from the root<br>    heapify(heap, 0)<br><br>    return min_element  # Return the extracted min element<br><br># Example usage:<br>min_heap = [2, 5, 3, 10]<br>min_val = extract_min(min_heap)<br># min_heap should now be [3, 5, 10], min_val should be 2</pre><h4>5. Peek Operation</h4><p>Peek simply returns the root element without removing it, providing constant-time access to the minimum or maximum element.</p><pre>def peek(heap):<br>    if len(heap) == 0:<br>        return None  # Return if heap is empty<br>    return heap[0]  # The root element (min in min-heap or max in max-heap)<br><br># Example usage:<br>min_heap = [2, 5, 3, 10]<br>root_val = peek(min_heap)  # root_val should be 2 without modifying the heap</pre><h3>Building a Heap</h3><p>When constructing a heap from an unsorted array, the bottom-up approach is both efficient and commonly used. This approach involves starting from the last non-leaf node and applying the heapify operation to each node up to the root, ensuring the heap property is maintained throughout the structure.</p><h4>Bottom-Up Approach (Heapify)</h4><p>The bottom-up approach:</p><ol><li>Identifies the last non-leaf node (at index (n // 2) - 1 for a zero-indexed array).</li><li>Applies the heapify function on each non-leaf node, starting from the bottom and moving upwards.</li><li>Once all nodes are heapified, the array will satisfy the heap property.</li></ol><p>This approach is efficient because heapify on nodes closer to the bottom (leaf nodes) requires fewer operations compared to those closer to the root, resulting in an overall linear time complexity.</p><p>Here’s how the bottom-up approach works in code:</p><pre>def build_heap(heap):<br>    # Start from the last non-leaf node and apply heapify upwards to the root<br>    for i in range(len(heap) // 2 - 1, -1, -1):<br>        heapify(heap, i)<br><br># Example usage:<br>unsorted_array = [10, 20, 15, 30, 40]<br>build_heap(unsorted_array)<br># After calling build_heap, unsorted_array will satisfy the min-heap property</pre><p>When constructing a heap from an unsorted array, the bottom-up approach is both efficient and commonly used. This approach involves starting from the last non-leaf node and applying the heapify operation to each node up to the root, ensuring the heap property is maintained throughout the structure.</p><h4>Time Complexity Analysis</h4><ul><li><strong>Single </strong><strong>heapify Operation</strong>: Each call to heapify on a node with height h takes O(h) time.</li><li><strong>Total Time for Bottom-Up Heap Construction</strong>: When summing the time taken across all nodes, it results in an O(n) time complexity for the entire build_heap process.</li></ul><p>The linear time complexity is possible because the deeper nodes (with smaller subtrees) require less work, while the larger subtrees closer to the root are fewer in number.</p><h4>Practical Examples of Heap-Building Applications</h4><ol><li><strong>Priority Queues</strong>: Building a heap from an array of tasks allows you to prioritize tasks efficiently. Priority queues often use heaps to offer quick access to the highest or lowest priority element.</li><li><strong>Heap Sort</strong>: The heap-building step is a fundamental part of the Heap Sort algorithm. By first building a max-heap, you can repeatedly extract the maximum element to sort an array in ascending order.</li><li><strong>Event Scheduling</strong>: Applications with event scheduling can benefit from heaps to quickly determine the next event based on priority or timing.</li></ol><h3>Heap Sort</h3><p>Heap Sort is a comparison-based sorting algorithm that leverages the heap data structure to sort an array efficiently. By building a max-heap (or min-heap) and repeatedly extracting the root element, we can place the largest (or smallest) element at the end of the array and sort the list in ascending (or descending) order.</p><h4>How Heaps Can Be Used for Sorting</h4><ol><li><strong>Build a Heap</strong>: Convert the unsorted array into a max-heap (for ascending order) or a min-heap (for descending order). This ensures that the root of the heap is always the largest or smallest element.</li><li><strong>Extract the Root</strong>: Swap the root (max or min) with the last element in the heap and reduce the heap’s size.</li><li><strong>Re-Heapify</strong>: After each swap, apply heapify to maintain the heap property.</li><li><strong>Repeat</strong>: Continue extracting the root and heapifying until all elements are sorted.</li></ol><p>In the end, this procedure leaves a sorted array without requiring additional data structures.</p><h4>Step-by-Step Guide to the Heap Sort Algorithm</h4><ol><li><strong>Build the Max-Heap</strong>: Start by applying the bottom-up heapify process to create a max-heap. This step ensures that the largest element is at the root.</li><li><strong>Extract the Root and Re-Heapify</strong>: Swap the root element with the last element in the array, effectively removing it from the heap. Then, heapify the root to restore the max-heap property.</li><li><strong>Repeat</strong>: Continue until the heap’s size is reduced to one.</li></ol><p>Here’s the code for Heap Sort:</p><pre>def heapify(arr, n, i):<br>    largest = i  # Initialize largest as root<br>    left = 2 * i + 1  # Left child index<br>    right = 2 * i + 2  # Right child index<br><br>    # Check if left child exists and is greater than root<br>    if left &lt; n and arr[left] &gt; arr[largest]:<br>        largest = left<br><br>    # Check if right child exists and is greater than largest so far<br>    if right &lt; n and arr[right] &gt; arr[largest]:<br>        largest = right<br><br>    # Swap and continue heapifying if root is not largest<br>    if largest != i:<br>        arr[i], arr[largest] = arr[largest], arr[i]<br>        heapify(arr, n, largest)<br><br>def heap_sort(arr):<br>    n = len(arr)<br><br>    # Step 1: Build a max-heap<br>    for i in range(n // 2 - 1, -1, -1):<br>        heapify(arr, n, i)<br><br>    # Step 2: Extract elements from heap one by one<br>    for i in range(n - 1, 0, -1):<br>        # Move current root to end<br>        arr[i], arr[0] = arr[0], arr[i]<br>        # Call max-heapify on the reduced heap<br>        heapify(arr, i, 0)<br><br># Example usage:<br>arr = [4, 10, 3, 5, 1]<br>heap_sort(arr)<br># After heap_sort, arr should be [1, 3, 4, 5, 10]</pre><h4>Time and Space Complexity of Heap Sort</h4><p><strong>Time Complexity</strong>:</p><ul><li>Building the heap: O(n)</li><li>Extracting elements (repeatedly heapifying): O(n log n)</li><li>Overall: <strong>O(n log n)</strong></li></ul><p><strong>Space Complexity</strong>:</p><ul><li>Heap Sort operates <strong>in-place</strong> with O(1) auxiliary space, as it does not require additional data structures.</li></ul><h4>Comparison with Other Sorting Algorithms</h4><p><strong>Quicksort</strong>:</p><ul><li><strong>Average Time Complexity</strong>: O(n log n)</li><li><strong>Worst Time Complexity</strong>: O(n^2) (for highly unbalanced partitions)</li><li><strong>Space Complexity</strong>: O(log n) to O(n) for recursive stack space</li><li><strong>Comparison</strong>: Quicksort is often faster for most data sets due to better cache performance, but Heap Sort offers a more consistent O(n log n) in worst-case scenarios.</li></ul><p><strong>Merge Sort</strong>:</p><ul><li><strong>Time Complexity</strong>: O(n log n) (worst, average, and best case)</li><li><strong>Space Complexity</strong>: O(n) (requires additional storage for merging)</li><li><strong>Comparison</strong>: Merge Sort is stable and optimal for linked lists, while Heap Sort is in-place but less cache-friendly.</li></ul><h3>Conclusion</h3><p>In this article, we’ve explored heaps in detail, from understanding the structure and types of heaps to mastering heap operations like insertion, deletion, heapify, and heap sort. We discussed how heaps can be efficiently represented as arrays, enabling memory-efficient storage and fast indexing. Through the heap sort algorithm, we saw a practical application of heaps in sorting data in O(n log n) time.</p><p>Heaps are invaluable in solving problems that require quick access to minimum or maximum elements, making them essential in scenarios like priority queues, scheduling algorithms, and pathfinding. Practicing heap-based problems will reinforce the concepts and help you become more confident with heap operations.</p><p>With heaps appearing frequently in coding interviews and being widely used in real-world applications, building a strong understanding of this data structure can be highly beneficial. Embrace the challenges of heap problems, and enjoy the efficiency they bring to data management and algorithm design!</p><h3>In Plain English 🚀</h3><p><em>Thank you for being a part of the </em><a href="https://plainenglish.io/"><strong><em>In Plain English</em></strong></a><em> community! Before you go:</em></p><ul><li>Be sure to <strong>clap</strong> and <strong>follow</strong> the writer ️👏<strong>️️</strong></li><li>Follow us: <a href="https://x.com/inPlainEngHQ"><strong>X</strong></a> | <a href="https://www.linkedin.com/company/inplainenglish/"><strong>LinkedIn</strong></a> | <a href="https://www.youtube.com/channel/UCtipWUghju290NWcn8jhyAw"><strong>YouTube</strong></a> | <a href="https://discord.gg/in-plain-english-709094664682340443"><strong>Discord</strong></a> | <a href="https://newsletter.plainenglish.io/"><strong>Newsletter</strong></a> | <a href="https://open.spotify.com/show/7qxylRWKhvZwMz2WuEoua0"><strong>Podcast</strong></a></li><li><a href="https://differ.blog/"><strong>Create a free AI-powered blog on Differ.</strong></a></li><li>More content at <a href="https://plainenglish.io/"><strong>PlainEnglish.io</strong></a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0275c6d32769" width="1" height="1" alt=""><hr><p><a href="https://python.plainenglish.io/lets-learn-heap-in-an-easy-way-0275c6d32769">Lets Learn Heap In an Easy Way</a> was originally published in <a href="https://python.plainenglish.io/">Python in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Learn Java for Automation: Beginner to Advanced]]></title>
            <link>https://medium.com/@kapilbhardwaj850/learn-java-for-automation-beginner-to-advanced-e5b999c34ccb?source=rss-bc8547726b7d------2</link>
            <guid isPermaLink="false">https://medium.com/p/e5b999c34ccb</guid>
            <dc:creator><![CDATA[Kapil Bhardwaj]]></dc:creator>
            <pubDate>Wed, 02 Oct 2024 09:38:34 GMT</pubDate>
            <atom:updated>2024-10-02T09:38:34.121Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*SCmGjZ-rU5eOVk8D" /><figcaption>Photo by <a href="https://unsplash.com/@possessedphotography?utm_source=medium&amp;utm_medium=referral">Possessed Photography</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Automation has become a core part of software development, helping to reduce repetitive tasks, improve efficiency, and ensure quality. Java, known for its platform independence, robustness, and vast ecosystem, is widely used in the automation space, from web automation with Selenium to API testing with tools like RestAssured. In this comprehensive guide, we’ll walk through Java from the basics to advanced topics, focusing on automation.</p><h3>I. Introduction</h3><h4>Overview of Java for Automation</h4><p>Automation involves using scripts to perform tasks that would otherwise require human intervention, such as running test cases, interacting with web elements, or validating APIs. Java’s popularity in automation is due to:</p><ul><li><strong>Strong Object-Oriented Programming (OOP)</strong>: Java’s OOP principles make code modular and reusable, which is crucial in large automation projects.</li><li><strong>Rich Ecosystem</strong>: Libraries like Selenium for web automation, RestAssured for API testing, and frameworks like TestNG and JUnit make Java ideal for various types of automation.</li><li><strong>Cross-Platform Compatibility</strong>: Java’s “Write Once, Run Anywhere” nature allows automation scripts to run across different environments.</li></ul><p>Let’s dive into Java automation, starting from the basics!</p><h3>II. Getting Started with Java (Beginner Level)</h3><h4>A. Basics of Java Programming</h4><p>Before automating anything, you need a solid foundation in Java programming.</p><p><strong>1. Setting Up Your Development Environment</strong></p><p>To start coding in Java:</p><ul><li><strong>Download and Install JDK</strong>: You need the Java Development Kit (JDK). Download it from <a href="https://www.oracle.com/java/technologies/javase-downloads.html">Oracle’s website</a>.</li><li><strong>Install an IDE</strong>: An IDE like IntelliJ IDEA or Eclipse helps manage and run Java projects efficiently.</li></ul><p><strong>2. Your First Java Program: Hello World</strong></p><p>Let’s begin with a simple Java program that prints “Hello World!” to the console.</p><pre>public class HelloWorld {<br>    public static void main(String[] args) {<br>        System.out.println(&quot;Hello, World!&quot;);<br>    }<br>}</pre><p><strong>Explanation</strong>:</p><ul><li>public class HelloWorld: Defines a class named HelloWorld.</li><li>public static void main(String[] args): The main method is the entry point of any Java application.</li><li>System.out.println(&quot;Hello, World!&quot;);: This prints text to the console.</li></ul><h4>B. Core Concepts in Java</h4><p>Understanding the basic concepts of Java is key before automating tasks.</p><p><strong>1. Variables and Data Types</strong></p><p>Variables are containers for storing data. Java has different data types such as int (integer), String (text), boolean (true/false), etc.</p><pre>public class CoreConcepts {<br>    public static void main(String[] args) {<br>        int numberOfTests = 5; // Integer type<br>        String testName = &quot;Login Automation&quot;; // String type<br>        boolean isTestPassed = true; // Boolean type<br>        <br>        System.out.println(&quot;Running &quot; + numberOfTests + &quot; tests for &quot; + testName);<br>    }<br>}</pre><ul><li><strong>Explanation</strong>: Variables are used to store the number of tests, the test name, and the result status. The values are then printed out.</li></ul><p><strong>2. Control Flow (Conditionals and Loops)</strong></p><p>Automation often involves decision-making and repetition, which can be achieved through control structures like conditionals (if-else) and loops (for, while).</p><pre>public class ControlFlow {<br>    public static void main(String[] args) {<br>        int attempts = 0;<br><br>        while (attempts &lt; 3) {<br>            System.out.println(&quot;Attempt &quot; + (attempts + 1));<br>            attempts++;<br>        }<br>    }<br>}</pre><ul><li><strong>Explanation</strong>: The while loop repeats the block of code until the number of attempts reaches 3. Loops like this are used in automation to retry tasks.</li></ul><h4>C. Object-Oriented Programming (OOP)</h4><p>Java’s OOP principles are essential in building modular, reusable automation frameworks.</p><p><strong>1. Classes and Objects</strong></p><p>A class is a blueprint for objects. In automation, classes can represent test cases or components of a web application.</p><pre>class TestAutomation {<br>    String testName;<br><br>    TestAutomation(String name) {<br>        this.testName = name;<br>    }<br><br>    void runTest() {<br>        System.out.println(testName + &quot; is running...&quot;);<br>    }<br>}<br><br>public class OOPExample {<br>    public static void main(String[] args) {<br>        TestAutomation loginTest = new TestAutomation(&quot;Login Test&quot;);<br>        loginTest.runTest();<br>    }<br>}</pre><ul><li><strong>Explanation</strong>: The TestAutomation class defines the structure for a test. An object loginTest is created with the name &quot;Login Test&quot; and then executed.</li></ul><p><strong>2. Inheritance, Polymorphism, Encapsulation, and Abstraction</strong></p><p>These OOP principles allow the creation of reusable and extendable test frameworks. Let’s look at an example of inheritance:</p><pre>class TestAutomation {<br>    String testName;<br><br>    TestAutomation(String name) {<br>        this.testName = name;<br>    }<br><br>    void runTest() {<br>        System.out.println(testName + &quot; is running...&quot;);<br>    }<br>}<br><br>class WebTest extends TestAutomation {<br>    WebTest(String name) {<br>        super(name);<br>    }<br><br>    void setupBrowser() {<br>        System.out.println(&quot;Setting up browser for &quot; + testName);<br>    }<br>}<br><br>public class OOPInheritanceExample {<br>    public static void main(String[] args) {<br>        WebTest loginTest = new WebTest(&quot;Login Test&quot;);<br>        loginTest.setupBrowser();<br>        loginTest.runTest();<br>    }<br>}</pre><ul><li><strong>Explanation</strong>: The WebTest class extends TestAutomation, inheriting its properties and adding a browser setup step.</li></ul><h3>III. Intermediate Java for Automation</h3><h4>A. Working with Libraries and Frameworks</h4><p>Automation tasks are made easier using libraries like <strong>Selenium</strong> for web automation, <strong>RestAssured</strong> for API testing, and frameworks like <strong>TestNG</strong> or <strong>JUnit</strong>.</p><p><strong>1. Dependency Management with Maven</strong></p><p>Maven is a build tool that manages project dependencies. Here’s how you add Selenium to your project using Maven.</p><pre>&lt;dependencies&gt;<br>    &lt;dependency&gt;<br>        &lt;groupId&gt;org.seleniumhq.selenium&lt;/groupId&gt;<br>        &lt;artifactId&gt;selenium-java&lt;/artifactId&gt;<br>        &lt;version&gt;4.0.0&lt;/version&gt;<br>    &lt;/dependency&gt;<br>&lt;/dependencies&gt;</pre><ul><li><strong>Explanation</strong>: This XML snippet in pom.xml allows Maven to download Selenium libraries for browser automation.</li></ul><h4>B. File Handling and I/O</h4><p>File handling is essential for reading input data (e.g., test data from CSV files) or writing test results to reports.</p><p><strong>Reading and Writing Files</strong></p><pre>import java.io.FileWriter;<br>import java.io.IOException;<br><br>public class FileHandlingExample {<br>    public static void main(String[] args) {<br>        try {<br>            FileWriter writer = new FileWriter(&quot;test-output.txt&quot;);<br>            writer.write(&quot;Automation test results...&quot;);<br>            writer.close();<br>        } catch (IOException e) {<br>            e.printStackTrace();<br>        }<br>    }<br>}</pre><ul><li><strong>Explanation</strong>: This example writes test results to a file, simulating a reporting mechanism in an automation project. Handling files programmatically allows you to log automation results in various formats (e.g., text, CSV).</li></ul><h4>C. Exception Handling</h4><p>Automation scripts must handle errors gracefully to ensure tests don’t fail without proper logging. Java’s try-catch blocks are used for this.</p><pre>public class ExceptionHandling {<br>    public static void main(String[] args) {<br>        try {<br>            int result = 10 / 0; // This will throw an ArithmeticException<br>        } catch (ArithmeticException e) {<br>            System.out.println(&quot;Error: Division by zero!&quot;);<br>        }<br>    }<br>}</pre><ul><li><strong>Explanation</strong>: The try block contains code that may throw an exception, and the catch block handles the error. Exception handling is critical for managing unexpected issues in automation, such as failed web page interactions or API timeouts.</li></ul><h4>D. Multithreading in Automation</h4><p>In large test suites, running tests concurrently can save time. Java supports multithreading, which allows parallel execution of code.</p><pre>public class ParallelTests extends Thread {<br>    public void run() {<br>        System.out.println(&quot;Running test: &quot; + Thread.currentThread().getId());<br>    }<br><br>    public static void main(String[] args) {<br>        ParallelTests test1 = new ParallelTests();<br>        ParallelTests test2 = new ParallelTests();<br>        <br>        test1.start(); // Start thread 1<br>        test2.start(); // Start thread 2<br>    }<br>}</pre><ul><li><strong>Explanation</strong>: This example demonstrates parallel execution by running two tests simultaneously using threads. Multithreading is particularly useful when executing multiple test cases that are independent of each other.</li></ul><h3>IV. Advanced Java for Automation</h3><h4>A. Java Reflection for Dynamic Test Cases</h4><p>Reflection allows Java programs to inspect or modify the behavior of classes at runtime. This is useful for building generic and flexible automation frameworks.</p><pre>import java.lang.reflect.Method;<br><br>public class ReflectionExample {<br>    public static void main(String[] args) throws Exception {<br>        Class&lt;?&gt; cls = Class.forName(&quot;TestAutomation&quot;);<br>        Method method = cls.getDeclaredMethod(&quot;runTest&quot;);<br>        method.invoke(cls.newInstance());<br>    }<br>}</pre><ul><li><strong>Explanation</strong>: Reflection is used here to dynamically invoke the runTest method from the TestAutomation class at runtime. Reflection helps in scenarios where you need to build automation frameworks that can adapt to changes dynamically, without hardcoding test cases.</li></ul><h4>B. Building a Test Automation Framework (TestNG)</h4><p>TestNG is a powerful testing framework used to structure automation tests effectively.</p><p><strong>1. Setting Up TestNG</strong></p><p>Add TestNG as a dependency in Maven:</p><pre>&lt;dependency&gt;<br>    &lt;groupId&gt;org.testng&lt;/groupId&gt;<br>    &lt;artifactId&gt;testng&lt;/artifactId&gt;<br>    &lt;version&gt;7.4.0&lt;/version&gt;<br>    &lt;scope&gt;test&lt;/scope&gt;<br>&lt;/dependency&gt;</pre><p><strong>2. Writing Test Cases in TestNG</strong></p><p>TestNG allows for writing automated test cases with annotations like @Test, @BeforeMethod, @AfterMethod.</p><pre>import org.testng.annotations.Test;<br>import org.testng.annotations.BeforeMethod;<br>import org.testng.annotations.AfterMethod;<br><br>public class TestNGExample {<br>    @BeforeMethod<br>    public void setup() {<br>        System.out.println(&quot;Setting up test environment...&quot;);<br>    }<br><br>    @Test<br>    public void testLogin() {<br>        System.out.println(&quot;Executing login test...&quot;);<br>    }<br><br>    @AfterMethod<br>    public void tearDown() {<br>        System.out.println(&quot;Cleaning up after test...&quot;);<br>    }<br>}</pre><ul><li><strong>Explanation</strong>: @BeforeMethod runs before each test case, @AfterMethod runs after each test, and the @Test method contains the actual test logic. TestNG makes organizing and running automation tests simple and efficient.</li></ul><h3>V. Conclusion</h3><p>Java is a powerful language for automation due to its rich ecosystem, robust performance, and platform independence. From basic programming concepts to advanced multithreading and reflection, Java equips you with the tools needed to automate anything from web testing with Selenium to API testing with RestAssured.</p><p>Whether you are just starting or looking to build complex automation frameworks, Java offers a solid foundation for crafting efficient and scalable automation solutions.</p><p>Now that you have a complete roadmap, it’s time to start automating!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e5b999c34ccb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Medium-Level Problems To Master Array]]></title>
            <link>https://python.plainenglish.io/medium-level-problems-to-master-array-0a37be697626?source=rss-bc8547726b7d------2</link>
            <guid isPermaLink="false">https://medium.com/p/0a37be697626</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[education]]></category>
            <dc:creator><![CDATA[Kapil Bhardwaj]]></dc:creator>
            <pubDate>Mon, 23 Sep 2024 07:17:29 GMT</pubDate>
            <atom:updated>2024-09-23T07:17:29.357Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*6U3JSuoN47b-UviE" /><figcaption>Photo by <a href="https://unsplash.com/@kellysikkema?utm_source=medium&amp;utm_medium=referral">Kelly Sikkema</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Arrays are one of the most fundamental data structures in programming. They form the building blocks for solving a wide range of computational problems, from simple tasks like finding the sum of elements to complex challenges involving dynamic programming and sorting. Mastering arrays at an intermediate level is crucial for developers who want to strengthen their problem-solving skills and excel in coding interviews.</p><p>In this article, we’ll explore medium-level array problems, providing you with the practice needed to hone your algorithmic thinking and approach common array challenges with confidence. Whether you’re preparing for interviews or simply looking to improve your coding skills, these problems will help you navigate the intricacies of arrays with ease.</p><h3>Problem 1: <strong>Two Sum : Check if a pair with given sum exists in Array</strong></h3><p><strong>Problem Statement:</strong> Given an array of integers arr[] and an integer target.</p><p><strong>1st variant: </strong>Return <strong>YES</strong> if there exist two numbers such that their sum is equal to the target. Otherwise, return <strong>NO.</strong></p><p><strong>2nd variant: </strong>Return indices of the two numbers such that their sum is equal to the target. Otherwise, we will return {-1, -1}.</p><p><strong>Note:</strong> You are not allowed to use the same element twice. Example: If the target is equal to 6 and num[1] = 3, then nums[1] + nums[1] = target is not a solution.</p><p><strong>Examples</strong>:</p><pre>Input Format: N = 5, arr[] = {2,6,5,8,11}, target = 14<br>Result: YES (for 1st variant)<br> [1, 3] (for 2nd variant)<br>Explanation: arr[1] + arr[3] = 14. So, the answer is &quot;YES&quot; for the first variant and [1, 3] for 2nd variant.</pre><p><strong>Solution:</strong></p><pre>def two_sum(arr, target): <br>    if len(arr) &lt;= 1:  # Handle edge case for an array with 1 or fewer elements<br>        return []<br>    <br>    left = 0<br>    right = len(arr) - 1<br>    <br>    while left &lt; right:<br>        current_sum = arr[left] + arr[right]<br>        if current_sum == target:<br>            return (left,right)  # Return the pair as a tuple<br>        elif current_sum &gt; target:<br>            right -= 1  # Move the right pointer to the left<br>        else:<br>            left += 1  # Move the left pointer to the right<br><br>    return []  # Return an empty list if no pair is found<br><br><br>array= [2,6,5,8,11]<br>target=14<br><br>answer=two_sum(array,target)<br>print(answer)</pre><h3>Problem 2: <strong>Sort an array of 0s, 1s and 2s</strong></h3><p><strong>Problem Statement:</strong> Given an array consisting of only 0s, 1s, and 2s. Write a program to in-place sort the array without using inbuilt sort functions.</p><p>( Expected: Single pass-O(N) and constant space)</p><p><strong>Examples:</strong></p><pre>nums = [2,0,2,1,1,0]<br>Output: [0,0,1,1,2,2]</pre><p><strong>Solution:</strong></p><pre>def sort_array(arr):<br>    # Traverse from the second element to the last<br>    for i in range(1, len(arr)):<br>        key = arr[i]<br>        j = i - 1<br><br>        # Move elements of arr[0..i-1], that are greater than key,<br>        # to one position ahead of their current position<br>        while j &gt;= 0 and arr[j] &gt; key:<br>            arr[j + 1] = arr[j]<br>            j -= 1<br><br>        # Insert the key at the correct position<br>        arr[j + 1] = key<br><br>    return arr<br><br># Example usage<br>arr = [0,2,2,1,1,0]<br>sorted_arr = sort_array(arr)<br>print(&quot;Sorted array:&quot;, sorted_arr)</pre><h3>Problem 3: Find the Majority Element that occurs more than N/2 times</h3><p><strong>Problem Statement:</strong> Given an array of <strong>N integers</strong>, write a program to return an element that occurs more than <strong>N/2 </strong>times in the given array. You may consider that such an element always exists in the array.</p><p><strong>Example</strong>:</p><pre>Example:<br>Input Format<br>: N = 3, nums[] = {3,2,3}<br>Result<br>: 3</pre><p><strong>Solution:</strong></p><pre>def find_elements(arr):<br>    target = len(arr) / 2  # Calculate N/2<br>    freq_dict = {}<br>    <br>    for i in arr:<br>        if i in freq_dict:<br>            freq_dict[i] += 1  # Increment count if element already exists<br>        else:<br>            freq_dict[i] = 1  # Initialize count for new element<br>    <br>    # Find elements with frequency greater than N/2<br>    target_frequency = [num for num, count in freq_dict.items() if count &gt; target]<br>    <br>    return target_frequency<br><br># Example usage<br>array = [3,2,3]    <br>elements = find_elements(array)<br>print(elements)</pre><h3>Problem 4:<strong>Kadane’s Algorithm : Maximum Subarray Sum in an Array</strong></h3><p><strong>Problem Statement</strong>: Given an integer array arr, find the contiguous subarray (containing at least one number) which<br>has the largest sum and returns its sum and prints the subarray</p><p><strong>Example</strong>:</p><pre>Example:<br>Input:<br> arr = [-2,1,-3,4,-1,2,1,-5,4] <br><br>Output:<br> 6 </pre><p><strong>Solution:</strong></p><pre>def kadane_algo(arr):<br>    max_number= float(&#39;-inf&#39;)<br>    max_end_number=0<br>    for x in arr:<br>        max_end_number+=x<br>        if  max_number&lt;max_end_number:<br>            max_number=max_end_number<br>        if max_end_number&lt;0:<br>            max_end_number=0<br>    return max_number        <br><br># Example usage<br>array = [-2,1,-3,4,-1,2,1,-5,4]    <br>elements = kadane_algo(array)<br>print(elements)</pre><h3>Problem 5: <strong>Stock Buy And Sell</strong></h3><p><strong>Problem Statement:</strong> You are given an array of prices where prices[i] is the price of a given stock on an ith day.</p><p>You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock. Return <em>the maximum profit you can achieve from this transaction</em>. If you cannot achieve any profit, return 0.</p><p><strong>Example:</strong></p><pre>Example:<br>Input:<br> prices = [7,1,5,3,6,4]<br>Output:<br> 5<br>Explanation:<br> Buy on day 2 (price = 1) and <br>sell on day 5 (price = 6), profit = 6-1 = 5.</pre><p><strong>Solution:</strong></p><pre>def maxProfit(prices):<br>        if len(prices)==0 : return 0<br>        else:<br>            profit=0<br>            minbuy=prices[0]<br>            for i in range(len(prices)):<br>                profit=max(prices[i]-minbuy,profit)<br>                minbuy=min(prices[i],minbuy)<br>            return profit  <br>    <br>    <br>array=[7,1,5,3,6,4]<br>find_profit= maxProfit(array)<br>print(find_profit)  </pre><h3>Problem 6:<strong>Rearrange Array Elements by Sign</strong></h3><p><strong>Problem Statement</strong>:</p><p>There’s an array ‘A’ of size ’N’ with an equal number of positive and negative elements. Without altering the relative order of positive and negative elements, you must return an array of alternately positive and negative values.</p><p><strong>Note: </strong>Start the array with positive elements.</p><p><strong>Examples:</strong></p><pre>Input:<br>arr[] = {1,2,-4,-5}, N = 4<br>Output:<br>1 -4 2 -5<br><br>Explanation: <br><br>Positive elements = 1,2<br>Negative elements = -4,-5<br>To maintain relative ordering, 1 must occur before 2, and -4 must occur before -5.</pre><p><strong>Solution:</strong></p><pre>   def print_order(arr):<br>    positive_numbers = []<br>    negative_numbers = []<br>    <br>    # Separate positive and negative numbers<br>    for i in range(len(arr)):<br>        if arr[i] &gt; 0:<br>            positive_numbers.append(arr[i])<br>        else:<br>            negative_numbers.append(arr[i])<br>    <br>    # Print alternating positive and negative numbers<br>    for j in range(min(len(positive_numbers), len(negative_numbers))):<br>        print(positive_numbers[j], end=&#39; &#39;)<br>        print(negative_numbers[j], end=&#39; &#39;)<br>    <br>    # If any remaining positive numbers<br>    for j in range(min(len(positive_numbers), len(negative_numbers)), len(positive_numbers)):<br>        print(positive_numbers[j], end=&#39; &#39;)<br>    <br>    # If any remaining negative numbers<br>    for j in range(min(len(positive_numbers), len(negative_numbers)), len(negative_numbers)):<br>        print(negative_numbers[j], end=&#39; &#39;)<br><br># Example array<br>array = [1, 2, -4, -5]<br><br># Call the function and print the result<br>order = print_order(array)<br><br># `print_order` does not return anything, so `order` will be `None`<br>print(order)  # This will print `None` because there&#39;s no return statement in `print_order()`</pre><h3>Problem 7: <strong>next_permutation : find next lexicographically greater permutation</strong></h3><p><strong>Problem Statement: </strong>Given an array Arr[] of integers, rearrange the numbers of the given array into the lexicographically next greater permutation of numbers.</p><p>If such an arrangement is not possible, it must rearrange to the lowest possible order (i.e., sorted in ascending order).</p><p>Example:</p><pre>Input format:<br> Arr[] = {1,3,2}<br>Output<br>: Arr[] = {2,1,3}<br>Explanation: <br>All permutations of {1,2,3} are {{1,2,3} , {1,3,2}, {2,13} , {2,3,1} , {3,1,2} , {3,2,1}}. So, the next permutation just after {1,3,2} is {2,1,3}.</pre><p>Solution:</p><pre>def find_permutations(arr):<br>    # Base case: If the array has only one element or is empty, return the array itself<br>    if len(arr) &lt;= 1:<br>        return [arr]  # Return as a list of lists<br>    <br>    pers = []  # List to store all permutations<br>    for i in range(len(arr)):<br>        # Get the rest of the list except the current element<br>        rest = arr[:i] + arr[i+1:]<br>        # Recursively find permutations of the rest of the list<br>        for perm in find_permutations(rest):<br>            pers.append([arr[i]] + perm)  # Add the current element to the front<br>    return pers # Return the full list of permutations<br><br># List of elements<br>elements = [1, 2, 3]<br><br># Find and print all permutations<br>for perm in find_permutations(elements):<br>    print(perm)</pre><h3>Problem 8: <strong>Leaders in an Array</strong></h3><p><strong>Problem Statement:</strong> Given an array, print all the elements which are leaders. A Leader is an element that is greater than all of the elements on its right side in the array.</p><p><strong>Example:</strong></p><pre>Example:<br>Input:<br><br> arr = [4, 7, 1, 0]<br>Output<br>:<br> 7 1 0<br>Explanation:<br><br> Rightmost element is always a leader. 7 and 1 are greater than the elements in their right side.</pre><p><strong>Solution:</strong></p><pre>def find_leaders(arr):<br>    ladder = []<br>    if len(arr) &lt;= 1:<br>        return arr<br>    <br>    # Start with the last element, it is always a leader<br>    current_element = arr[-1]<br>    ladder.append(current_element)<br><br>    # Traverse the array from second last to the first<br>    for i in range(len(arr) - 2, -1, -1):<br>        if arr[i] &gt; current_element:<br>            ladder.append(arr[i])<br>            current_element = arr[i]<br>    <br>    # Since we are collecting leaders from right to left, reverse the result<br>    return ladder[::-1]<br><br># Test with an example<br>arr = [4, 7, 1, 0]<br>leaders = find_leaders(arr)<br>print(leaders)  # Output should be [17, 5, 2]</pre><h3>Problem 9: <strong>Longest Consecutive Sequence in an Array</strong></h3><p><strong>Problem Statement: </strong>You are given an array of ’N’ integers. You need to find the length of the longest sequence which contains the consecutive elements.</p><p><strong>Examples</strong></p><pre>Example:<br>Input:<br> [100, 200, 1, 3, 2, 4]<br><br>Output:<br> 4<br><br>Explanation:<br> The longest consecutive subsequence is 1, 2, 3, and 4.</pre><p><strong>Solution:</strong></p><pre>def find_sequence(arr):<br>    if len(arr)&lt;=1:<br>        return arr<br>    arr.sort()<br>    max_length=1<br>    current_length=1<br>    for i in range(1,len(arr)):<br>        if arr[i]==arr[i-1]:<br>            continue<br>        elif arr[i]==arr[i-1]+1:<br>            current_length+=1<br>        else:<br>            max_length=max(max_length,current_length)<br>            current_length=1<br>    max_length=max(max_length,current_length)<br>    return max_length<br>    <br>arr = [100, 4, 200, 1, 3, 2]<br>print(find_sequence(arr))   </pre><h3>Problem 10: <strong>Set Matrix Zero</strong></h3><p><strong>Problem Statement:</strong> Given a matrix if an element in the matrix is 0 then you will have to set its entire column and row to 0 and then return the matrix.</p><p><strong>Example:</strong></p><pre>Examples:<br>Input: matrix=[[1,1,1],[1,0,1],[1,1,1]]<br><br>Output: [[1,0,1],[0,0,0],[1,0,1]]<br><br>Explanation: Since matrix[2][2]=0.Therfore the 2nd column and 2nd row wil be set to 0.</pre><p><strong>Solution:</strong></p><pre>def set_matrix(maxt):<br>    rows = len(maxt)<br>    cols = len(maxt[0]) if rows &gt; 0 else 0<br>    <br>    # Create sets to track which rows and columns need to be set to zero<br>    zero_rows = set()<br>    zero_cols = set()<br><br>    # First pass to find all the rows and columns that need to be set to zero<br>    for i in range(rows):<br>        for j in range(cols):<br>            if maxt[i][j] == 0:<br>                zero_rows.add(i)  # Add the row index<br>                zero_cols.add(j)  # Add the column index<br><br>    # Set the identified rows to zero<br>    for row in zero_rows:<br>        for j in range(cols):<br>            maxt[row][j] = 0<br><br>    # Set the identified columns to zero<br>    for col in zero_cols:<br>        for i in range(rows):<br>            maxt[i][col] = 0<br><br>    return maxt  # Return the modified matrix<br><br># Test with a matrix<br>matrix = [[1, 1, 1], [1, 0, 1], [1, 1, 1]]<br>modified_matrix = set_matrix(matrix)<br><br># Print the modified matrix<br>for row in modified_matrix:<br>    print(row)<br><br>        </pre><h3>Problem 11: <strong>Rotate Image by 90 degree</strong></h3><p><strong>Problem Statement: </strong>Given a matrix, your task is to rotate the matrix 90 degrees clockwise.</p><p>Example:</p><pre>Example:<br>Input:<br> [[1,2,3],[4,5,6],[7,8,9]]<br><br>Output<br>: [[7,4,1],[8,5,2],[9,6,3]]<br><br>Explanation:<br> Rotate the matrix simply by 90 degree clockwise and return the matrix.</pre><p>Solution:</p><pre>def rotate_matrix(matrix):<br>    if len(matrix) &lt;= 1:<br>        return matrix<br>    <br>    for i in range(len(matrix)):<br>        for j in range(i, len(matrix)):<br>            matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]<br>    <br>    for i in range(len(matrix)):<br>        matrix[i].reverse()<br>    <br>    return matrix<br><br>matrix = [<br>    [1, 2, 3],<br>    [4, 5, 6],<br>    [7, 8, 9]<br>]<br><br>rotated_matrix = rotate_matrix(matrix)<br><br>for row in rotated_matrix:<br>    print(row)</pre><h3>Problem 12: <strong>Spiral Traversal of Matrix</strong></h3><p><strong>Problem Statement: </strong>Given a Matrix, print the given matrix in spiral order.</p><p><strong>Examples:</strong></p><pre>Example:<br>Input: Matrix[][] = { { 1, 2, 3, 4 },<br>        { 5, 6, 7, 8 },<br>        { 9, 10, 11, 12 },<br>               { 13, 14, 15, 16 } }<br><br>Outhput: 1, 2, 3, 4, 8, 12, 16, 15, 14, 13, 9, 5, 6, 7, 11, 10.<br>Explanation: The output of matrix in spiral form.</pre><p><strong>Solution:</strong></p><pre>def spiral_order(matrix):<br>    result = []<br>    <br>    if not matrix or not matrix[0]:  # Return empty result if matrix is empty<br>        return result<br>    <br>    top, bottom = 0, len(matrix) - 1<br>    left, right = 0, len(matrix[0]) - 1<br>    <br>    while top &lt;= bottom and left &lt;= right:<br>        # Traverse from left to right along the top row<br>        for i in range(left, right + 1):<br>            result.append(matrix[top][i])<br>        top += 1<br>        <br>        # Traverse from top to bottom along the right column<br>        for i in range(top, bottom + 1):<br>            result.append(matrix[i][right])<br>        right -= 1<br>        <br>        if top &lt;= bottom:<br>            # Traverse from right to left along the bottom row<br>            for i in range(right, left - 1, -1):<br>                result.append(matrix[bottom][i])<br>            bottom -= 1<br>        <br>        if left &lt;= right:<br>            # Traverse from bottom to top along the left column<br>            for i in range(bottom, top - 1, -1):<br>                result.append(matrix[i][left])<br>            left += 1<br><br>    return result<br><br># Example matrix<br>matrix = [<br>    [1, 2, 3, 4],<br>    [5, 6, 7, 8],<br>    [9, 10, 11, 12],<br>    [13, 14, 15, 16]<br>]<br><br># Get the spiral order of the matrix<br>spiral = spiral_order(matrix)<br><br># Print the result<br>print(&quot;, &quot;.join(map(str, spiral)))</pre><h3><strong>Conclusion:</strong></h3><p>Mastering arrays is an essential skill for every developer, and medium-level problems are the perfect stepping stones to building that expertise. These problems not only enhance your ability to manipulate and analyze data efficiently but also improve your problem-solving mindset for tackling more complex coding challenges. By consistently practicing and understanding the underlying concepts of these array-based problems, you’ll be better equipped to handle real-world scenarios and coding interviews with confidence.</p><p>Remember, the key to mastering arrays — or any data structure — is practice and persistence. So, keep challenging yourself with new problems, and you’ll continue to grow as a developer.</p><h3>In Plain English 🚀</h3><p><em>Thank you for being a part of the </em><a href="https://plainenglish.io/"><strong><em>In Plain English</em></strong></a><em> community! Before you go:</em></p><ul><li>Be sure to <strong>clap</strong> and <strong>follow</strong> the writer ️👏<strong>️️</strong></li><li>Follow us: <a href="https://twitter.com/inPlainEngHQ"><strong>X</strong></a> | <a href="https://www.linkedin.com/company/inplainenglish/"><strong>LinkedIn</strong></a> | <a href="https://www.youtube.com/channel/UCtipWUghju290NWcn8jhyAw"><strong>YouTube</strong></a> | <a href="https://discord.gg/in-plain-english-709094664682340443"><strong>Discord</strong></a> | <a href="https://newsletter.plainenglish.io/"><strong>Newsletter</strong></a></li><li>Visit our other platforms: <a href="https://cofeed.app/"><strong>CoFeed</strong></a> | <a href="https://differ.blog/"><strong>Differ</strong></a></li><li>More content at <a href="https://plainenglish.io/"><strong>PlainEnglish.io</strong></a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0a37be697626" width="1" height="1" alt=""><hr><p><a href="https://python.plainenglish.io/medium-level-problems-to-master-array-0a37be697626">Medium-Level Problems To Master Array</a> was originally published in <a href="https://python.plainenglish.io/">Python in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Asynchronous Programming In JavaScript]]></title>
            <link>https://medium.com/@kapilbhardwaj850/asynchronous-programming-in-javascript-9aa8949faff8?source=rss-bc8547726b7d------2</link>
            <guid isPermaLink="false">https://medium.com/p/9aa8949faff8</guid>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[education]]></category>
            <dc:creator><![CDATA[Kapil Bhardwaj]]></dc:creator>
            <pubDate>Sun, 15 Sep 2024 18:57:16 GMT</pubDate>
            <atom:updated>2024-09-15T18:57:16.263Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Iu3PvJwS8DkI9Bwf" /><figcaption>Photo by <a href="https://unsplash.com/@growtika?utm_source=medium&amp;utm_medium=referral">Growtika</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>JavaScript is a <strong>single-threaded</strong> programming language, meaning it has a single call stack and can only execute one task at a time. This might seem limiting, especially when handling tasks that take a long time to complete, like fetching data from a server, waiting for user input, or processing large files. If JavaScript handled everything synchronously, tasks like these would block the execution of other code, causing the application to freeze or become unresponsive. However, JavaScript’s <strong>non-blocking</strong> nature allows it to handle long-running operations asynchronously, ensuring that other tasks can continue to execute without interruption.</p><h4>JavaScript’s Execution Model: Single-Threaded, Non-Blocking</h4><p>In a single-threaded environment, only one line of code is executed at a time. JavaScript’s execution model relies on a <strong>call stack</strong>, which keeps track of function executions. Each time a function is called, it is added to the stack. Once the function completes, it is removed from the stack.</p><p>However, not all tasks in JavaScript are instantaneous. For example, making a network request to fetch data from an API can take time due to factors like server response delays or slow internet connections. If JavaScript were to wait for this task to finish before moving to the next line of code (i.e., synchronous execution), it would block the rest of the program. This is where <strong>asynchronous programming</strong> comes into play.</p><p>JavaScript uses mechanisms like <strong>callbacks</strong>, <strong>promises</strong>, and <strong>async/await</strong> to handle these tasks asynchronously. When a time-consuming operation occurs, JavaScript doesn’t halt; it delegates the task to the <strong>event loop</strong> and <strong>web APIs</strong> (like setTimeout, fetch, or DOM events) to manage it in the background, allowing other code to continue running.</p><h4>Why Asynchronous Programming is Essential in JavaScript</h4><p>Asynchronous programming is vital in JavaScript because it enables <strong>non-blocking</strong> behavior, making applications more efficient and responsive. Here’s why it’s so important:</p><ol><li><strong>Performance</strong>: Without asynchronous programming, long-running operations (e.g., API requests, file I/O) would block the main thread, slowing down performance. Asynchronous programming allows other tasks to continue executing while waiting for slower operations to complete.</li><li><strong>User Experience</strong>: A responsive UI is crucial in web development. If an application freezes while waiting for a task to complete, it can lead to a poor user experience. Asynchronous programming ensures that the user interface remains interactive, even during background tasks.</li><li><strong>Concurrency</strong>: JavaScript can manage multiple tasks concurrently, even though it runs on a single thread. Asynchronous programming allows the JavaScript engine to handle multiple tasks simultaneously by delegating time-consuming tasks to web APIs, which manage them independently of the main thread.</li></ol><h4>Common Use Cases for Asynchronous Operations</h4><p>Asynchronous programming is used in a wide variety of scenarios in JavaScript, including:</p><ul><li><strong>API Calls (AJAX/Fetch)</strong>: Fetching data from a server is one of the most common asynchronous tasks. Since network requests can take time, handling them asynchronously prevents the entire application from stalling while waiting for the server to respond.</li><li><strong>Timers (setTimeout, setInterval)</strong>: Timers are used to delay the execution of code for a specified amount of time. Using setTimeout or setInterval asynchronously prevents blocking the main thread while waiting for the timer to finish.</li><li><strong>Event Listeners</strong>: Interactions with users, such as clicking a button or typing in a text field, are handled asynchronously using event listeners. This allows the application to respond to user inputs without affecting the rest of the code.</li><li><strong>File Handling</strong>: Reading or writing files can be a time-consuming operation, especially with large data sets. Asynchronous file handling ensures that these operations do not block other code execution.</li><li><strong>Database Operations</strong>: In full-stack JavaScript (e.g., Node.js), communicating with databases is a common task. Database queries, especially complex ones, can take time to complete, and asynchronous programming allows the application to remain responsive while waiting for the database to return results.</li></ul><h3>Synchronous vs. Asynchronous JavaScript</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/694/1*I_T8_VSCSEV4GVAV_heguA.png" /></figure><p>JavaScript is a <strong>single-threaded, non-blocking</strong> programming language. It executes one command at a time in a specific order, which we refer to as synchronous execution. However, JavaScript also supports asynchronous operations, which allows it to handle tasks like API calls, file reading, or timers without blocking the entire application. Understanding the difference between synchronous and asynchronous execution is crucial for mastering JavaScript.</p><h4>Synchronous JavaScript</h4><p>In synchronous JavaScript, tasks are executed one after another, in sequence. This means that JavaScript will not move to the next line of code until the current line has finished execution. This is often referred to as <strong>blocking behavior</strong>, as long-running operations can block subsequent code from executing.</p><p><strong>Example of Synchronous Code:</strong></p><pre>console.log(&#39;Start&#39;);<br><br>// Simulating a long-running task (blocking)<br>for (let i = 0; i &lt; 1000000000; i++) { }<br><br>console.log(&#39;End&#39;);</pre><p>In this example:</p><ul><li>The for loop simulates a long-running task.</li><li>The browser will execute the loop completely before moving to the next line, blocking the UI and potentially freezing the page.</li><li>The user can’t interact with the page until the loop finishes, which can cause a poor user experience.</li></ul><p><strong>The Problem with Synchronous Code:</strong></p><p>Synchronous JavaScript can create performance issues because it blocks the execution of other tasks, including UI updates. If a function or operation takes too long to complete (like waiting for a file to download or retrieving data from a server), the entire application may become unresponsive during that time.</p><p>For example, in a web application:</p><ul><li>A synchronous API call to fetch data would prevent the user from interacting with the page until the response is received.</li><li>A long-running loop or operation can freeze the interface, preventing animations or event handling like clicking buttons.</li></ul><p>This is where <strong>asynchronous programming</strong> comes into play.</p><h4>Introduction to Asynchronous Concepts in JavaScript</h4><p><strong>Asynchronous programming</strong> allows JavaScript to handle time-consuming tasks like network requests or reading files in the background, without blocking the main thread. When a task starts, JavaScript can continue executing other code while it waits for the asynchronous task to complete. Once the task finishes, JavaScript processes the result.</p><p>Asynchronous operations are essential for building responsive web applications. Some common scenarios include:</p><ul><li>Fetching data from a server</li><li>Reading or writing files</li><li>Setting timers (e.g., setTimeout)</li><li>Listening for events like clicks or keystrokes</li></ul><p><strong>Example of Asynchronous Code:</strong></p><pre>console.log(&#39;Start&#39;);<br><br>setTimeout(() =&gt; {<br>  console.log(&#39;This happens after 2 seconds&#39;);<br>}, 2000);<br><br>console.log(&#39;End&#39;);</pre><p>In this example:</p><ul><li>JavaScript schedules the setTimeout function to run after 2 seconds, but immediately continues to the next line.</li><li>The console.log(&#39;End&#39;) statement is executed right away, and only after the 2 seconds delay will &#39;This happens after 2 seconds&#39; be logged.</li><li>This allows JavaScript to remain non-blocking and handle other tasks in the meantime.</li></ul><h4>Event loop and callback queue</h4><p>JavaScript uses the <strong>event loop</strong> to manage asynchronous operations. The event loop enables JavaScript to handle tasks without blocking execution and makes sure that operations like I/O or timers are processed once the main code has finished running.</p><h4>Call Stack vs. Task Queue</h4><ul><li><strong>Call Stack</strong>: The call stack contains the list of functions that are being executed, one by one. When JavaScript encounters a function call, it pushes it to the top of the call stack and processes it. When the function finishes, it is popped off the stack.</li><li><strong>Task Queue (Callback Queue)</strong>: The task queue holds the functions (callbacks) that are waiting to be executed after asynchronous operations complete. The event loop moves functions from the task queue to the call stack when the call stack is empty.</li></ul><p><strong>Example of the Event Loop in Action:</strong></p><pre>console.log(&#39;Start&#39;);<br><br>setTimeout(() =&gt; {<br>  console.log(&#39;Async operation finished&#39;);<br>}, 2000);<br><br>console.log(&#39;End&#39;);</pre><p>In this example:</p><ol><li>console.log(&#39;Start&#39;) is pushed to the call stack and executed, logging &quot;Start&quot;.</li><li>The setTimeout function is encountered. It is passed to the Web APIs with a 2-second timer.</li><li>console.log(&#39;End&#39;) is added to the call stack and executed, logging &quot;End&quot;.</li><li>After 2 seconds, the setTimeout callback is moved to the callback queue.</li><li>The event loop checks the call stack, which is empty, so it pushes the callback to the stack.</li><li>&quot;Async operation finished&quot; is logged to the console.</li></ol><h3>The Event Loop: How JavaScript Handles Asynchronous Tasks</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/802/1*DvYUStI7fuhVdnjrWuNZSw.png" /></figure><h4>Explanation of the Event Loop</h4><p>JavaScript is single-threaded, meaning it can only execute one task at a time. This single-threaded nature might seem limiting, especially when working with tasks like network requests or file I/O, which can take an uncertain amount of time. However, JavaScript uses an <strong>event loop</strong> to handle asynchronous tasks efficiently without blocking the execution of other code.</p><p>The event loop is responsible for executing JavaScript code, collecting and processing events, and executing queued tasks. It ensures that asynchronous operations (e.g., fetching data from an API) are completed in the background, while the rest of the program continues executing.</p><p>Here’s how the event loop works:</p><ol><li><strong>Call Stack</strong>: This is where JavaScript tracks the currently executing function. Each time a function is called, it gets added to the call stack. When the function completes, it is removed from the stack.</li><li><strong>Web APIs</strong>: Functions like setTimeout, fetch, and event listeners (e.g., click) are managed by the browser&#39;s Web APIs. When an asynchronous operation is initiated, it is handled by these APIs in the background.</li><li><strong>Callback Queue (Task Queue)</strong>: Once an asynchronous operation (e.g., timer, API response) completes, its associated callback function is placed into the callback queue (or task queue).</li><li><strong>Event Loop</strong>: The event loop continuously checks whether the call stack is empty. If the stack is empty, the event loop takes a function from the callback queue and pushes it onto the call stack for execution.</li></ol><p><strong>Visualizing the Event Loop:</strong></p><ol><li>A synchronous function is executed and added to the call stack.</li><li>If an asynchronous function is encountered (e.g., setTimeout), the task is handed over to Web APIs, and JavaScript continues executing other code.</li><li>Once the asynchronous task completes (like the timer or an API response), its callback is added to the callback queue.</li><li>The event loop waits for the call stack to be empty before moving the callback from the queue to the stack for execution.</li></ol><h4>Microtasks vs. Macrotasks</h4><p>The <strong>Task Queue</strong> can be divided into two categories: <strong>microtasks</strong> and <strong>macrotasks</strong>. Understanding the difference between these two is crucial for mastering JavaScript’s asynchronous behavior.</p><ul><li><strong>Microtasks:</strong> These are tasks that need to be executed after the currently running JavaScript code completes but before any <strong>macrotasks</strong>. Promises and mutation observers are examples of microtasks.</li><li><strong>Macrotasks:</strong> These are tasks queued in response to user interactions, I/O operations (e.g., HTTP requests), or timers (e.g., setTimeout). Macrotasks are executed after microtasks are completed. Examples include setTimeout, setInterval, and event handling.</li></ul><p><strong>Order of Execution:</strong></p><ol><li>The event loop first checks the call stack for any pending functions.</li><li>Once the call stack is empty, it looks at the microtask queue and executes all microtasks.</li><li>After the microtasks are completed, the event loop processes one macrotask from the macrotask queue.</li><li>This process repeats continuously.</li></ol><h4>How the Call Stack, Task Queue, and Event Loop Work Together</h4><p>Here’s a step-by-step breakdown of how these components interact:</p><p><strong>Call Stack Execution:</strong></p><ul><li>JavaScript begins by placing code into the call stack.</li><li>Synchronous tasks (e.g., normal function calls) are executed one by one in the order they appear in the call stack.</li></ul><p><strong>Handling Asynchronous Tasks:</strong></p><ul><li>When JavaScript encounters an asynchronous operation (e.g., setTimeout or a network request), it delegates that task to the browser’s Web API.</li><li>The Web API runs the task in the background while JavaScript continues executing the remaining code.</li></ul><p><strong>Completion and Callback:</strong></p><ul><li>Once the asynchronous task completes, the Web API adds the corresponding callback function to the Task Queue (microtask queue for promises or macrotask queue for things like setTimeout).</li></ul><p><strong>Event Loop Coordination:</strong></p><ul><li>The event loop checks whether the call stack is empty.</li><li>If it is empty, the event loop first processes the microtask queue and executes all microtasks.</li><li>After that, it processes one macrotask and places it on the call stack.</li></ul><p>This order ensures that microtasks have higher priority than macrotasks.</p><h4>Visualizing the Event Loop (Example with Code and Diagrams)</h4><p>Let’s break it down with an example:</p><pre>console.log(&#39;Start&#39;);  // Synchronous - added to call stack and executed immediately<br><br>setTimeout(() =&gt; {<br>  console.log(&#39;Timeout Callback&#39;);  // Asynchronous - goes to macrotask queue<br>}, 0);<br><br>Promise.resolve().then(() =&gt; {<br>  console.log(&#39;Promise Resolved&#39;);  // Asynchronous - goes to microtask queue<br>});<br><br>console.log(&#39;End&#39;);  // Synchronous - added to call stack and executed immediately</pre><p>output:</p><pre>Start<br>End<br>Promise Resolved<br>Timeout Callback</pre><p><strong>Explanation:</strong></p><p><strong>Call Stack Execution:</strong></p><ul><li>The call stack first executes console.log(&#39;Start&#39;), and &quot;Start&quot; is printed.</li></ul><p><strong>Handling Asynchronous Tasks:</strong></p><ul><li>setTimeout() is encountered, which delegates the task to the Web API. The callback console.log(&#39;Timeout Callback&#39;) is placed in the macrotask queue.</li><li>Promise.resolve() is also encountered. Its callback console.log(&#39;Promise Resolved&#39;) is placed in the microtask queue.</li><li>console.log(&#39;End&#39;) is executed synchronously and &quot;End&quot; is printed.</li></ul><p><strong>Event Loop and Task Queue:</strong></p><ul><li>Once the call stack is empty, the event loop checks the <strong>microtask queue</strong> and executes console.log(&#39;Promise Resolved&#39;), printing &quot;Promise Resolved&quot;.</li><li>After all microtasks are done, the event loop processes the <strong>macrotask queue</strong>, executing console.log(&#39;Timeout Callback&#39;) and printing &quot;Timeout Callback&quot;.</li></ul><h4>Diagram:</h4><pre>Call Stack            | Microtask Queue   | Macrotask Queue<br>---------------------------------------------------------------<br>1. Start              |                   |<br>2. End                |                   |<br>---------------------------------------------------------------<br>|                     | Promise Resolved  | Timeout Callback<br>---------------------------------------------------------------<br>|                     | (Executed next)   | (Executed after microtasks)</pre><h3>Asynchronous Patterns in JavaScript</h3><p>JavaScript provides several patterns to handle asynchronous code, each offering a different approach to managing tasks that do not execute sequentially. The three main patterns you’ll encounter are callbacks, promises, and async/await.</p><h4>Callbacks</h4><p><strong>What are callbacks?</strong></p><p>Callbacks are functions passed as arguments to other functions and are executed once a task is completed. In JavaScript, callbacks were the earliest approach to handling asynchronous operations, like fetching data or waiting for a timer to finish. A callback is executed after an asynchronous operation finishes, allowing the next block of code to run without blocking the thread.</p><p>For example:</p><pre>function fetchData(callback) {<br>    setTimeout(() =&gt; {<br>        console.log(&quot;Data fetched&quot;);<br>        callback(); // Execute callback after the task is done<br>    }, 2000);<br>}<br><br>function processData() {<br>    console.log(&quot;Processing data&quot;);<br>}<br><br>fetchData(processData);</pre><p><strong>Callback Hell (The “Pyramid of Doom”)</strong></p><p>As applications grow, you might find yourself with nested callbacks, which can make code hard to read and maintain. This is known as “callback hell” or the “Pyramid of Doom” because of the deeply nested structure of the code.</p><p>Example of callback hell:</p><pre>setTimeout(() =&gt; {<br>    console.log(&quot;Task 1 completed&quot;);<br>    setTimeout(() =&gt; {<br>        console.log(&quot;Task 2 completed&quot;);<br>        setTimeout(() =&gt; {<br>            console.log(&quot;Task 3 completed&quot;);<br>            // and so on...<br>        }, 1000);<br>    }, 1000);<br>}, 1000);</pre><p>While callbacks work, this pattern can quickly become unmanageable when dealing with multiple asynchronous operations. This leads us to the next evolution: promises.</p><h4>Promises</h4><p><strong>Introduction to Promises</strong></p><p>Promises were introduced to solve the issue of callback hell and make asynchronous code easier to read and maintain. A promise is an object that represents the eventual completion (or failure) of an asynchronous operation. Instead of passing callbacks, a promise allows you to attach then and catch handlers to handle the successful or unsuccessful completion of an operation.</p><p><strong>States of a Promise:</strong></p><p>A promise can be in one of three states:</p><ol><li><strong>Pending</strong>: The initial state, before the operation has completed.</li><li><strong>Fulfilled</strong>: The operation completed successfully.</li><li><strong>Rejected</strong>: The operation failed.</li></ol><p><strong>Creating and Using Promises (</strong><strong>then, </strong><strong>catch, </strong><strong>finally)</strong></p><p>Here’s how you create a simple promise:</p><pre>let promise = new Promise((resolve, reject) =&gt; {<br>    let success = true;<br>    if (success) {<br>        resolve(&quot;Operation successful&quot;);<br>    } else {<br>        reject(&quot;Operation failed&quot;);<br>    }<br>});<br><br>// Using the promise<br>promise<br>    .then(result =&gt; {<br>        console.log(result); // Logs &quot;Operation successful&quot;<br>    })<br>    .catch(error =&gt; {<br>        console.error(error); // Catches any errors<br>    })<br>    .finally(() =&gt; {<br>        console.log(&quot;Promise settled (either fulfilled or rejected)&quot;);<br>    });</pre><ul><li><strong>then()</strong> is used to handle the resolved state (i.e., when the promise is fulfilled).</li><li><strong>catch()</strong> is used to handle errors (i.e., when the promise is rejected).</li><li><strong>finally()</strong> is always executed, regardless of whether the promise was fulfilled or rejected, useful for cleanup tasks.</li></ul><p><strong>Chaining Promises and Handling Errors</strong></p><p>One of the strengths of promises is their ability to be chained. Each then() method returns a new promise, allowing you to chain asynchronous operations together in a more readable manner than nested callbacks.</p><pre>let fetchData = new Promise((resolve, reject) =&gt; {<br>    setTimeout(() =&gt; {<br>        resolve(&quot;Data fetched&quot;);<br>    }, 1000);<br>});<br><br>fetchData<br>    .then(result =&gt; {<br>        console.log(result); // Logs &quot;Data fetched&quot;<br>        return &quot;Processing data&quot;;<br>    })<br>    .then(nextResult =&gt; {<br>        console.log(nextResult); // Logs &quot;Processing data&quot;<br>    })<br>    .catch(error =&gt; {<br>        console.error(error); // Handles any errors from the entire chain<br>    });</pre><p>By chaining promises, asynchronous operations can be handled sequentially in a clean, linear way. Errors that occur anywhere in the chain can be caught by the catch block.</p><h4>Async/Await</h4><p><strong>Introduction to </strong><strong>async and </strong><strong>await</strong></p><p>Although promises made asynchronous code easier to manage than callbacks, chaining many promises could still become complex. To further simplify asynchronous code, ES2017 introduced async and await. These keywords allow you to write asynchronous code that looks synchronous, making it easier to follow and maintain.</p><ul><li><strong>async</strong>: Declares a function as asynchronous and ensures it returns a promise.</li><li><strong>await</strong>: Pauses the execution of an asynchronous function until the promise resolves or rejects.</li></ul><p>Example:</p><pre>async function fetchData() {<br>    let promise = new Promise((resolve, reject) =&gt; {<br>        setTimeout(() =&gt; resolve(&quot;Data fetched&quot;), 1000);<br>    });<br><br>    let result = await promise; // Waits for the promise to resolve<br>    console.log(result); // Logs &quot;Data fetched&quot;<br>}<br><br>fetchData();</pre><p>In the example, await pauses the execution of the fetchData function until the promise resolves, at which point the value is stored in result. Unlike promises or callbacks, async/await makes asynchronous code look more like synchronous code, improving readability.</p><p><strong>Error Handling in </strong><strong>async/await (</strong><strong>try...catch)</strong></p><p>Handling errors in async/await is as simple as using a try...catch block. This is an improvement over the need for a .catch() handler in promises.</p><pre>async function fetchData() {<br>    try {<br>        let promise = new Promise((resolve, reject) =&gt; {<br>            setTimeout(() =&gt; reject(&quot;Error fetching data&quot;), 1000);<br>        });<br><br>        let result = await promise;<br>        console.log(result);<br>    } catch (error) {<br>        console.error(error); // Catches the rejected promise<br>    }<br>}<br><br>fetchData();</pre><h4>The unhandledrejection Event</h4><p>If a promise is rejected and there is no error handling (catch block or try...catch for async/await), the error will go unhandled. To catch these unhandled rejections, JavaScript provides the unhandledrejection event.</p><p><strong>Example:</strong></p><pre>window.addEventListener(&#39;unhandledrejection&#39;, function (event) {<br>  console.error(&#39;Unhandled promise rejection:&#39;, event.reason);<br>});</pre><p>This is particularly useful in large applications where not all promise rejections are handled explicitly, as it provides a way to log or handle global errors.</p><h3>Parallel and Sequential Execution</h3><p>When working with asynchronous tasks, there are scenarios where you need to either run tasks in parallel or in sequence, depending on the nature of the operations and performance requirements.</p><h4>Running Asynchronous Tasks in Parallel Using Promise.all</h4><p>When multiple asynchronous tasks are independent of each other and can be executed simultaneously, Promise.all() is a great choice. It runs the tasks in parallel and resolves once all promises have been fulfilled. If any promise is rejected, Promise.all() immediately rejects.</p><pre>async function fetchMultipleResources() {<br>  try {<br>    const [data1, data2, data3] = await Promise.all([<br>      fetch(&#39;https://api.example.com/resource1&#39;),<br>      fetch(&#39;https://api.example.com/resource2&#39;),<br>      fetch(&#39;https://api.example.com/resource3&#39;)<br>    ]);<br><br>    const result1 = await data1.json();<br>    const result2 = await data2.json();<br>    const result3 = await data3.json();<br><br>    console.log(result1, result2, result3);<br>  } catch (error) {<br>    console.error(&#39;Error fetching resources:&#39;, error);<br>  }<br>}<br><br>fetchMultipleResources();</pre><p>In this example, all three API calls are executed in parallel, reducing overall execution time.</p><h4>Running Tasks Sequentially with async/await</h4><p>In some cases, tasks depend on the result of previous tasks and must be executed sequentially. Using async/await makes writing sequential asynchronous code straightforward.</p><p><strong>Example:</strong></p><pre>async function fetchResourcesSequentially() {<br>  try {<br>    const data1 = await fetch(&#39;https://api.example.com/resource1&#39;);<br>    const result1 = await data1.json();<br><br>    const data2 = await fetch(&#39;https://api.example.com/resource2&#39;);<br>    const result2 = await data2.json();<br><br>    const data3 = await fetch(&#39;https://api.example.com/resource3&#39;);<br>    const result3 = await data3.json();<br><br>    console.log(result1, result2, result3);<br>  } catch (error) {<br>    console.error(&#39;Error fetching resources:&#39;, error);<br>  }<br>}<br><br>fetchResourcesSequentially();</pre><h3>Throttling and Debouncing Asynchronous Calls</h3><p>Throttling and debouncing are optimization techniques used to control the rate at which a function is executed in response to events, such as user input or API requests.</p><h4>Explanation of Throttling</h4><p>Throttling ensures that a function is called at most once in a specified period of time, even if it&#39;s triggered multiple times. This is useful for scenarios like handling window resizing events, where frequent updates are unnecessary and can lead to performance issues.</p><p><strong>Throttle Example:</strong></p><pre>function throttle(func, limit) {<br>  let inThrottle;<br>  return function() {<br>    const args = arguments;<br>    const context = this;<br>    if (!inThrottle) {<br>      func.apply(context, args);<br>      inThrottle = true;<br>      setTimeout(() =&gt; inThrottle = false, limit);<br>    }<br>  }<br>}<br><br>window.addEventListener(&#39;resize&#39;, throttle(() =&gt; {<br>  console.log(&#39;Window resized&#39;);<br>}, 200));</pre><p>In this example, the resize event handler is throttled to execute at most once every 200 milliseconds.</p><h4>Explanation of Debouncing</h4><p>Debouncing ensures that a function is called only once, after a certain delay has passed since it was last triggered. This is useful in cases like handling user input in a search box, where you want to wait for the user to stop typing before making an API request.</p><p><strong>Debounce Example:</strong></p><pre>function debounce(func, delay) {<br>  let timeout;<br>  return function() {<br>    const args = arguments;<br>    const context = this;<br>    clearTimeout(timeout);<br>    timeout = setTimeout(() =&gt; func.apply(context, args), delay);<br>  }<br>}<br><br>const searchInput = document.getElementById(&#39;search&#39;);<br>searchInput.addEventListener(&#39;keyup&#39;, debounce(() =&gt; {<br>  console.log(&#39;API request for search&#39;);<br>}, 500));</pre><p>In this example, the API request will only be triggered 500 milliseconds after the user stops typing.</p><h4>Practical Examples:</h4><ul><li><strong>Throttling API requests:</strong> When sending API requests triggered by frequent events like scrolling, throttling can ensure that requests are not sent too often, preventing server overload.</li><li><strong>Debouncing user input:</strong> In search fields, debouncing ensures that an API request is only made once the user has finished typing, reducing unnecessary requests and improving performance.</li></ul><h3>Conclusion</h3><p>Asynchronous programming is one of the most crucial concepts to master in JavaScript, especially as modern web applications continue to grow in complexity and demand high levels of interactivity. In this article, we’ve explored the key concepts that form the foundation of asynchronous JavaScript: <strong>callbacks</strong>, <strong>promises</strong>, <strong>async/await</strong>, and the <strong>event loop</strong>.</p><h4>Summary of Key Concepts:</h4><ol><li><strong>Callbacks</strong> were the original way to handle asynchronous operations in JavaScript, allowing functions to be passed as arguments and executed after a task completes. However, callbacks often led to nested, hard-to-read code, known as “callback hell.”</li><li><strong>Promises</strong> provided a more elegant way to deal with asynchronous tasks by representing eventual completion (or failure) of an asynchronous operation. Promises offer a cleaner approach by allowing chaining with .then(), .catch(), and .finally() methods, making it easier to manage multiple asynchronous operations.</li><li><strong>Async/Await</strong> further simplified asynchronous code by allowing developers to write asynchronous logic as if it were synchronous. It eliminated the need for complex promise chains, resulting in more readable and maintainable code, while still retaining full control over error handling.</li><li>The <strong>Event Loop</strong> is the core mechanism that drives asynchronous operations in JavaScript. Understanding how the event loop manages tasks, microtasks, and the call stack is essential for writing efficient, non-blocking code.</li></ol><h4>Why Mastering Asynchronous Programming is Crucial for Modern Web Development:</h4><p>Modern web applications are often dynamic and interactive, requiring constant communication with servers, databases, and external APIs. Whether it’s fetching user data, handling real-time updates, or processing large amounts of information in the background, asynchronous programming allows these tasks to be performed without freezing the user interface.</p><p>Mastering asynchronous programming ensures that your web applications remain <strong>responsive</strong>, <strong>efficient</strong>, and <strong>scalable</strong>. As websites increasingly rely on APIs, real-time communication, and complex workflows, knowing how to handle asynchronous operations properly is essential for delivering seamless user experiences.</p><h4>Encouraging Real-World Practice:</h4><p>While learning the theory behind asynchronous programming is essential, true mastery comes through practice. I encourage you to apply these concepts by building real-world projects, such as:</p><ul><li>A <strong>weather application</strong> that fetches real-time data from a public API</li><li>A <strong>chat application</strong> using <strong>WebSockets</strong> for real-time communication</li><li>A <strong>task manager</strong> that uses asynchronous file uploads and API integration</li></ul><p>Working on such projects will not only solidify your understanding of callbacks, promises, and async/await but also prepare you to tackle real challenges in modern web development. As you build, you’ll encounter practical scenarios where asynchronous programming is critical, reinforcing your skills and boosting your confidence as a JavaScript developer.</p><p>By mastering these concepts, you’re not just becoming proficient in JavaScript — you’re equipping yourself with the tools to build powerful, interactive, and responsive applications that can handle the demands of today’s web ecosystem.</p><p>That’s all from my side! I’ve tried to cover everything you need to master asynchronous programming in JavaScript. If you enjoyed the content, kindly follow me. I’ll keep bringing more tutorials and articles to help you learn and grow!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/425/1*dPlvI0_XFyUpO4iYUTovaQ.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9aa8949faff8" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>