<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: RATHEESH G KUMAR </title>
    <description>The latest articles on DEV Community by RATHEESH G KUMAR  (@ratheeshkumar25).</description>
    <link>https://dev.to/ratheeshkumar25</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1374517%2F059ac819-86d4-415d-8159-4700bad63f09.jpeg</url>
      <title>DEV Community: RATHEESH G KUMAR </title>
      <link>https://dev.to/ratheeshkumar25</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ratheeshkumar25"/>
    <language>en</language>
    <item>
      <title>Understanding Buffered Channels in Golang with sample problems</title>
      <dc:creator>RATHEESH G KUMAR </dc:creator>
      <pubDate>Thu, 03 Apr 2025 14:10:07 +0000</pubDate>
      <link>https://dev.to/ratheeshkumar25/understanding-buffered-channels-in-golang-with-sample-problems-5f8m</link>
      <guid>https://dev.to/ratheeshkumar25/understanding-buffered-channels-in-golang-with-sample-problems-5f8m</guid>
      <description>&lt;p&gt;How do you work over a buffered channel ? this question while we learn about the concept channel ? &lt;/p&gt;

&lt;p&gt;Before jumping into the buffered channel, we need to understand what the channel is.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Introduction to Channels in Go&lt;/strong&gt;&lt;br&gt;
Channels in Go provide a medium for communication between two or more goroutines. Goroutines are lightweight threads managed by the Go runtime scheduler, allowing concurrent execution.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Types of Channels in Go&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Buffered Channel&lt;/strong&gt; – A sender can send data until the buffer is full. Once full, it blocks further sends until space is available.&lt;/p&gt;

&lt;p&gt;Used for asynchronous communication.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Unbuffered Channel&lt;/strong&gt; – A sender can only send data if a receiver is ready. The sender blocks until the receiver reads the value.&lt;/p&gt;

&lt;p&gt;Used for synchronous communication.&lt;/p&gt;

&lt;p&gt;Working with Buffered Channels&lt;br&gt;
Let's take an array and use goroutines with buffered channels to compute:&lt;/p&gt;

&lt;p&gt;The sum of the array&lt;/p&gt;

&lt;p&gt;The largest element&lt;/p&gt;

&lt;p&gt;The second-largest element&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;`
package main

import (
    "fmt"
    "sync"
)

func secondLargest(arr []int, ch chan int, wg *sync.WaitGroup) &amp;lt;-chan int {
    // Decreases the counter by 1 when a goroutine finishes execution
    defer wg.Done()
    lar := arr[0]
    secnd := -1

    for _, v := range arr {
        if v &amp;gt; lar {
            secnd = lar
            lar = v
        } else if v &amp;lt; lar &amp;amp;&amp;amp; v &amp;gt; secnd {
            secnd = v
        }
    }
    ch &amp;lt;- secnd
    return ch
}

func sumOfArray(arr []int, ch chan int, wg *sync.WaitGroup) &amp;lt;-chan int {
    // Decreases the counter by 1 when a goroutine finishes execution
    defer wg.Done()
    sum := 0

    for _, v := range arr {
        sum += v
    }
    ch &amp;lt;- sum
    return ch
}

func largestElement(arr []int, ch chan int, wg *sync.WaitGroup) &amp;lt;-chan int {
    // Decreases the counter by 1 when a goroutine finishes execution
    defer wg.Done()
    largest := arr[0]

    for _, v := range arr {
        if v &amp;gt; largest {
            largest = v
        }
    }

    ch &amp;lt;- largest
    return ch
}

func main() {
    //Buffered channel of size 3
    ch := make(chan int, 3)
    //wait for a group of goroutines to finish execution
    var wg sync.WaitGroup
    arr := []int{2, 3, 4, 50, 6, 70, 8, 9}

    //Increases the counter by the specified number of goroutines.
    wg.Add(3)

    go secondLargest(arr, ch, &amp;amp;wg)
    fmt.Println("Second largest", &amp;lt;-ch)

    go sumOfArray(arr, ch, &amp;amp;wg)
    fmt.Println("Sum of array", &amp;lt;-ch)
    go largestElement(arr, ch, &amp;amp;wg)

    fmt.Println("largest element of array", &amp;lt;-ch)

    go func() {
        close(ch)
        //Blocks execution until the counter becomes zero.
        wg.Wait()
    }()

}
`

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Buffered Channel Diagram (Capacity = 3)&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   Goroutines Send Data
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;┌───────────────────────────────┐&lt;br&gt;
  │   Goroutine 1 -&amp;gt; ch &amp;lt;- 50     │&lt;br&gt;
  │   Goroutine 2 -&amp;gt; ch &amp;lt;- 70     │&lt;br&gt;
  │   Goroutine 3 -&amp;gt; ch &amp;lt;- 100    │&lt;br&gt;
  └───────────────────────────────┘&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    Buffered Channel (Capacity = 3)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;┌───────────┬───────────┬───────────┐&lt;br&gt;
  │    50     │    70     │    100    │   (Buffered)&lt;br&gt;
  └───────────┴───────────┴───────────┘&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    Main Goroutine Reads Data
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;┌───────────────────────────────┐&lt;br&gt;
  │ fmt.Println(&amp;lt;-ch) → 50        │&lt;br&gt;
  │ fmt.Println(&amp;lt;-ch) → 70        │&lt;br&gt;
  │ fmt.Println(&amp;lt;-ch) → 100       │&lt;br&gt;
  └───────────────────────────────┘&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;📌 Understanding Buffered Channel Behavior&lt;/strong&gt;&lt;br&gt;
The buffered channel is created with a capacity of 3 (make(chan int, 3)).&lt;/p&gt;

&lt;p&gt;This means it can hold three values at a time before blocking further writes.&lt;/p&gt;

&lt;p&gt;If we try to send more than three values before reading, the program will deadlock.&lt;/p&gt;

&lt;p&gt;source code : &lt;a href="https://go.dev/play/p/YqaoecokCjl" rel="noopener noreferrer"&gt;https://go.dev/play/p/YqaoecokCjl&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>[Boost]</title>
      <dc:creator>RATHEESH G KUMAR </dc:creator>
      <pubDate>Thu, 03 Apr 2025 13:02:18 +0000</pubDate>
      <link>https://dev.to/ratheeshkumar25/-47di</link>
      <guid>https://dev.to/ratheeshkumar25/-47di</guid>
      <description></description>
      <category>emptystring</category>
    </item>
    <item>
      <title>#goroutine</title>
      <dc:creator>RATHEESH G KUMAR </dc:creator>
      <pubDate>Fri, 14 Mar 2025 10:02:09 +0000</pubDate>
      <link>https://dev.to/ratheeshkumar25/goroutine-jm7</link>
      <guid>https://dev.to/ratheeshkumar25/goroutine-jm7</guid>
      <description>&lt;div class="ltag__link"&gt;
  &lt;a href="/ratheeshkumar25" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1374517%2F059ac819-86d4-415d-8159-4700bad63f09.jpeg" alt="ratheeshkumar25"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="https://dev.to/ratheeshkumar25/character-frequency-count-in-a-string-using-goroutines-in-go-4ekb" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;💡 Character Frequency Count in a String using Goroutines in Go&lt;/h2&gt;
      &lt;h3&gt;RATHEESH G KUMAR  ・ Mar 14&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


</description>
      <category>go</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>💡 Character Frequency Count in a String using Goroutines in Go</title>
      <dc:creator>RATHEESH G KUMAR </dc:creator>
      <pubDate>Fri, 14 Mar 2025 10:00:37 +0000</pubDate>
      <link>https://dev.to/ratheeshkumar25/character-frequency-count-in-a-string-using-goroutines-in-go-4ekb</link>
      <guid>https://dev.to/ratheeshkumar25/character-frequency-count-in-a-string-using-goroutines-in-go-4ekb</guid>
      <description>&lt;p&gt;In Go, strings are immutable, meaning once a string is created, its value cannot be modified directly. However, we can still analyze and process strings efficiently.&lt;/p&gt;

&lt;p&gt;In this example, we will implement a program to calculate the frequency of each character in a string using Goroutines and channels.&lt;/p&gt;

&lt;p&gt;🔤 Sample String:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;`str := "We often will need to manipulate strings in our messaging app. For example, adding some personalization by using a customer's name within a template"`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;📦 Step 1: Define a Struct to Hold Character and Count&lt;/p&gt;

&lt;p&gt;type CharFreq struct {&lt;br&gt;
    Char  rune&lt;br&gt;
    Count int&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;📦 Step 2: Declare a Map and Channel&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
charCh := make(chan CharFreq)
strMap := make(map[string]int)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🔁 Step 3: Iterate Through the String and Count Characters&lt;br&gt;
We will convert all characters to lowercase and ignore spaces and punctuation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
`for _, v := range str {
    // Convert uppercase to lowercase
    if v &amp;gt;= 'A' &amp;amp;&amp;amp; v &amp;lt;= 'Z' {
        v += 32
    }

    // Skip spaces and punctuation (optional enhancement)
    if v == ' ' {
        continue
    }

    // Convert rune to string and count
    strMap[v]++
}`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;⚙️ Step 4: Use an Anonymous Goroutine to Send Data to Channel&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;`go func() {
    for i, v := range strMap {
        charCh &amp;lt;- CharFreq{Char: i, Count: v}
    }
    close(charCh)
}()`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;📤 Step 5: Read from Channel and Print Results&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
`for item := range charCh {
    fmt.Printf("Character: %s, Count: %d\n", item.Char, item.Count)
}`

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅ Final Output:&lt;br&gt;
You will get the frequency of each character (excluding spaces) printed via the channel using a concurrent approach.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;//output&lt;br&gt;
Character: g, Count: 5&lt;br&gt;
Character: x, Count: 1&lt;br&gt;
Character: e, Count: 14&lt;br&gt;
Character: o, Count: 8&lt;br&gt;
Character: m, Count: 7&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Complete implementation link: &lt;a href="https://www.programiz.com/online-compiler/0G4nsPTV0NR3B" rel="noopener noreferrer"&gt;https://www.programiz.com/online-compiler/0G4nsPTV0NR3B&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Product of Array Expect Itself Leetcode Question - 238</title>
      <dc:creator>RATHEESH G KUMAR </dc:creator>
      <pubDate>Sat, 08 Mar 2025 06:29:25 +0000</pubDate>
      <link>https://dev.to/ratheeshkumar25/product-of-array-expect-itself-leetcode-question-238-2oc2</link>
      <guid>https://dev.to/ratheeshkumar25/product-of-array-expect-itself-leetcode-question-238-2oc2</guid>
      <description>&lt;p&gt;Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i].&lt;br&gt;
The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.&lt;br&gt;
You must write an algorithm that runs in O(n) time and without using the division operation.&lt;/p&gt;

&lt;p&gt;Example 1:&lt;/p&gt;

&lt;p&gt;Input: nums = [1,2,3,4]&lt;br&gt;
Output: [24,12,8,6]&lt;br&gt;
Example 2:&lt;/p&gt;

&lt;p&gt;Input: nums = [-1,1,0,-3,3]&lt;br&gt;
Output: [0,0,9,0,0]&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Approach: Prefix and Postfix Products&lt;/strong&gt;&lt;br&gt;
My solution uses a two-pass approach utilizing prefix and postfix products to efficiently solve the problem in O(n) time with O(1) extra space (excluding the output array).&lt;br&gt;
Key Insight&lt;/p&gt;

&lt;p&gt;Prefix product: The product of all elements to the left of the current element&lt;br&gt;
Postfix product: The product of all elements to the right of the current element&lt;br&gt;
The product of all elements except the current one is: prefix × postfix&lt;/p&gt;

&lt;p&gt;Algorithm&lt;/p&gt;

&lt;p&gt;Initialize a result array of the same size as the input array&lt;br&gt;
First pass: Calculate prefix products from left to right&lt;br&gt;
Second pass: Calculate postfix products from right to left, while multiplying with the existing prefix products&lt;/p&gt;

&lt;p&gt;Visualization&lt;br&gt;
For example, with input array nums = [1, 2, 3, 4]:&lt;br&gt;
First Pass (Prefix Products):&lt;/p&gt;

&lt;p&gt;Start with prefix = 1&lt;br&gt;
For each position, store the current prefix in the result array, then update the prefix&lt;br&gt;
After first pass, result = [1, 1, 2, 6]&lt;/p&gt;

&lt;p&gt;Second Pass (Postfix Products):&lt;/p&gt;

&lt;p&gt;Start with postfix = 1&lt;br&gt;
For each position (in reverse), multiply the existing value by the current postfix, then update the postfix&lt;br&gt;
After second pass, result = [24, 12, 4, 1]&lt;/p&gt;

&lt;p&gt;This gives us our answer, as each position now contains the product of all elements except the one at that position.&lt;br&gt;
Code Implementation&lt;br&gt;
&lt;strong&gt;Approach: Prefix and Postfix Products&lt;/strong&gt;&lt;br&gt;
My solution uses a two-pass approach utilizing prefix and postfix products to efficiently solve the problem in O(n) time with O(1) extra space (excluding the output array).&lt;br&gt;
Key Insight&lt;/p&gt;

&lt;p&gt;Prefix product: The product of all elements to the left of the current element&lt;br&gt;
Postfix product: The product of all elements to the right of the current element&lt;br&gt;
The product of all elements except the current one is: prefix × postfix&lt;/p&gt;

&lt;p&gt;Algorithm&lt;/p&gt;

&lt;p&gt;Initialize a result array of the same size as the input array&lt;br&gt;
First pass: Calculate prefix products from left to right&lt;br&gt;
Second pass: Calculate postfix products from right to left, while multiplying with the existing prefix products&lt;/p&gt;

&lt;p&gt;Visualization&lt;br&gt;
For example, with input array nums = [1, 2, 3, 4]:&lt;br&gt;
First Pass (Prefix Products):&lt;/p&gt;

&lt;p&gt;Start with prefix = 1&lt;br&gt;
For each position, store the current prefix in the result array, then update the prefix&lt;br&gt;
After first pass, result = [1, 1, 2, 6]&lt;/p&gt;

&lt;p&gt;Second Pass (Postfix Products):&lt;/p&gt;

&lt;p&gt;Start with postfix = 1&lt;br&gt;
For each position (in reverse), multiply the existing value by the current postfix, then update the postfix&lt;br&gt;
After second pass, result = [24, 12, 4, 1]&lt;/p&gt;

&lt;p&gt;This gives us our answer, as each position now contains the product of all elements except the one at that position.&lt;br&gt;
Code Implementation&lt;br&gt;
My solution uses a two-pass approach utilizing prefix and postfix products to efficiently solve the problem in O(n) time with O(1) extra space (excluding the output array).&lt;br&gt;
Key Insight&lt;/p&gt;

&lt;p&gt;Prefix product: The product of all elements to the left of the current element&lt;br&gt;
Postfix product: The product of all elements to the right of the current element&lt;br&gt;
The product of all elements except the current one is: prefix × postfix&lt;/p&gt;

&lt;p&gt;Algorithm&lt;/p&gt;

&lt;p&gt;Initialize a result array of the same size as the input array&lt;br&gt;
First pass: Calculate prefix products from left to right&lt;br&gt;
Second pass: Calculate postfix products from right to left, while multiplying with the existing prefix products&lt;/p&gt;

&lt;p&gt;Visualization&lt;br&gt;
For example, with input array nums = [1, 2, 3, 4]:&lt;br&gt;
First Pass (Prefix Products):&lt;/p&gt;

&lt;p&gt;Start with prefix = 1&lt;br&gt;
For each position, store the current prefix in the result array, then update the prefix&lt;br&gt;
After first pass, result = [1, 1, 2, 6]&lt;/p&gt;

&lt;p&gt;Second Pass (Postfix Products):&lt;/p&gt;

&lt;p&gt;Start with postfix = 1&lt;br&gt;
For each position (in reverse), multiply the existing value by the current postfix, then update the postfix&lt;br&gt;
After second pass, result = [24, 12, 4, 1]&lt;/p&gt;

&lt;p&gt;This gives us our answer, as each position now contains the product of all elements except the one at that position.&lt;br&gt;
&lt;strong&gt;Code Implementation&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func productExceptSelf(nums []int) []int {
// result array of the same size as input array
    res := make([]int,len(nums))
//calculate prefix
    prefix :=1
    for i,v := range nums{
        res[i] = prefix
        prefix *= v
    }

// Calculate postfix products and multiply with prefix products
    postfix := 1
    for i := len(nums)-1; i &amp;gt;= 0 ; i--{
      res[i] *= postfix
      postfix *= nums[i]
    }

    return res
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>leetcode</category>
      <category>go</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
