<?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>Forem: Guna Sai</title>
    <description>The latest articles on Forem by Guna Sai (@guna01).</description>
    <link>https://forem.com/guna01</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%2F2540874%2F342f0cb2-ff98-4ecb-95fb-d46e6bb7f517.jpg</url>
      <title>Forem: Guna Sai</title>
      <link>https://forem.com/guna01</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/guna01"/>
    <language>en</language>
    <item>
      <title>Rate Limiting: Concepts, Algorithms, and Distributed Challenges</title>
      <dc:creator>Guna Sai</dc:creator>
      <pubDate>Sun, 08 Feb 2026 11:15:39 +0000</pubDate>
      <link>https://forem.com/guna01/rate-limiting-concepts-algorithms-and-distributed-challenges-4gfl</link>
      <guid>https://forem.com/guna01/rate-limiting-concepts-algorithms-and-distributed-challenges-4gfl</guid>
      <description>&lt;p&gt;If you've ever built an api or backend service, you've probably faced one of these problems:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;One user sends too many requests&lt;/li&gt;
&lt;li&gt;bots abusing your endpoints&lt;/li&gt;
&lt;li&gt;traffic spikes breaking your service&lt;/li&gt;
&lt;li&gt;retries or cron jobs accidentally overloading your system.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This blog is about rate limiting, a simple but critical technique used to protect systems from these issues&lt;/p&gt;

&lt;p&gt;In this post, we will&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;understand why rate limiting is needed,&lt;/li&gt;
&lt;li&gt;learn how common rate limiting algorithms work,&lt;/li&gt;
&lt;li&gt;see where rate limiting fits in real systems.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You don't need prior knowledge of rate limiting.&lt;br&gt;
If you understand basic APIs and requests, you'll be able to follow along.&lt;/p&gt;




&lt;h2&gt;
  
  
  Content of this blog
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The Problem Rate Limiting Solves&lt;/li&gt;
&lt;li&gt;What Rate Limiting Actually Does&lt;/li&gt;
&lt;li&gt;
Common Rate Limiting Algorithms

&lt;ul&gt;
&lt;li&gt;Fixed Window Counter&lt;/li&gt;
&lt;li&gt;Sliding Window&lt;/li&gt;
&lt;li&gt;Token Bucket&lt;/li&gt;
&lt;li&gt;Leaky Bucket&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Comparing the Algorithms&lt;/li&gt;

&lt;li&gt;Challenges in Distributed Systems&lt;/li&gt;

&lt;/ul&gt;




&lt;h2&gt;
  
  
  The Problem Rate Limiting Solves
&lt;/h2&gt;

&lt;p&gt;When a req hits your server, it consumes resources like CPU time, memory, database connections, network bandwidth etc...&lt;/p&gt;

&lt;p&gt;Under normal usage, this works fine, but problems starts when &lt;strong&gt;too many requests arrive at the same time&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This can happen because of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a single user sending requests in a tight loop,&lt;/li&gt;
&lt;li&gt;bots hitting public endpoints,&lt;/li&gt;
&lt;li&gt;retry mechanisms without proper backoff,&lt;/li&gt;
&lt;li&gt;sudden traffic spikes after a release or promotion&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The server sees all requests as the same it doesn't know which request is important and which one is harmful.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why does this becomes a serious problem
&lt;/h3&gt;

&lt;p&gt;If the request volume keeps increasing without limits:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;response times go up,&lt;/li&gt;
&lt;li&gt;Databases start slowing down,&lt;/li&gt;
&lt;li&gt;timeouts increase,&lt;/li&gt;
&lt;li&gt;error rates spike,&lt;/li&gt;
&lt;li&gt;and eventually the service becomes unavailable for everyone.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Why can't we just "scale the server."
&lt;/h3&gt;

&lt;p&gt;Our common reaction will be "let's just add more servers."&lt;/p&gt;

&lt;p&gt;Scaling helps, but it &lt;strong&gt;does not solve the root problem&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;eventually, unlimited requests will overwhelm any system,&lt;/li&gt;
&lt;li&gt;scaling increases cost,&lt;/li&gt;
&lt;li&gt;Databases, third-party APIs may not scale the same way.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;if just keep on scaling it only delays failure.&lt;/p&gt;

&lt;h3&gt;
  
  
  What systems really need is
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;a way to control how fast requests are allowed.&lt;/li&gt;
&lt;li&gt;protection against accidental or intentional abuse,&lt;/li&gt;
&lt;li&gt;fairness so one user cannot starve others.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is exactly the problem &lt;strong&gt;rate limiting&lt;/strong&gt; is designed to solve.&lt;/p&gt;




&lt;h2&gt;
  
  
  What Rate Limiting Actually Does
&lt;/h2&gt;

&lt;p&gt;At its core, rate limiting controls &lt;strong&gt;how frequently an action is allowed&lt;/strong&gt; within a given time period.&lt;/p&gt;

&lt;p&gt;Most commonly, this action is an API request.&lt;br&gt;
A rate limit usually looks like this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;em&gt;allow 100 reqs per minute per user&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;allow 10 reqs per second per IP&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;allow 1 request per second for a sensitive endpoint&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When the limit is reached, the system does not process further reqs until enough time has passed.&lt;/p&gt;

&lt;h3&gt;
  
  
  What happens when a limit is exceeded
&lt;/h3&gt;

&lt;p&gt;When a client crosses the allowed limit:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the request is rejected,&lt;/li&gt;
&lt;li&gt;the server responds immediately,&lt;/li&gt;
&lt;li&gt;resources are preserved for other users.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In HTTP-based systems, this is commonly returned as a &lt;strong&gt;429 Too Many Requests&lt;/strong&gt; response.&lt;/p&gt;

&lt;p&gt;This early rejection is important.&lt;br&gt;&lt;br&gt;
It prevents unnecessary work from happening in the system, such as database queries or external api calls.&lt;/p&gt;

&lt;h3&gt;
  
  
  What rate limiting guarantees
&lt;/h3&gt;

&lt;p&gt;Rate limiting helps systems achieve a few important guarantees:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Fair usage&lt;/em&gt;&lt;/strong&gt; &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;One user cannot consume resources meant for everyone else.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Predictable performance&lt;/em&gt;&lt;/strong&gt;  &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The system remains responsive even under load.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Controlled bursts&lt;/em&gt;&lt;/strong&gt;  &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Some algorithms allow short bursts while still enforcing long-term limits.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;System protection&lt;/strong&gt;  &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Accidental bugs or misbehaving clients are contained early.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What rate limiting does NOT do
&lt;/h3&gt;

&lt;p&gt;Rate limiting is often misunderstood, so it's important to be clear about its limits.&lt;/p&gt;

&lt;p&gt;Rate limiting is &lt;strong&gt;&lt;em&gt;not&lt;/em&gt;&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;authentication (it does not verify who the user is),&lt;/li&gt;
&lt;li&gt;a complete security solution,&lt;/li&gt;
&lt;li&gt;a replacement for proper input validation.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It is a &lt;strong&gt;&lt;em&gt;traffic control mechanism&lt;/em&gt;&lt;/strong&gt;, not a security gate.&lt;/p&gt;




&lt;h2&gt;
  
  
  Common Rate Limiting Algorithms
&lt;/h2&gt;

&lt;p&gt;Different systems use different rate limiting algorithms depending on their needs.&lt;br&gt;
There is not algo that is "best", each one makes different trade-offs&lt;br&gt;
btw simplicity, accuracy, and flexibility.&lt;/p&gt;

&lt;p&gt;let's go through the most commonly used ones,&lt;/p&gt;




&lt;h3&gt;
  
  
  Fixed Window Counter
&lt;/h3&gt;

&lt;p&gt;This is the simplest form of rate limiting.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How it works&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Time is divided into fixed windows, such as 1 minute or 1 hour.&lt;/li&gt;
&lt;li&gt;For each user, the system keeps a counter for the current window.&lt;/li&gt;
&lt;li&gt;Every incoming request increments this counter.&lt;/li&gt;
&lt;li&gt;Once the counter reaches the limit, further requests are rejected.&lt;/li&gt;
&lt;li&gt;When the window ends, the counter is reset to zero.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Limit: 5 requests per minute
&lt;/li&gt;
&lt;li&gt;Window: 12:00 - 12:01
&lt;/li&gt;
&lt;li&gt;A user sends 5 requests at 12:00:59 -&amp;gt; all allowed
&lt;/li&gt;
&lt;li&gt;The counter resets at 12:01:00 -&amp;gt; another 5 requests are allowed&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This means the user effectively made &lt;strong&gt;10 requests in 1 second&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="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%2Farticles%2Fuatqt1bmosu1k6nwcav6.png" class="article-body-image-wrapper"&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%2Farticles%2Fuatqt1bmosu1k6nwcav6.png" alt="fixed-window-ex-1" width="800" height="426"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="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%2Farticles%2Fmxfcxk1z4zm77n4n7fdp.png" class="article-body-image-wrapper"&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%2Farticles%2Fmxfcxk1z4zm77n4n7fdp.png" alt="fixed-window-ex-2" width="700" height="300"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why Fixed Window fails&lt;/strong&gt;&lt;br&gt;
The main issue with Fixed Window Counter is that it only looks at the clock.&lt;br&gt;&lt;br&gt;
It does not care &lt;em&gt;how requests are distributed&lt;/em&gt; inside the window.&lt;/p&gt;

&lt;p&gt;Because of this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;users can exploit window boundaries,&lt;/li&gt;
&lt;li&gt;traffic becomes very bursty,&lt;/li&gt;
&lt;li&gt;backend services can suddenly receive huge spikes,&lt;/li&gt;
&lt;li&gt;the system becomes unfair under load.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;When Fixed Window can be acceptable&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;very low-traffic systems,&lt;/li&gt;
&lt;li&gt;internal tools,&lt;/li&gt;
&lt;li&gt;prototypes or demos,&lt;/li&gt;
&lt;li&gt;cases where correctness is less important than simplicity.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In most production apis, Fixed Window is usually avoided.&lt;/p&gt;




&lt;h3&gt;
  
  
  Sliding Window
&lt;/h3&gt;

&lt;p&gt;Sliding window algorithms try to fix the burst problem of fixed windows.&lt;/p&gt;

&lt;p&gt;Instead of looking at a fixed clock window, the system looks at the &lt;strong&gt;last N seconds&lt;/strong&gt; from the current time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How it works&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The system always looks at the last &lt;code&gt;N&lt;/code&gt; seconds from the current time.&lt;/li&gt;
&lt;li&gt;Every request is evaluated against this rolling window.&lt;/li&gt;
&lt;li&gt;The system counts how many requests occurred in the previous &lt;code&gt;N&lt;/code&gt; seconds.&lt;/li&gt;
&lt;li&gt;If the count exceeds the limit, the request is rejected.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Limit: 100 requests per 60 seconds
&lt;/li&gt;
&lt;li&gt;At any moment, the system checks how many requests happened in the previous 60 seconds&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This prevents sudden spikes caused by window resets, as reqs are spread more evenly over time.&lt;/p&gt;

&lt;p&gt;&lt;a href="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%2Farticles%2Ffj1bbxm2dwp90skhkvv3.png" class="article-body-image-wrapper"&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%2Farticles%2Ffj1bbxm2dwp90skhkvv3.png" alt="sliding window example-1" width="800" height="418"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="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%2Farticles%2F1syifekah2hjqmpf1v21.png" class="article-body-image-wrapper"&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%2Farticles%2F1syifekah2hjqmpf1v21.png" alt="sliding window example-2" width="800" height="449"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sliding Window is better because&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;much fairer request distribution,&lt;/li&gt;
&lt;li&gt;Traffic spikes are naturally reduced&lt;/li&gt;
&lt;li&gt;No burst problems at window boundaries.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Sliding Window is harder because&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the system need to store timestamps of requests,&lt;/li&gt;
&lt;li&gt;memory usage increases with traffic,&lt;/li&gt;
&lt;li&gt;more computation is needed per req.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Token Bucket
&lt;/h3&gt;

&lt;p&gt;Token Bucket is one of the most commonly used algorithms in production systems because it balances strict limits with good user experience.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How it works&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Each user has a bucket that holds tokens.&lt;/li&gt;
&lt;li&gt;Tokens are added to the bucket at a fixed rate.&lt;/li&gt;
&lt;li&gt;Each request consumes one token.&lt;/li&gt;
&lt;li&gt;If there are no tokens left, the request is rejected.&lt;/li&gt;
&lt;li&gt;The bucket has a maximum capacity, so tokens cannot grow infinitely.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Bucket size: 10 tokens
&lt;/li&gt;
&lt;li&gt;Refill rate: 1 token per second
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Scenario:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;User is idle -&amp;gt; bucket fills up to 10 tokens
&lt;/li&gt;
&lt;li&gt;User sends 10 requests instantly -&amp;gt; all allowed
&lt;/li&gt;
&lt;li&gt;The 11th request is rejected
&lt;/li&gt;
&lt;li&gt;After 1 second, 1 token is added -&amp;gt; 1 request allowed&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="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%2Farticles%2Fq0m7il97cg2kt0oxziwt.png" class="article-body-image-wrapper"&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%2Farticles%2Fq0m7il97cg2kt0oxziwt.png" alt="token bucket ex 1" width="800" height="450"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="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%2Farticles%2Fumlnz48uisqgwkyl5e9o.jpg" class="article-body-image-wrapper"&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%2Farticles%2Fumlnz48uisqgwkyl5e9o.jpg" alt="token bucket ex 2" width="800" height="349"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Token Bucket works well because&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;allows short bursts without breaking the system,&lt;/li&gt;
&lt;li&gt;enforces long-term rate limits,&lt;/li&gt;
&lt;li&gt;provides better user experience,&lt;/li&gt;
&lt;li&gt;simple and efficient to implement.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Because of these properties, Token Bucket is often the &lt;strong&gt;default choice&lt;/strong&gt; for APIs.&lt;/p&gt;




&lt;h3&gt;
  
  
  Leaky Bucket
&lt;/h3&gt;

&lt;p&gt;Leaky Bucket focuses on producing a &lt;strong&gt;smooth and stable output rate&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How it works&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Incoming requests are placed into a queue (the bucket).&lt;/li&gt;
&lt;li&gt;Requests leave the queue at a constant, fixed rate.&lt;/li&gt;
&lt;li&gt;If the queue becomes full, new requests are dropped.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Many requests arrive at once
&lt;/li&gt;
&lt;li&gt;The system processes requests at a steady pace
&lt;/li&gt;
&lt;li&gt;Extra requests are dropped when the queue is full&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Leaky Bucket is useful because&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;protects downstream systems very well,&lt;/li&gt;
&lt;li&gt;ensures predictable processing speed,&lt;/li&gt;
&lt;li&gt;prevents sudden traffic spikes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;But Leaky Bucket is not ideal for APIs because&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;burst requests are delayed or dropped,&lt;/li&gt;
&lt;li&gt;latency increases under load,&lt;/li&gt;
&lt;li&gt;user experience can suffer.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="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%2Farticles%2Fvqn0n99sj5lez3r546sp.jpg" class="article-body-image-wrapper"&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%2Farticles%2Fvqn0n99sj5lez3r546sp.jpg" alt="leaky bucket" width="279" height="181"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Leaky Bucket is more suitable for background jobs and pipelines than user-facing APIs.&lt;/p&gt;




&lt;h2&gt;
  
  
  Comparing the Algorithms
&lt;/h2&gt;

&lt;p&gt;The goal here is not to dive deep again, but to understand &lt;strong&gt;which algorithm fits which situation&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  High-level Comparison
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Algorithm&lt;/th&gt;
&lt;th&gt;Burst Handling&lt;/th&gt;
&lt;th&gt;Fairness&lt;/th&gt;
&lt;th&gt;Complexity&lt;/th&gt;
&lt;th&gt;Common Usage&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Fixed Window&lt;/td&gt;
&lt;td&gt;Poor&lt;/td&gt;
&lt;td&gt;Low&lt;/td&gt;
&lt;td&gt;Very Low&lt;/td&gt;
&lt;td&gt;Simple or low-traffic systems&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Sliding Window&lt;/td&gt;
&lt;td&gt;Good&lt;/td&gt;
&lt;td&gt;High&lt;/td&gt;
&lt;td&gt;High&lt;/td&gt;
&lt;td&gt;Systems needing accuracy&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Token Bucket&lt;/td&gt;
&lt;td&gt;Very Good&lt;/td&gt;
&lt;td&gt;High&lt;/td&gt;
&lt;td&gt;Medium&lt;/td&gt;
&lt;td&gt;Most APIs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Leaky Bucket&lt;/td&gt;
&lt;td&gt;Poor&lt;/td&gt;
&lt;td&gt;Medium&lt;/td&gt;
&lt;td&gt;Medium&lt;/td&gt;
&lt;td&gt;Background jobs&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  How to think about the choice
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Fixed Window is easy to build, but breaks under bursty traffic. It's fine for simple or internal use cases.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Sliding Window gives fair and accurate limits, but costs more in memory and computation.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Token Bucket is the most practical choice for APIs. It allows small bursts while still enforcing long-term limits.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Leaky Bucket focuses on smooth traffic rather than fast responses. It works better for background processing than user-facing APIs.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Challenges in Distributed Systems
&lt;/h2&gt;

&lt;p&gt;So far, everything we discussed assumes a single server.&lt;/p&gt;

&lt;p&gt;In real-world applications, this is rarely the case.&lt;br&gt;&lt;br&gt;
Most systems run on &lt;strong&gt;multiple servers&lt;/strong&gt; behind a load balancer.&lt;/p&gt;

&lt;p&gt;This introduces a few important challenges for rate limiting.&lt;/p&gt;

&lt;h3&gt;
  
  
  Shared State Problem
&lt;/h3&gt;

&lt;p&gt;If each server keeps its own rate limit counters:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;one user can hit different servers,&lt;/li&gt;
&lt;li&gt;each server thinks the user is under the limit,&lt;/li&gt;
&lt;li&gt;the user ends up sending more requests than allowed.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This breaks the rate limit completely.&lt;/p&gt;

&lt;h3&gt;
  
  
  Consistency vs Performance
&lt;/h3&gt;

&lt;p&gt;To fix this, servers need to &lt;strong&gt;share state&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;But shared state brings trade-offs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;strong consistency is accurate but slow,&lt;/li&gt;
&lt;li&gt;weak consistency is fast but slightly inaccurate.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Most real systems accept &lt;strong&gt;small inaccuracies&lt;/strong&gt; to gain performance.&lt;/p&gt;

&lt;h3&gt;
  
  
  Time Synchronization
&lt;/h3&gt;

&lt;p&gt;Many rate limiting algorithms depend on time.&lt;/p&gt;

&lt;p&gt;In distributed systems:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;server clocks may not be perfectly aligned,&lt;/li&gt;
&lt;li&gt;small time differences can affect limits.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This needs to be handled carefully.&lt;/p&gt;

&lt;h3&gt;
  
  
  Key takeaway
&lt;/h3&gt;

&lt;p&gt;Rate limiting becomes much harder once a system is distributed.&lt;/p&gt;

&lt;p&gt;You must:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;share state,&lt;/li&gt;
&lt;li&gt;accept trade-offs,&lt;/li&gt;
&lt;li&gt;balance accuracy and performance.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;At a high level this blog covered how rate limiting works, why different algorithms exist, and why things become more complex once systems are distributed.&lt;/p&gt;

&lt;p&gt;While exploring these ideas, I also implemented a complete distributed rate limiting system that applies the concepts discussed here.&lt;/p&gt;

&lt;p&gt;If you're interested in seeing a real-world implementation, &lt;br&gt;
you can find it here: &lt;a href="https://github.com/Guna1301/arbiter" rel="noopener noreferrer"&gt;github&lt;/a&gt;&lt;/p&gt;

</description>
      <category>backend</category>
      <category>distributedsystems</category>
      <category>api</category>
    </item>
    <item>
      <title>Bit Manipulation</title>
      <dc:creator>Guna Sai</dc:creator>
      <pubDate>Sat, 18 Oct 2025 13:36:49 +0000</pubDate>
      <link>https://forem.com/guna01/bit-manipulation-4ifh</link>
      <guid>https://forem.com/guna01/bit-manipulation-4ifh</guid>
      <description>&lt;p&gt;Bit manipulation can be confusing at first, but it's super useful. You see it in lots of coding problems, and it can make your code faster, lighter, and handle tricky cases like combinations or states.&lt;/p&gt;

&lt;p&gt;With a few simple tricks, you can do things like check if a number is a power of two, find the only unique number in an array, or quickly generate all subsets.&lt;/p&gt;

&lt;p&gt;Let's look at some patterns I've found interesting and see how they actually work in practice.&lt;/p&gt;




&lt;h2&gt;
  
  
  Quick Bit Refresher
&lt;/h2&gt;

&lt;p&gt;Before we get into patterns, here's a short review of the basic bit operators&lt;/p&gt;

&lt;h3&gt;
  
  
  Bitwise Operators
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;AND (&lt;code&gt;&amp;amp;&lt;/code&gt;)&lt;/strong&gt; - Keeps a bit set only if both are &lt;code&gt;1&lt;/code&gt;.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;OR (&lt;code&gt;|&lt;/code&gt;)&lt;/strong&gt; - Sets a bit if any one of them is &lt;code&gt;1&lt;/code&gt;.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;XOR (&lt;code&gt;^&lt;/code&gt;)&lt;/strong&gt; - Sets a bit if both are different.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;NOT (&lt;code&gt;~&lt;/code&gt;)&lt;/strong&gt; - Flips all bits.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Left Shift (&lt;code&gt;&amp;lt;&amp;lt;&lt;/code&gt;)&lt;/strong&gt; - Moves bits to the left (like multiplying by 2).
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Right Shift (&lt;code&gt;&amp;gt;&amp;gt;&lt;/code&gt;)&lt;/strong&gt; - Moves bits to the right (like dividing by 2).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now it's all about spotting patterns and using them.&lt;/p&gt;




&lt;h2&gt;
  
  
  Common Bit Manipulation Patterns
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;These are some patterns that we see and are helpful to us&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  1. Check if a Number is a Power of Two
&lt;/h3&gt;

&lt;p&gt;A number is a power of two if it has only one bit set.&lt;br&gt;&lt;br&gt;
For example, 1 (0001), 2 (0010), 4 (0100), 8 (1000), and so on.  &lt;/p&gt;

&lt;p&gt;The trick is simple:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;How it works:&lt;/strong&gt;&lt;br&gt;
Subtracting 1 flips all bits after the rightmost set bit.&lt;br&gt;
So, when you AND a number with n - 1, the result becomes zero only if there was just one bit set.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;8 (1000)
7 (0111)
8 &amp;amp; 7 = 0000 -&amp;gt; Power of Two
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;When to use:&lt;/strong&gt;&lt;br&gt;
Useful when working with constraints like array sizes, segment trees, or memory blocks that must be powers of two.&lt;br&gt;
&lt;strong&gt;Practice Problem:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://leetcode.com/problems/power-of-two/" rel="noopener noreferrer"&gt;Power of Two&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;
  
  
  2. Count Set Bits (Brian Kernighan's Algorithm)
&lt;/h3&gt;

&lt;p&gt;Counting the number of &lt;code&gt;1&lt;/code&gt;s in a binary number can be done efficiently using this method.&lt;/p&gt;

&lt;p&gt;The trick is simple:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="o"&gt;++;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;How it works:&lt;/strong&gt;&lt;br&gt;
Each time we do n &amp;amp; (n - 1), the rightmost set bit is removed.&lt;br&gt;
We repeat this until the number becomes zero, counting how many bits were set.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;13 (1101)
n = 1101 -&amp;gt; 1100 -&amp;gt; 1000 -&amp;gt; 0000
count = 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;When to use:&lt;/strong&gt;&lt;br&gt;
Useful in problems involving bit masks, subsets, or any situation where you need the number of set bits efficiently.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Practice Problems:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://leetcode.com/problems/number-of-1-bits" rel="noopener noreferrer"&gt;Number of 1 Bits&lt;/a&gt;&lt;br&gt;
&lt;a href="https://leetcode.com/problems/counting-bits" rel="noopener noreferrer"&gt;Counting Bits&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;
  
  
  3. Extract Rightmost Set Bit
&lt;/h3&gt;

&lt;p&gt;Sometimes you need to isolate the least significant &lt;code&gt;1&lt;/code&gt; bit in a number.&lt;/p&gt;

&lt;p&gt;The trick is simple:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;How it works:&lt;/strong&gt;&lt;br&gt;
-n is the two's complement of n.&lt;br&gt;
When you AND n with -n, all bits except the rightmost set bit become 0.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;12 (1100)
-12 (0100 in effect for AND)
12 &amp;amp; -12 = 0100 -&amp;gt; 4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;When to use:&lt;/strong&gt;&lt;br&gt;
Useful in submask iteration, Fenwick tree updates, and bit-based indexing.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Practice Problem:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://leetcode.com/problems/xor-queries-of-a-subarray/" rel="noopener noreferrer"&gt;XOR Queries of a Subarray&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;
  
  
  4. Swap Two Numbers Without Temp
&lt;/h3&gt;

&lt;p&gt;You can swap two numbers without using an extra variable using XOR.&lt;/p&gt;

&lt;p&gt;The trick is simple:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;How it works:&lt;/strong&gt;&lt;br&gt;
XOR has a property that allows you to combine and separate values.&lt;br&gt;
After these three steps, the values of a and b are swapped.&lt;br&gt;
&lt;strong&gt;Example:&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;a = 5 (0101)
b = 7 (0111)

Step 1: a ^= b -&amp;gt; a = 0101 ^ 0111 = 0010
Step 2: b ^= a -&amp;gt; b = 0111 ^ 0010 = 0101
Step 3: a ^= b -&amp;gt; a = 0010 ^ 0101 = 0111
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;When to use:&lt;/strong&gt;&lt;br&gt;
Useful in memory-constrained environments or when extra variables are not allowed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Practice Problem:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;do you actually need it 🤨&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;
  
  
  5. XOR Prefix Sum (Cumulative XOR)
&lt;/h3&gt;

&lt;p&gt;XOR prefix sums help calculate the XOR of any subarray quickly.&lt;/p&gt;

&lt;p&gt;The trick is simple:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;prefix&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;prefix&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
&lt;span class="n"&gt;xor&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;prefix&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="n"&gt;prefix&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;How it works:&lt;/strong&gt;&lt;br&gt;
The XOR of a subarray from index l to r can be found by XORing the prefix up to r with the prefix up to l - 1.&lt;br&gt;
This works because XOR is its own inverse.&lt;br&gt;
&lt;strong&gt;Example:&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;arr = [1, 2, 3, 4]
prefix = [0, 1, 3, 0, 4]  // prefix[0] = 0
XOR(1, 3) = prefix[3] ^ prefix[0] = 0 ^ 0 = 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;When to use:&lt;/strong&gt;&lt;br&gt;
Useful in problems involving range XOR queries, parity checks, or XOR-based subarray problems.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Practice Problems:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://leetcode.com/problems/xor-queries-of-a-subarray/" rel="noopener noreferrer"&gt;XOR Queries of a Subarray&lt;/a&gt;&lt;br&gt;
&lt;a href="https://leetcode.com/problems/single-number/" rel="noopener noreferrer"&gt;Single Number&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;
  
  
  6. Power of Four Check
&lt;/h3&gt;

&lt;p&gt;A number is a power of four if it has only one bit set and that bit is in an even position (0-based index from the right).  &lt;/p&gt;

&lt;p&gt;The trick is simple:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="nf"&gt;isPowerOfFour&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; 
        &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
        &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;How it works:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;n &amp;gt; 0 ensures the number is positive.&lt;/p&gt;

&lt;p&gt;(n &amp;amp; (n - 1)) == 0 checks that the number has only one bit set.&lt;/p&gt;

&lt;p&gt;n % 3 == 1 ensures that the set bit is at an even position. All powers of four (1, 4, 16, 64…) leave a remainder of 1 when divided by 3, which automatically eliminates numbers that are powers of two but not powers of four (like 2, 8, 32…).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&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;16 -&amp;gt; binary 10000 (set bit at position 4, even)
16 &amp;gt; 0 True
16 &amp;amp; (16 - 1) = 16 &amp;amp; 15 = 0 True
16 % 3 = 1 -&amp;gt; Power of Four

8 -&amp;gt; binary 1000 (set bit at position 3, odd)
8 &amp;gt; 0 True
8 &amp;amp; (8 - 1) = 8 &amp;amp; 7 = 0 True
8 % 3 = 2 -&amp;gt; Not Power of Four
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Practice Problem:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://leetcode.com/problems/power-of-four/" rel="noopener noreferrer"&gt;Power of Four&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  7. XOR from 1 to n Pattern
&lt;/h3&gt;

&lt;p&gt;XOR of all numbers from 1 to n follows a repeating pattern every 4 numbers.&lt;/p&gt;

&lt;p&gt;The trick is simple:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;
&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;How it works:&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
The XOR sequence repeats every 4 numbers, which allows you to find the XOR of 1 to n in O(1) time.&lt;br&gt;
XOR has two key properties:  &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;x ^ x = 0&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;x ^ 0 = x&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If you look at the sequence of XOR from 1:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1 -&amp;gt; 1
1 ^ 2 -&amp;gt; 3
1 ^ 2 ^ 3 -&amp;gt; 0
1 ^ 2 ^ 3 ^ 4 -&amp;gt; 4
1 ^ 2 ^ 3 ^ 4 ^ 5 -&amp;gt; 1
1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 6 -&amp;gt; 7
1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 6 ^ 7 -&amp;gt; 0
1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 6 ^ 7 ^ 8 -&amp;gt; 8
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can see the results repeat every 4 numbers: &lt;code&gt;[n, 1, n+1, 0]&lt;/code&gt;.&lt;br&gt;&lt;br&gt;
This happens because each group of 4 numbers cancels out in a specific way due to XOR properties.&lt;br&gt;
&lt;strong&gt;Example:&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;XOR(1..5) -&amp;gt; 1^2^3^4^5
5 % 4 = 1 -&amp;gt; result = 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;When to use:&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Useful in problems with range XOR queries, finding missing numbers, or single number problems.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Practice Problems:&lt;/strong&gt;  &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://leetcode.com/problems/missing-number/" rel="noopener noreferrer"&gt;Missing Number&lt;/a&gt;&lt;br&gt;&lt;br&gt;
&lt;a href="https://leetcode.com/problems/xor-operation-in-an-array/" rel="noopener noreferrer"&gt;XOR Operation in an Array&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;
  
  
  8. Subset Generation via Bitmask
&lt;/h3&gt;

&lt;p&gt;You can generate all subsets of a set using bitmasks. Each element is either included or not included in a subset.&lt;/p&gt;

&lt;p&gt;The trick is simple:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;mask&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;mask&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="n"&gt;mask&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;((&lt;/span&gt;&lt;span class="n"&gt;mask&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// include element i in the subset&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;How it works:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Each number from 0 to 2^n - 1 represents a subset.&lt;/p&gt;

&lt;p&gt;Each bit in the number decides whether to include the corresponding element in the subset.&lt;/p&gt;

&lt;p&gt;Rightmost bit -&amp;gt; arr[0], next bit -&amp;gt; arr[1], and so on.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&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;arr = [a, b, c]
mask = 5 -&amp;gt; binary 101
Check bits:
i = 0 -&amp;gt; 1 -&amp;gt; include a
i = 1 -&amp;gt; 0 -&amp;gt; exclude b
i = 2 -&amp;gt; 1 -&amp;gt; include c
Subset = {a, c}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Practice Problems:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://leetcode.com/problems/subsets" rel="noopener noreferrer"&gt;Subset&lt;/a&gt;&lt;br&gt;
&lt;a href="https://leetcode.com/problems/subsets-ii/" rel="noopener noreferrer"&gt;Subsets II&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  9. Gray Code Conversion
&lt;/h3&gt;

&lt;p&gt;Gray code is a binary sequence where two consecutive numbers differ by only one bit.&lt;br&gt;&lt;br&gt;
It is often used in hardware design or algorithms where minimal bit changes are required.&lt;/p&gt;

&lt;p&gt;The trick is simple:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;gray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;binary&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;g&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;gray&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;g&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;g&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;binary&lt;/span&gt; &lt;span class="o"&gt;^=&lt;/span&gt; &lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;How it works:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Binary to Gray:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Shift the number n one bit to the right.&lt;/p&gt;

&lt;p&gt;XOR the original number with the shifted number.&lt;/p&gt;

&lt;p&gt;This ensures that only one bit changes between consecutive numbers.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Gray to Binary:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Start with binary = 0.&lt;/p&gt;

&lt;p&gt;Take the Gray code and repeatedly XOR it with itself shifted right.&lt;/p&gt;

&lt;p&gt;This reconstructs the original binary number by accumulating changes from left to right.&lt;br&gt;
&lt;strong&gt;Example:&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;Binary 5 -&amp;gt; 101
Shift right -&amp;gt; 010
XOR -&amp;gt; 101 ^ 010 = 111 -&amp;gt; Gray code

Gray 111 -&amp;gt; Binary:
Start binary = 0
Step 1: binary ^= 111 -&amp;gt; 111
Step 2: shift 111 &amp;gt;&amp;gt; 1 = 011, binary ^= 011 -&amp;gt; 100
Step 3: shift 011 &amp;gt;&amp;gt; 1 = 001, binary ^= 001 -&amp;gt; 101
Binary = 101 (original number)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;When to use:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Useful in circular sequences where only one bit should change at a time.&lt;/p&gt;

&lt;p&gt;Helpful in hardware counters, encoders, and minimizing errors during state changes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Practice Problem:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://leetcode.com/problems/gray-code" rel="noopener noreferrer"&gt;Gray Code&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  10. Highest Set Bit (Most Significant Bit)
&lt;/h3&gt;

&lt;p&gt;Finding the position of the highest set bit tells us the largest power of two less than or equal to a number.&lt;/p&gt;

&lt;p&gt;The trick is simple:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;msb&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;pos&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;pos&lt;/span&gt;&lt;span class="o"&gt;++;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;pos&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;How it works:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Keep shifting the number right until it becomes zero.&lt;/p&gt;

&lt;p&gt;Count how many shifts it takes. the last set bit before zero is the highest bit.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&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;n = 10 -&amp;gt; binary 1010
Shift 1: 101 -&amp;gt; pos 1
Shift 2: 10 -&amp;gt; pos 2
Shift 3: 1 -&amp;gt; pos 3
Shift 4: 0 -&amp;gt; stop
Highest set bit position = 3 (value 8)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When to use:&lt;br&gt;
Useful in bucket indexing, determining ranges of powers of two, or binary segmentation problems.&lt;/p&gt;

&lt;p&gt;Practice Problems:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://leetcode.com/problems/number-of-1-bits" rel="noopener noreferrer"&gt;Number of 1 Bits&lt;/a&gt;&lt;br&gt;
&lt;a href="https://leetcode.com/problems/missing-number/" rel="noopener noreferrer"&gt;Missing Number&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;These are some patterns I found interesting and wanted to share from what I learned. &lt;/p&gt;

</description>
      <category>programming</category>
      <category>dsa</category>
      <category>coding</category>
      <category>learning</category>
    </item>
    <item>
      <title>please give it a read</title>
      <dc:creator>Guna Sai</dc:creator>
      <pubDate>Tue, 07 Oct 2025 16:55:23 +0000</pubDate>
      <link>https://forem.com/guna01/please-give-it-a-read-3n8b</link>
      <guid>https://forem.com/guna01/please-give-it-a-read-3n8b</guid>
      <description>&lt;div class="ltag__link"&gt;
  &lt;a href="/gayathri_mangalarapu" 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%2F3551065%2F25842a0d-d02b-42d2-a3ef-2c9d1f08486d.png" alt="gayathri_mangalarapu"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="https://dev.to/gayathri_mangalarapu/agentic-ai-how-llms-really-work-behind-the-scenes-31ap" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Agentic AI: How LLMs Really Work Behind the Scenes&lt;/h2&gt;
      &lt;h3&gt;Gayathri Mangalarapu ・ Oct 7&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#ai&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#llm&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#agenticai&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#tools&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


</description>
      <category>ai</category>
      <category>llm</category>
      <category>agenticai</category>
      <category>tools</category>
    </item>
    <item>
      <title>From HTTP to WebSockets: How Socket.IO Powers Real-Time Apps</title>
      <dc:creator>Guna Sai</dc:creator>
      <pubDate>Sat, 16 Aug 2025 13:52:51 +0000</pubDate>
      <link>https://forem.com/guna01/from-http-to-websockets-how-socketio-powers-real-time-apps-1i1c</link>
      <guid>https://forem.com/guna01/from-http-to-websockets-how-socketio-powers-real-time-apps-1i1c</guid>
      <description>&lt;p&gt;We live in a world where waiting even a few seconds feels like eternity. Imagine chatting with your friend and seeing their reply after 10 seconds… that’s not a conversation, that’s email!  &lt;/p&gt;

&lt;p&gt;If your app still needs a refresh to show what’s new, it already feels old. Think about a chat that lags, a scoreboard that updates late, or a game that’s out of sync. That tiny delay ruins the vibe.  &lt;/p&gt;

&lt;p&gt;That’s where &lt;strong&gt;real-time communication&lt;/strong&gt; comes in, and it’s simpler than it sounds.  &lt;/p&gt;

&lt;p&gt;In this post, you’ll quickly learn:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Why HTTP falls short&lt;/strong&gt; for “instant” experiences
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;What WebSockets are&lt;/strong&gt; &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A tiny client + server example&lt;/strong&gt; you can copy-paste
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Where Socket.IO helps&lt;/strong&gt; (reconnects, rooms, events)
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s dive in &lt;/p&gt;




&lt;h2&gt;
  
  
  Why Do We Even Need Real-Time Communication?
&lt;/h2&gt;

&lt;p&gt;Imagine watching a cricket match on TV where the score updates &lt;strong&gt;5 minutes late&lt;/strong&gt;. By the time you cheer for a six, the batsman’s already out. Feels wrong, right?  &lt;/p&gt;

&lt;p&gt;That’s exactly what happens when apps don’t update in real-time.  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Chats&lt;/strong&gt; -&amp;gt; need instant delivery (otherwise it feels like sending postcards).
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Games&lt;/strong&gt; -&amp;gt; need immediate sync (you can’t dodge an attack you see 3 seconds late).
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Dashboards&lt;/strong&gt; -&amp;gt; need live updates (a stock price that lags is basically useless).
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Basically, &lt;strong&gt;real-time = the world as it happens&lt;/strong&gt;.  &lt;/p&gt;




&lt;h2&gt;
  
  
  HTTP vs WebSockets: The Classic Showdown
&lt;/h2&gt;

&lt;p&gt;Let’s break it down in the simplest way possible&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How HTTP Works (Request/Response)&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;Client ------------------&amp;gt; Server
"Hey, give me the latest news."

Client &amp;lt;------------------ Server
"Here's the news article."
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Always &lt;strong&gt;client-initiated&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Works perfectly for &lt;strong&gt;static content&lt;/strong&gt; (like loading a webpage)
&lt;/li&gt;
&lt;li&gt;But if you need frequent updates? The client has to &lt;strong&gt;keep asking again... and again...&lt;/strong&gt; (polling)
&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;strong&gt;How WebSockets Work (Full Duplex)&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;Client &amp;lt;------------------&amp;gt; Server
"Ye"                    "What’s up?"
"Typing..."             "Seen ✔"
"Ping"                  "Pong"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Two-way street&lt;/strong&gt; (client ↔ server)
&lt;/li&gt;
&lt;li&gt;Connection stays &lt;strong&gt;open&lt;/strong&gt; after the handshake
&lt;/li&gt;
&lt;li&gt;Perfect for chats, games, dashboards where &lt;strong&gt;live sync&lt;/strong&gt; is critical
&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;strong&gt;Quick Comparison&lt;/strong&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;HTTP&lt;/th&gt;
&lt;th&gt;WebSockets&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Communication Type&lt;/td&gt;
&lt;td&gt;Request/Response (Half)&lt;/td&gt;
&lt;td&gt;Full Duplex (Both directions)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Who Starts?&lt;/td&gt;
&lt;td&gt;Always Client&lt;/td&gt;
&lt;td&gt;Either Client or Server&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Best For&lt;/td&gt;
&lt;td&gt;Static pages, APIs&lt;/td&gt;
&lt;td&gt;Real-time apps (chat, games)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Overhead&lt;/td&gt;
&lt;td&gt;Higher (headers every req)&lt;/td&gt;
&lt;td&gt;Lower (persistent connection)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  Why Not Stick with HTTP?
&lt;/h2&gt;

&lt;p&gt;The problem with plain old HTTP is that it was never designed for &lt;em&gt;real-time&lt;/em&gt; communication.&lt;br&gt;&lt;br&gt;
Here’s what usually happens:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The client sends a request: &lt;em&gt;“Got anything new?”&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;The server replies: &lt;em&gt;“Nope, not yet.”&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Two seconds later… the client asks again.
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This constant back-and-forth is called &lt;strong&gt;polling&lt;/strong&gt;.&lt;br&gt;&lt;br&gt;
It:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Burns bandwidth and server resources.
&lt;/li&gt;
&lt;li&gt;Feels unnecessarily slow.
&lt;/li&gt;
&lt;li&gt;Becomes painful for chat apps, games, or live updates.
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It’s like standing next to your friend and asking every 2 seconds:&lt;br&gt;&lt;br&gt;
&lt;em&gt;“Hey, are you there? Any news? How about now?”&lt;/em&gt;&lt;/p&gt;


&lt;h2&gt;
  
  
  WebSockets
&lt;/h2&gt;

&lt;p&gt;WebSockets flip the game completely. Instead of knocking every few seconds,&lt;br&gt;&lt;br&gt;
The client and server just open &lt;strong&gt;one connection&lt;/strong&gt; and keep it alive.  &lt;/p&gt;

&lt;p&gt;That means:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The client can send data whenever it wants.
&lt;/li&gt;
&lt;li&gt;The server can &lt;em&gt;also&lt;/em&gt; push updates instantly.
&lt;/li&gt;
&lt;li&gt;No repeated “Are we there yet?” requests.
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It’s like putting your friend on a call instead of texting every 2 seconds,&lt;br&gt;
You both can talk naturally, in real-time, without the extra noise.&lt;/p&gt;


&lt;h2&gt;
  
  
  WebSockets in Action (Client + Server)
&lt;/h2&gt;

&lt;p&gt;Think of WebSockets like a phone call; once connected, both sides can talk anytime. Let’s see it in code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Server (Node.js)&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;WebSocket&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ws&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Import the WebSocket library&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;wss&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;WebSocket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Server&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;port&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;8080&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt; &lt;span class="c1"&gt;// Create server on port 8080&lt;/span&gt;

&lt;span class="c1"&gt;// When a client connects&lt;/span&gt;
&lt;span class="nx"&gt;wss&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;connection&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ws&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;New client connected!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// When server receives a message&lt;/span&gt;
  &lt;span class="nx"&gt;ws&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;message&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Received: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;ws&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`message received at server end`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Reply back to client&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="c1"&gt;// When client disconnects&lt;/span&gt;
  &lt;span class="nx"&gt;ws&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;close&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Client disconnected.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What’s happening here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Server runs on port 8080.&lt;/li&gt;
&lt;li&gt;When someone connects, it says “New client connected!”.&lt;/li&gt;
&lt;li&gt;If the client sends a message, server replies back with “message received at server end”.&lt;/li&gt;
&lt;li&gt;If they leave, it logs “Client disconnected.”.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Client (Browser JavaScript)&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;socket&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;WebSocket&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ws://localhost:8080&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Connect to server&lt;/span&gt;

&lt;span class="c1"&gt;// When the connection is ready&lt;/span&gt;
&lt;span class="nx"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onopen&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Connected to server&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;hi server!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Send first message&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// When server sends something&lt;/span&gt;
&lt;span class="nx"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onmessage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;event&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Message from server:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What’s happening here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Client connects to the server &lt;code&gt;(ws://localhost:8080)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;As soon as it’s connected, it says “Hello Server!”.&lt;/li&gt;
&lt;li&gt;Whenever server replies, client shows the message.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So in short:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The server waits for clients.&lt;/li&gt;
&lt;li&gt;Client connects &amp;amp; sends messages.&lt;/li&gt;
&lt;li&gt;Both can chat in real-time, just like WhatsApp, no repeated “Are you there?” checks.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Making Life Easier with Socket.IO
&lt;/h2&gt;

&lt;p&gt;Think of &lt;strong&gt;WebSockets&lt;/strong&gt; like a &lt;strong&gt;phone call&lt;/strong&gt; -&amp;gt; fast, real-time talking.&lt;br&gt;&lt;br&gt;
Now, &lt;strong&gt;Socket.IO&lt;/strong&gt; is like &lt;strong&gt;WhatsApp on your phone&lt;/strong&gt; -&amp;gt; same call, but with extra powers:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Auto Reconnects&lt;/strong&gt; -&amp;gt; If your internet drops, it joins back automatically.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Rooms/Groups&lt;/strong&gt; -&amp;gt; Chat with multiple people at once.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Events&lt;/strong&gt; -&amp;gt; Send specific messages (like "typing...", "joined", etc.), not just plain text.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fallbacks&lt;/strong&gt; -&amp;gt; If WebSockets don’t work, it uses another method to still connect.
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;In short:&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Socket.IO = WebSockets + reliability + cool features.&lt;/strong&gt;  &lt;/p&gt;
&lt;h3&gt;
  
  
  WebSocket vs Socket.IO
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;WebSocket&lt;/th&gt;
&lt;th&gt;Socket.IO&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Real-time&lt;/td&gt;
&lt;td&gt;✅ Yes&lt;/td&gt;
&lt;td&gt;✅ Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Auto Reconnect&lt;/td&gt;
&lt;td&gt;❌ No&lt;/td&gt;
&lt;td&gt;✅ Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Rooms/Namespaces&lt;/td&gt;
&lt;td&gt;❌ No&lt;/td&gt;
&lt;td&gt;✅ Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Event-based Messages&lt;/td&gt;
&lt;td&gt;❌ No&lt;/td&gt;
&lt;td&gt;✅ Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Fallback Support&lt;/td&gt;
&lt;td&gt;❌ No&lt;/td&gt;
&lt;td&gt;✅ Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;


&lt;h2&gt;
  
  
  Socket.IO Example
&lt;/h2&gt;

&lt;p&gt;Let’s look at a basic chat example using Node.js, Express, and Socket.IO.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Server (Node.js + Express + Socket.IO)&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;http&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;http&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Server&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;socket.io&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;express&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;server&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createServer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// create HTTP server&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;io&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Server&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;server&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// attach socket.io to server&lt;/span&gt;

&lt;span class="c1"&gt;// Event: when a client connects&lt;/span&gt;
&lt;span class="nx"&gt;io&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;connection&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;socket&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;a user connected&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// Listen for "chat message" from client&lt;/span&gt;
  &lt;span class="nx"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;chat message&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;msg&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Broadcast the message to all connected clients&lt;/span&gt;
    &lt;span class="nx"&gt;io&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;emit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;chat message&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="c1"&gt;// Event: when client disconnects&lt;/span&gt;
  &lt;span class="nx"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;disconnect&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;user disconnected&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="c1"&gt;// Start server&lt;/span&gt;
&lt;span class="nx"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;listening on *:3000&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;http.createServer(app)&lt;/code&gt; -&amp;gt; creates an HTTP server from Express.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;new Server(server)&lt;/code&gt; -&amp;gt; attaches Socket.IO to that HTTP server.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;io.on("connection", ...)&lt;/code&gt; -&amp;gt; triggered whenever a client connects.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;socket.on("chat message", msg =&amp;gt; {...})&lt;/code&gt; -&amp;gt; listens for messages sent from the client.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;io.emit("chat message", msg)&lt;/code&gt; -&amp;gt; broadcasts the message to all connected clients.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;socket.on("disconnect", ...)&lt;/code&gt; -&amp;gt; runs when a client disconnects.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Client (Browser JS)&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;script&lt;/span&gt; &lt;span class="nx"&gt;src&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/socket.io/socket.io.js&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/script&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;script&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;socket&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;io&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// connect to server&lt;/span&gt;

  &lt;span class="c1"&gt;// Event: when connected&lt;/span&gt;
  &lt;span class="nx"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;connect&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Connected to server&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// Send a chat message to server&lt;/span&gt;
    &lt;span class="nx"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;emit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;chat message&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello World!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="c1"&gt;// Listen for chat messages from server&lt;/span&gt;
  &lt;span class="nx"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;chat message&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;msg&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;New message:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/script&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;script src="/socket.io/socket.io.js"&amp;gt;&amp;lt;/script&amp;gt;&lt;/code&gt; -&amp;gt; loads Socket.IO client library.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;const socket = io();&lt;/code&gt; -&amp;gt; connects browser to server.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;socket.emit("chat message", "Hello World!")&lt;/code&gt; -&amp;gt; sends message to server.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;socket.on("chat message", ...)&lt;/code&gt;-&amp;gt; receives messages broadcasted by server.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Key Takeaway&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Server: listens for events and broadcasts messages.&lt;/li&gt;
&lt;li&gt;Client: connects, sends events, and listens for server broadcasts.
This makes building real-time apps (like chat, live notifications, multiplayer games) super easy with Socket.IO. &lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;HTTP works well for standard requests, but it is not designed for real-time communication.
&lt;/li&gt;
&lt;li&gt;WebSockets enable instant, two-way communication between client and server.
&lt;/li&gt;
&lt;li&gt;Socket.IO builds on WebSockets by adding reliability, ease of use, and useful features like rooms and reconnections.
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In short, if you are building a chat app, live feed, or multiplayer game, WebSockets with Socket.IO can make the process much smoother.  &lt;/p&gt;

&lt;h3&gt;
  
  
  Resources
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://socket.io/docs/" rel="noopener noreferrer"&gt;Socket.IO Documentation&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API" rel="noopener noreferrer"&gt;MDN WebSockets Guide&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>devjournal</category>
      <category>javascript</category>
      <category>node</category>
    </item>
    <item>
      <title>DSA? Dev? Why Not Suffer Through Both?</title>
      <dc:creator>Guna Sai</dc:creator>
      <pubDate>Sat, 19 Jul 2025 15:05:30 +0000</pubDate>
      <link>https://forem.com/guna01/dsa-dev-why-not-suffer-through-both-206i</link>
      <guid>https://forem.com/guna01/dsa-dev-why-not-suffer-through-both-206i</guid>
      <description>&lt;p&gt;&lt;em&gt;A personal journey through the noisy crossroads of problem-solving and product-building.&lt;/em&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  The Developer Dilemma
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;“Should I solve another Leetcode problem or build that SaaS idea?”&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This isn't just a shower thought; it's a recurring loop every developer, especially students and self-learners, goes through. You're stuck between two equally demanding paths:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;One says, "Crack DSA. Get that dream package."&lt;/li&gt;
&lt;li&gt;The other whispers, "Ship fast. Build projects. Be a 10x dev."&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But here's the catch: time isn't infinite, and doing both well feels overwhelming. You open VS Code to push that UI update, then Twitter hits you with “Solving 5 DSA problems a day changed my life.” So you panic-go to Leetcode. Then, midway through a binary search question, your brain says: &lt;em&gt;“Wasn't I building a full-stack app a few minutes back?”&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;And this cycle repeats.&lt;/p&gt;

&lt;p&gt;We're all stuck between two doors:&lt;br&gt;&lt;br&gt;
&lt;strong&gt;DSA&lt;/strong&gt; (Data Structures &amp;amp; Algorithms): Logical thinking, coding interviews, and time complexities.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Dev&lt;/strong&gt; (Development): Building real-world projects, deploying apps, and playing with tools.&lt;/p&gt;

&lt;p&gt;It's not a lack of direction. It's &lt;strong&gt;too much direction&lt;/strong&gt;, and that creates a new kind of confusion: &lt;em&gt;&lt;strong&gt;productive anxiety&lt;/strong&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="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%2Farticles%2Fklj87aol2dshznehmcqa.jpeg" class="article-body-image-wrapper"&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%2Farticles%2Fklj87aol2dshznehmcqa.jpeg" alt="Confused guy between dev vs dsa" width="168" height="300"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  The DSA Grind
&lt;/h2&gt;

&lt;p&gt;Think DSA is just for interviews? Not really.&lt;br&gt;&lt;br&gt;
It's like a gym for your brain.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Helps you break big problems into &lt;strong&gt;bite-sized logic chunks&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Makes &lt;code&gt;for&lt;/code&gt; loops feel like second nature&lt;/li&gt;
&lt;li&gt;Teaches you to smell brute-force like a code bloodhound&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Real-world example?&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Your dashboard is slow.&lt;br&gt;&lt;br&gt;
You’re mapping inside a filter, calling the DB twice, sorting for fun...&lt;br&gt;&lt;br&gt;
You stare at it like 😐, rewrite it using prefix sums or memoization, and now it loads in milliseconds.&lt;br&gt;&lt;br&gt;
That’s not magic. That’s you, with your DSA reps kicking in silently.&lt;/p&gt;

&lt;p&gt;It’s less about bubble sort, more about &lt;em&gt;knowing when not to write a nested loop that summons the devil&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Also, ever tried explaining recursion to an invisible person and accidentally become a philosopher?&lt;br&gt;&lt;br&gt;
Yeah. That’s character development.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Dev Drive - Building Beyond the Console
&lt;/h2&gt;

&lt;p&gt;DSA is cool... but Dev? Dev is &lt;strong&gt;alive&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;There’s nothing like shipping something &lt;em&gt;you&lt;/em&gt; built and seeing it live on the internet
&lt;/li&gt;
&lt;li&gt;From UI finesse to backend logic to DB schema. Dev lets you wear all the hats
&lt;/li&gt;
&lt;li&gt;You start appreciating how systems &lt;em&gt;really&lt;/em&gt; talk: APIs, state, latency, user rage, everything
&lt;/li&gt;
&lt;li&gt;The bugs feel personal, and so do the wins
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Analogy Time
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;If DSA is like sorting Lego blocks by size and color,&lt;br&gt;&lt;br&gt;
then &lt;strong&gt;Dev is building the entire Lego city.&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
You don’t just care about one block; you care how it fits, connects, and supports the structure.&lt;br&gt;&lt;br&gt;
Every piece matters. And when one breaks, the whole tower wobbles.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That’s the real power of Dev:&lt;br&gt;&lt;br&gt;
You don’t just solve problems. You &lt;em&gt;design&lt;/em&gt; around them.&lt;br&gt;&lt;br&gt;
You start asking better questions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Can this scale?
&lt;/li&gt;
&lt;li&gt;What happens if the DB goes down?
&lt;/li&gt;
&lt;li&gt;Can I decouple this logic?
&lt;/li&gt;
&lt;li&gt;Will users rage if it lags for 2 seconds?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Every project pushes you a level deeper, from styling a button to thinking about &lt;strong&gt;state management&lt;/strong&gt;, &lt;strong&gt;rate limits&lt;/strong&gt;, and &lt;strong&gt;user experience at scale&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Dev builds not just your resume, but your &lt;strong&gt;intuition&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  When Worlds Collide - DSA Meets Dev
&lt;/h2&gt;

&lt;p&gt;DSA isn't just for whiteboards. It’s quietly powering real-world apps.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Search bars?&lt;/strong&gt; Behind that clean UI: binary search, trie.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Chat systems?&lt;/strong&gt; Queues, hashing, and optimized data structures for real-time flow.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pagination?&lt;/strong&gt; Sliding window and prefix sums make it feel “infinite.”&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Caching?&lt;/strong&gt; LRU logic, maps, and heaps doing the heavy lifting.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;DSA gives the “how.” Dev gives the “why.”&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Ever fixed a slow feature and realized:&lt;br&gt;&lt;br&gt;
“Wait… this is just a DSA problem with users instead of testcases”?&lt;br&gt;&lt;br&gt;
Exactly that.&lt;/p&gt;

&lt;p&gt;DSA trains you to think in edge cases.&lt;br&gt;&lt;br&gt;
Dev makes you build with edge users in mind.&lt;br&gt;&lt;br&gt;
Together? Clean logic, fast features, and few 3 am bug hunts.&lt;/p&gt;




&lt;h2&gt;
  
  
  My Path, My Puzzle
&lt;/h2&gt;

&lt;p&gt;I started with DSA because I thought it was the &lt;em&gt;only&lt;/em&gt; path to crack interviews.&lt;br&gt;&lt;br&gt;
Then came Dev, and suddenly I wasn’t solving problems, I was building stuff that mattered.&lt;/p&gt;

&lt;p&gt;The FOMO hit hard.&lt;br&gt;&lt;br&gt;
While others were pushing ratings, I was debugging CSS and setting up MongoDB clusters.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Now?&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
I'm trying to balance both:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Solving 2-3 DSA problems daily
&lt;/li&gt;
&lt;li&gt;Building projects that excite me
&lt;/li&gt;
&lt;li&gt;Writing blogs when I get time to make sense of all this chaos&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I'm not a pro yet, just a learner with a long to-do list and bugs as my bedtime stories.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Reality Check
&lt;/h2&gt;

&lt;p&gt;Still figuring things out?&lt;br&gt;&lt;br&gt;
Same here.&lt;/p&gt;

&lt;p&gt;One day it's system design docs, the next it's a DP bug eating your brain.&lt;br&gt;&lt;br&gt;
And in between? Googling "how to center a div" for the 47th time.&lt;/p&gt;

&lt;p&gt;Everyone’s journey looks different.&lt;br&gt;&lt;br&gt;
Some peak with DSA.&lt;br&gt;&lt;br&gt;
Some thrive in dev.&lt;br&gt;&lt;br&gt;
But most of us? We’re just out here trying, tinkering, switching tabs between LeetCode and localhost.&lt;/p&gt;

&lt;p&gt;So don’t stress the choice.&lt;br&gt;&lt;br&gt;
You don’t have to pick sides. You can push both buttons.&lt;/p&gt;

&lt;p&gt;&lt;a href="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%2Farticles%2Fxb24j11zpva5ee5ebapk.jpeg" class="article-body-image-wrapper"&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%2Farticles%2Fxb24j11zpva5ee5ebapk.jpeg" alt="Going with both dsa and dev" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But here's the real talk:&lt;br&gt;&lt;br&gt;
In the long run, &lt;strong&gt;you’ll need both&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;DSA builds your core logic and problem-solving mindset.
&lt;/li&gt;
&lt;li&gt;Dev teaches you how that logic lives, breathes, and scales in the real world.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;DSA vs Dev?&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Naa. It’s DSA &lt;em&gt;and&lt;/em&gt; Dev.&lt;/p&gt;

&lt;p&gt;Let them coexist. That’s how we level up.&lt;/p&gt;

</description>
      <category>dsa</category>
      <category>development</category>
      <category>programming</category>
      <category>discuss</category>
    </item>
    <item>
      <title>No Recursion, No Table, Still DP? My Shift From Brute Force to Intuition</title>
      <dc:creator>Guna Sai</dc:creator>
      <pubDate>Fri, 13 Jun 2025 13:07:02 +0000</pubDate>
      <link>https://forem.com/guna01/no-recursion-no-table-still-dp-my-shift-from-brute-force-to-intuition-5e6n</link>
      <guid>https://forem.com/guna01/no-recursion-no-table-still-dp-my-shift-from-brute-force-to-intuition-5e6n</guid>
      <description>&lt;h2&gt;
  
  
  Wait... Is This Even DP?
&lt;/h2&gt;

&lt;p&gt;When I started learning Dynamic Programming (DP), I thought I had it all figured out.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;First write recursion -&amp;gt;&lt;br&gt;
Then add memoization -&amp;gt;&lt;br&gt;
Then convert to tabulation -&amp;gt;&lt;br&gt;
And if you're feeling fancy, do space optimization.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Easy, right?&lt;/p&gt;

&lt;p&gt;So whenever I saw a tough problem, my brain immediately went:&lt;br&gt;&lt;br&gt;
&lt;em&gt;"Where’s the recursion? Let's add dp[i][j] and go"&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;That's how I thought DP was supposed to be.&lt;/p&gt;

&lt;p&gt;But recently, I solved a problem... and it broke that pattern completely.&lt;/p&gt;

&lt;p&gt;No recursion.&lt;br&gt;&lt;br&gt;
No DP array.&lt;br&gt;&lt;br&gt;
No &lt;code&gt;i&lt;/code&gt;, no &lt;code&gt;j&lt;/code&gt;, no table.  &lt;/p&gt;

&lt;p&gt;Still, it &lt;strong&gt;felt like&lt;/strong&gt; Dynamic Programming.&lt;br&gt;&lt;br&gt;
And that's when I paused and asked myself:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“Wait… is this even DP?”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Turned out, it was. Just in disguise.&lt;/p&gt;

&lt;p&gt;That's when I realized:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;DP isn't just about writing recursion and caching it.&lt;br&gt;&lt;br&gt;
It's more about spotting repeated work and finding smart ways to reuse it.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Mind blown&lt;/p&gt;


&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Max of Min for Every Window Size&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You're given an array &lt;code&gt;M&lt;/code&gt; of length &lt;code&gt;N&lt;/code&gt;. For every window size &lt;code&gt;i&lt;/code&gt; from 1 to &lt;code&gt;N&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Find all subarrays of size &lt;code&gt;i&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Take the &lt;strong&gt;minimum&lt;/strong&gt; of each subarray&lt;/li&gt;
&lt;li&gt;From those, find the &lt;strong&gt;maximum&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Store that in position &lt;code&gt;i&lt;/code&gt; of a new array &lt;code&gt;P&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Input: &lt;code&gt;M = [1, 2, 3]&lt;/code&gt;&lt;br&gt;&lt;br&gt;
Output: &lt;code&gt;P = [3, 2, 1]&lt;/code&gt;&lt;/p&gt;


&lt;h2&gt;
  
  
  My Brute Force
&lt;/h2&gt;

&lt;p&gt;The moment I read the problem, I felt confident.&lt;br&gt;&lt;br&gt;
"Sliding window + priority queue? Easy!"&lt;/p&gt;

&lt;p&gt;I wrote it quickly.&lt;br&gt;&lt;br&gt;
Clean logic. Passed the sample cases.  &lt;/p&gt;

&lt;p&gt;Hit submit...&lt;/p&gt;

&lt;p&gt;and &lt;strong&gt;TLE.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="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%2Farticles%2Fzpd6rdi1klykc7h0ytch.gif" class="article-body-image-wrapper"&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%2Farticles%2Fzpd6rdi1klykc7h0ytch.gif" alt="crying over TLE" width="480" height="478"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That hurt.&lt;br&gt;
That's when I knew…&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Maybe it's time to change my thinking.&lt;/p&gt;
&lt;/blockquote&gt;


&lt;h2&gt;
  
  
  Flipping the Perspective
&lt;/h2&gt;

&lt;p&gt;Up to this point, I was stuck thinking:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Fix a window size, slide it, take minimums."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;But then I came across a different way of thinking (shoutout to ChatGPT for the nudge):&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“What if instead of fixing the window… I fix the &lt;strong&gt;element&lt;/strong&gt;?”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That led to a much better question:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"In how many windows is this element the &lt;strong&gt;minimum&lt;/strong&gt;?"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This flipped everything.&lt;/p&gt;

&lt;p&gt;Using a &lt;strong&gt;monotonic stack&lt;/strong&gt;, I found:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;left[i]&lt;/code&gt;: index of the previous smaller element
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;right[i]&lt;/code&gt;: index of the next smaller element
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That gave me the range of window sizes where &lt;code&gt;arr[i]&lt;/code&gt; is the minimum.&lt;/p&gt;

&lt;p&gt;Now I could directly say:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;len&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="no"&gt;P&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;len&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;max&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="no"&gt;P&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;len&lt;/span&gt;&lt;span class="o"&gt;],&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Array:        [10, 20, 30, 50, 10, 70, 30]
Indexes:        0   1   2   3   4   5   6

Fix element:              ↑
                         arr[4] = 10

Find:
   left[4]  = 0   (index of previous smaller)
   right[4] = 7   (index of next smaller)

Window size where arr[4] is min = right - left - 1 = 7 - 0 - 1 = 6

So, 10 is the minimum in all windows of size 6 that include index 4.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  What DP Really Means
&lt;/h2&gt;

&lt;p&gt;Dynamic Programming isn't about sticking to recursion or memo tables.&lt;/p&gt;

&lt;p&gt;It's just... not doing the same work again and again.&lt;/p&gt;

&lt;p&gt;Sometimes it shows up as a clever stack.&lt;br&gt;&lt;br&gt;
Other times, it's a loop with a trick.&lt;br&gt;&lt;br&gt;
And occasionally, it's just thinking from the other side.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I don't know all of it yet, but I've started noticing patterns I never used to.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Maybe that's what DP is not a formula, but a way of noticing.&lt;/p&gt;




&lt;h2&gt;
  
  
  Some Patterns I Started Noticing
&lt;/h2&gt;

&lt;p&gt;After getting out of the “everything must be recursion” mindset, I started to notice some small things that helped.&lt;/p&gt;

&lt;p&gt;Like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If I keep doing the same thing on subarrays - maybe prefix sum or sliding window is the way.&lt;/li&gt;
&lt;li&gt;If I'm checking min or max in ranges a lot - maybe a monotonic stack can save me.&lt;/li&gt;
&lt;li&gt;If there are too many nested loops - maybe I can flip how I look at the problem.&lt;/li&gt;
&lt;li&gt;And if recursion doesn't even fit - maybe it's just about reusing stuff smartly.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Still figuring it all out, but these small shifts made a big difference for me.&lt;/p&gt;




&lt;h2&gt;
  
  
  Final Thought
&lt;/h2&gt;

&lt;p&gt;Okay yeah...&lt;br&gt;&lt;br&gt;
So all those “DP = recursion -&amp;gt; memo -&amp;gt; tabulation -&amp;gt; space optimization” steps?&lt;/p&gt;

&lt;p&gt;Kinda feels silly now. 😅&lt;/p&gt;

&lt;p&gt;This problem made me realize:  &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;DP is just &lt;em&gt;not doing the same thing twice&lt;/em&gt;, even if it doesn't look like typical DP.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And honestly?&lt;br&gt;&lt;br&gt;
That realization hit harder than a brute force TLE 💀&lt;/p&gt;

&lt;p&gt;I'm still figuring things out, still breaking stuff.&lt;br&gt;&lt;br&gt;
But now I don't reach for recursion every time. I try to &lt;strong&gt;think smarter&lt;/strong&gt;, not just code faster.&lt;/p&gt;

&lt;p&gt;&lt;a href="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%2Farticles%2F0wng4pzgvnttw76vdx3a.gif" class="article-body-image-wrapper"&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%2Farticles%2F0wng4pzgvnttw76vdx3a.gif" alt="finally smart" width="193" height="180"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Many wrong solutions, many TLEs and a few tears later… I think I just changed my thinking 🙃&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;&lt;strong&gt;P.S.&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Wanna know what broke my brain (in a good way)? Ping me. I’ve got more where that came from 😄&lt;/p&gt;

</description>
      <category>programming</category>
      <category>learning</category>
      <category>dsa</category>
      <category>dp</category>
    </item>
  </channel>
</rss>
