<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Software Guy]]></title><description><![CDATA[Software Guy Blog is where I share tech articles on coding, web development, databases, and more. I simplify complex topics into easy guides and insights to help developers grow.]]></description><link>https://blog.softwareguy.xyz</link><image><url>https://cdn.hashnode.com/res/hashnode/image/upload/v1746108982885/bd8c73ff-7a92-43b0-94dd-d55d8c94e64a.webp</url><title>Software Guy</title><link>https://blog.softwareguy.xyz</link></image><generator>RSS for Node</generator><lastBuildDate>Fri, 15 May 2026 03:44:22 GMT</lastBuildDate><atom:link href="https://blog.softwareguy.xyz/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Time Complexity Over Line Count: Why 100 Lines May Run Faster Than 2]]></title><description><![CDATA[Imagine searching for a solution on stackoverflow you came across two solution one is of 2 lines another is 10 lines there is a great chance that you will pick the one with 2 lines. But wait that’s now always right.

Understanding Time Complexity
Tim...]]></description><link>https://blog.softwareguy.xyz/time-complexity-over-line-count-why-100-lines-may-run-faster-than-2</link><guid isPermaLink="true">https://blog.softwareguy.xyz/time-complexity-over-line-count-why-100-lines-may-run-faster-than-2</guid><category><![CDATA[ChaiCode]]></category><category><![CDATA[#chaiaurdsa]]></category><category><![CDATA[timecomplexity]]></category><dc:creator><![CDATA[Ayush Kumar Singh]]></dc:creator><pubDate>Sun, 04 May 2025 15:18:53 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1746371922186/0b721597-112c-47bc-af75-ce0fc237638b.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<blockquote>
<p>Imagine searching for a solution on stackoverflow you came across two solution one is of 2 lines another is 10 lines there is a great chance that you will pick the one with 2 lines. But wait that’s now always right.</p>
</blockquote>
<h2 id="heading-understanding-time-complexity">Understanding Time Complexity</h2>
<p>Time Complexity describes how much time the code will take to execute with increasing input size.</p>
<p>Say your code executes fine for 10 inputs but is slow for 1000 inputs; the time complexity will help you understand why.</p>
<p>Time Complexity can be categorised based on 3 scenarios:</p>
<ol>
<li><p>Best Case Scenario</p>
</li>
<li><p>Average Case Scenario</p>
</li>
<li><p>Worst Case Scenario</p>
</li>
</ol>
<p>The best case, average case and worst case are denoted by Ω(Omega), θ(Theta) and O(Big O), respectively.</p>
<p>Time complexity can be Constant, Logarithmic, Linear, Linear Logarithmic, Quadratic, Exponential and Factorial, where constant means the least time and factorial is the maximum time.</p>
<h2 id="heading-a-simple-analogy">A Simple Analogy</h2>
<p>Imagine, while you are using Google Maps to navigate, sometimes maps suggest you take a longer route instead of a shortcut, and by doing so, you can save time since the shortcut has potholes, traffic lights, and traffic jams, which can increase travel time.</p>
<p>Similarly, in coding, your 2 lines of code may be forcing the machine to iterate multiple times, whereas the 10 lines of code can do it easily.</p>
<h2 id="heading-code-example">Code Example</h2>
<p>Let’s consider a very common problem, “The Two Sum Problem”</p>
<blockquote>
<p><strong>Problem</strong>: Given an array of integers and a target value, find if there are two numbers that add up to the target.</p>
</blockquote>
<h3 id="heading-the-brute-force-approach">The Brute Force Approach</h3>
<pre><code class="lang-cpp"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;iostream&gt;</span></span>
<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
    <span class="hljs-keyword">int</span> arr[] = {<span class="hljs-number">2</span>, <span class="hljs-number">7</span>, <span class="hljs-number">11</span>, <span class="hljs-number">15</span>};
    <span class="hljs-keyword">int</span> target = <span class="hljs-number">9</span>, n = <span class="hljs-number">4</span>;

    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; n; i++)
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> j = i + <span class="hljs-number">1</span>; j &lt; n; j++)
            <span class="hljs-keyword">if</span> (arr[i] + arr[j] == target)
                <span class="hljs-built_in">cout</span> &lt;&lt; i &lt;&lt; <span class="hljs-string">" "</span> &lt;&lt; j &lt;&lt; <span class="hljs-built_in">endl</span>;

    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<p>This code looks short and easy to implement, but it’s slow during execution because here we are checking every possible pair of numbers in the array. It has two nested loops, making it inefficient for larger arrays.</p>
<h3 id="heading-optimised-code">Optimised Code</h3>
<pre><code class="lang-cpp"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;iostream&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;unordered_map&gt;</span></span>
<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
    <span class="hljs-keyword">int</span> arr[] = {<span class="hljs-number">2</span>, <span class="hljs-number">7</span>, <span class="hljs-number">11</span>, <span class="hljs-number">15</span>};
    <span class="hljs-keyword">int</span> target = <span class="hljs-number">9</span>, n = <span class="hljs-number">4</span>;

    <span class="hljs-built_in">unordered_map</span>&lt;<span class="hljs-keyword">int</span>, <span class="hljs-keyword">int</span>&gt; <span class="hljs-built_in">map</span>; <span class="hljs-comment">// stores number and its index</span>

    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; n; i++) {
        <span class="hljs-keyword">int</span> complement = target - arr[i];

        <span class="hljs-keyword">if</span> (<span class="hljs-built_in">map</span>.find(complement) != <span class="hljs-built_in">map</span>.end()) {
            <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-built_in">map</span>[complement] &lt;&lt; <span class="hljs-string">" "</span> &lt;&lt; i &lt;&lt; <span class="hljs-built_in">endl</span>;
            <span class="hljs-keyword">break</span>;
        }

        <span class="hljs-built_in">map</span>[arr[i]] = i;
    }

    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<p>This solution uses a hash map to remember the numbers we've already seen. For each new number, it checks if the complement (target - current number) is already in the map. This means we only need to loop through the array once, which makes it way faster than the brute-force approach.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>In conclusion, when evaluating code, it's crucial to look beyond the number of lines and consider the time complexity. A shorter code snippet might seem appealing, but it can be less efficient, especially with larger inputs. Understanding and applying the principles of time complexity can lead to more efficient and scalable solutions.</p>
<p>Check out my full portfolio on <a target="_blank" href="https://softwareguy.xyz">https://</a><a target="_blank" href="https://softwareguy.xyz">softwareguy.xyz</a></p>
]]></content:encoded></item><item><title><![CDATA[How to Build a REST API with Express.js and MongoDB in 10 Minutes]]></title><description><![CDATA[Introduction
In this guide, we'll quickly build a simple REST API using Express.js and MongoDB. Whether you're new to backend development or need a refresher, this tutorial will help you start with a basic CRUD (Create, Read, Update, Delete) API.
Wha...]]></description><link>https://blog.softwareguy.xyz/how-to-build-a-rest-api-with-expressjs-and-mongodb-in-10-minutes</link><guid isPermaLink="true">https://blog.softwareguy.xyz/how-to-build-a-rest-api-with-expressjs-and-mongodb-in-10-minutes</guid><category><![CDATA[MongoDB]]></category><category><![CDATA[Express.js]]></category><category><![CDATA[REST API]]></category><category><![CDATA[APIs]]></category><dc:creator><![CDATA[Ayush Kumar Singh]]></dc:creator><pubDate>Thu, 03 Apr 2025 11:54:58 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1743681214882/f9317580-0d95-4d28-bf8d-abe02b1394bb.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-introduction">Introduction</h2>
<p>In this guide, we'll quickly build a simple <strong>REST API</strong> using <strong>Express.js</strong> and <strong>MongoDB</strong>. Whether you're new to backend development or need a refresher, this tutorial will help you start with a basic CRUD (Create, Read, Update, Delete) API.</p>
<h3 id="heading-what-youll-learn">What You'll Learn:</h3>
<ul>
<li><p>Setting up an Express.js server</p>
</li>
<li><p>Connecting to MongoDB</p>
</li>
<li><p>Creating API routes</p>
</li>
<li><p>Performing CRUD operations</p>
</li>
<li><p>Testing the API using Postman</p>
</li>
</ul>
<h3 id="heading-prerequisites">Prerequisites:</h3>
<ul>
<li><p>Node.js installed</p>
</li>
<li><p>Basic understanding of JavaScript</p>
</li>
<li><p>MongoDB Atlas account (or local MongoDB setup)</p>
</li>
</ul>
<hr />
<h2 id="heading-step-1-setting-up-the-project">Step 1: Setting Up the Project</h2>
<p>Open your terminal and create a new project:</p>
<pre><code class="lang-sh">mkdir express-mongo-api &amp;&amp; <span class="hljs-built_in">cd</span> express-mongo-api
npm init -y
</code></pre>
<p>Install required dependencies:</p>
<pre><code class="lang-sh">npm install express mongoose dotenv cors body-parser
</code></pre>
<ul>
<li><p><strong>express</strong> – Web framework for Node.js</p>
</li>
<li><p><strong>mongoose</strong> – ODM for MongoDB</p>
</li>
<li><p><strong>dotenv</strong> – Manage environment variables</p>
</li>
<li><p><strong>cors</strong> – Handle cross-origin requests</p>
</li>
<li><p><strong>body-parser</strong> – Parse incoming JSON data</p>
</li>
</ul>
<hr />
<h2 id="heading-step-2-connecting-to-mongodb">Step 2: Connecting to MongoDB</h2>
<p>Create a <code>.env</code> file in the root directory and add your MongoDB connection string:</p>
<pre><code class="lang-plaintext">MONGO_URI=your_mongodb_connection_string
PORT=5000
</code></pre>
<p>Now, create a file <code>db.js</code> to establish the database connection:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> mongoose = <span class="hljs-built_in">require</span>(<span class="hljs-string">'mongoose'</span>);
<span class="hljs-built_in">require</span>(<span class="hljs-string">'dotenv'</span>).config();

mongoose.connect(process.env.MONGO_URI, { <span class="hljs-attr">useNewUrlParser</span>: <span class="hljs-literal">true</span>, <span class="hljs-attr">useUnifiedTopology</span>: <span class="hljs-literal">true</span> })
  .then(<span class="hljs-function">() =&gt;</span> <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'MongoDB Connected'</span>))
  .catch(<span class="hljs-function"><span class="hljs-params">err</span> =&gt;</span> <span class="hljs-built_in">console</span>.error(err));

<span class="hljs-built_in">module</span>.exports = mongoose;
</code></pre>
<hr />
<h2 id="heading-step-3-defining-the-user-model">Step 3: Defining the User Model</h2>
<p>Inside a <code>models</code> folder, create <code>User.js</code>:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> mongoose = <span class="hljs-built_in">require</span>(<span class="hljs-string">'mongoose'</span>);

<span class="hljs-keyword">const</span> userSchema = <span class="hljs-keyword">new</span> mongoose.Schema({
  <span class="hljs-attr">name</span>: <span class="hljs-built_in">String</span>,
  <span class="hljs-attr">email</span>: <span class="hljs-built_in">String</span>,
  <span class="hljs-attr">age</span>: <span class="hljs-built_in">Number</span>
});

<span class="hljs-built_in">module</span>.exports = mongoose.model(<span class="hljs-string">'User'</span>, userSchema);
</code></pre>
<hr />
<h2 id="heading-step-4-building-api-routes">Step 4: Building API Routes</h2>
<p>Create a <code>routes</code> folder and add <code>userRoutes.js</code>:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> express = <span class="hljs-built_in">require</span>(<span class="hljs-string">'express'</span>);
<span class="hljs-keyword">const</span> router = express.Router();
<span class="hljs-keyword">const</span> User = <span class="hljs-built_in">require</span>(<span class="hljs-string">'../models/User'</span>);

<span class="hljs-comment">// Create a user</span>
router.post(<span class="hljs-string">'/'</span>, <span class="hljs-keyword">async</span> (req, res) =&gt; {
  <span class="hljs-keyword">try</span> {
    <span class="hljs-keyword">const</span> user = <span class="hljs-keyword">new</span> User(req.body);
    <span class="hljs-keyword">await</span> user.save();
    res.status(<span class="hljs-number">201</span>).json(user);
  } <span class="hljs-keyword">catch</span> (err) {
    res.status(<span class="hljs-number">500</span>).json({ <span class="hljs-attr">error</span>: err.message });
  }
});

<span class="hljs-comment">// Get all users</span>
router.get(<span class="hljs-string">'/'</span>, <span class="hljs-keyword">async</span> (req, res) =&gt; {
  <span class="hljs-keyword">const</span> users = <span class="hljs-keyword">await</span> User.find();
  res.json(users);
});

<span class="hljs-comment">// Get a user by ID</span>
router.get(<span class="hljs-string">'/:id'</span>, <span class="hljs-keyword">async</span> (req, res) =&gt; {
  <span class="hljs-keyword">const</span> user = <span class="hljs-keyword">await</span> User.findById(req.params.id);
  res.json(user);
});

<span class="hljs-comment">// Update a user</span>
router.put(<span class="hljs-string">'/:id'</span>, <span class="hljs-keyword">async</span> (req, res) =&gt; {
  <span class="hljs-keyword">const</span> user = <span class="hljs-keyword">await</span> User.findByIdAndUpdate(req.params.id, req.body, { <span class="hljs-attr">new</span>: <span class="hljs-literal">true</span> });
  res.json(user);
});

<span class="hljs-comment">// Delete a user</span>
router.delete(<span class="hljs-string">'/:id'</span>, <span class="hljs-keyword">async</span> (req, res) =&gt; {
  <span class="hljs-keyword">await</span> User.findByIdAndDelete(req.params.id);
  res.json({ <span class="hljs-attr">message</span>: <span class="hljs-string">'User deleted'</span> });
});

<span class="hljs-built_in">module</span>.exports = router;
</code></pre>
<hr />
<h2 id="heading-step-5-setting-up-the-express-server">Step 5: Setting Up the Express Server</h2>
<p>Create <code>server.js</code> in the root directory:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> express = <span class="hljs-built_in">require</span>(<span class="hljs-string">'express'</span>);
<span class="hljs-built_in">require</span>(<span class="hljs-string">'dotenv'</span>).config();
<span class="hljs-built_in">require</span>(<span class="hljs-string">'./db'</span>); <span class="hljs-comment">// Connect to MongoDB</span>

<span class="hljs-keyword">const</span> app = express();
app.use(express.json()); <span class="hljs-comment">// Middleware to parse JSON</span>
app.use(<span class="hljs-string">'/users'</span>, <span class="hljs-built_in">require</span>(<span class="hljs-string">'./routes/userRoutes'</span>)); <span class="hljs-comment">// Use user routes</span>

<span class="hljs-keyword">const</span> PORT = process.env.PORT || <span class="hljs-number">5000</span>;
app.listen(PORT, <span class="hljs-function">() =&gt;</span> <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Server running on port <span class="hljs-subst">${PORT}</span>`</span>));
</code></pre>
<p>Run the server:</p>
<pre><code class="lang-sh">node server.js
</code></pre>
<p>You should see <strong>"MongoDB Connected"</strong> and <strong>"Server running on port 5000"</strong> in your terminal.</p>
<hr />
<h2 id="heading-step-6-testing-the-api-with-postman">Step 6: Testing the API with Postman</h2>
<p>Use <strong>Postman</strong> or <strong>cURL</strong> to test the API:</p>
<h3 id="heading-create-a-user-post-users">Create a user (POST <code>/users</code>)</h3>
<pre><code class="lang-json">{
  <span class="hljs-attr">"name"</span>: <span class="hljs-string">"John Doe"</span>,
  <span class="hljs-attr">"email"</span>: <span class="hljs-string">"johndoe@example.com"</span>,
  <span class="hljs-attr">"age"</span>: <span class="hljs-number">25</span>
}
</code></pre>
<h3 id="heading-get-all-users-get-users">Get all users (GET <code>/users</code>)</h3>
<p>Returns an array of users.</p>
<h3 id="heading-get-a-user-by-id-get-usersid">Get a user by ID (GET <code>/users/:id</code>)</h3>
<p>Fetches a specific user by their ID.</p>
<h3 id="heading-update-a-user-put-usersid">Update a user (PUT <code>/users/:id</code>)</h3>
<p>Send an updated JSON object.</p>
<h3 id="heading-delete-a-user-delete-usersid">Delete a user (DELETE <code>/users/:id</code>)</h3>
<p>Removes a user from the database.</p>
<hr />
<h2 id="heading-optional-deploying-the-api">Optional: Deploying the API</h2>
<ul>
<li><p>Deploy to <strong>Render, Vercel, or Railway</strong> for free hosting.</p>
</li>
<li><p>Use <strong>MongoDB Atlas</strong> instead of local MongoDB.</p>
</li>
<li><p>Add authentication using <strong>JWT (JSON Web Tokens).</strong></p>
</li>
</ul>
<hr />
<p>Check out my full portfolio on <a target="_blank" href="https://softwareguy.xyz">softwareguy.xyz</a></p>
]]></content:encoded></item></channel></rss>