<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Shrey Banugaria</title>
    <description>The latest articles on DEV Community by Shrey Banugaria (@shreybanugariya).</description>
    <link>https://dev.to/shreybanugariya</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%2F524317%2Fb1d185f8-624b-4fc8-9681-a0146bd7485f.jpg</url>
      <title>DEV Community: Shrey Banugaria</title>
      <link>https://dev.to/shreybanugariya</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shreybanugariya"/>
    <language>en</language>
    <item>
      <title>Understanding Event Loop with an Interview Question</title>
      <dc:creator>Shrey Banugaria</dc:creator>
      <pubDate>Mon, 06 May 2024 07:41:36 +0000</pubDate>
      <link>https://dev.to/shreybanugariya/understanding-event-loop-with-an-interivew-question-1p56</link>
      <guid>https://dev.to/shreybanugariya/understanding-event-loop-with-an-interivew-question-1p56</guid>
      <description>&lt;p&gt;Here is the JS code, you have to guess the output. Also, you have to mention why that particular output&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;br&gt;
console.log('A');&lt;br&gt;
setTimeout(() =&amp;gt; console.log('B'), 0);&lt;br&gt;
Promise.resolve().then(() =&amp;gt; console.log('C'));&lt;br&gt;
setTimeout(() =&amp;gt; console.log('D'), 0);&lt;br&gt;
console.log('E');&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If your answer is &lt;code&gt;AE CBD&lt;/code&gt;. You are correct. &lt;br&gt;
Let's see how&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1 Synchronous Execution:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;console.log('A');&lt;/code&gt; and &lt;code&gt;console.log('E');&lt;/code&gt; are executed first because they are synchronous statements. They don't involve any waiting for asynchronous operations.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2 setTimeout and Promises:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;setTimeout(() =&amp;gt; console.log('B'), 0);&lt;/code&gt; and &lt;code&gt;Promise.resolve().then(() =&amp;gt; console.log('C'));&lt;/code&gt; are both asynchronous operations.&lt;br&gt;
However, the delay specified in setTimeout (0 milliseconds) doesn't guarantee immediate execution after synchronous code.&lt;br&gt;
Promises are typically processed before setTimeout callbacks due to the way JavaScript's event loop handles tasks.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3 Event Loop:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;JavaScript uses an event loop to manage synchronous and asynchronous operations.&lt;br&gt;
After executing synchronous code &lt;code&gt;(console.log('A');&lt;/code&gt; and &lt;code&gt;console.log('E');)&lt;/code&gt;, the event loop checks for any tasks in the "microtask queue."&lt;br&gt;
&lt;code&gt;Promises&lt;/code&gt; are queued in the microtask queue, while &lt;code&gt;setTimeout&lt;/code&gt; callbacks are placed in the "task queue."&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4 Microtask vs Task Queue:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The event loop processes the microtask queue before the task queue in each iteration.&lt;br&gt;
Since &lt;code&gt;Promise.resolve().then(...)&lt;/code&gt; creates a resolved promise immediately, it gets pushed to the microtask queue.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5 Execution Order:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Therefore, the resolved promise's callback &lt;code&gt;(console.log('C');&lt;/code&gt; gets executed first from the microtask queue.&lt;br&gt;
setTimeout Callbacks:&lt;/p&gt;

&lt;p&gt;After processing the microtask queue, the event loop moves to the task queue.&lt;br&gt;
Even though both setTimeout callbacks have a delay of 0, they don't run immediately. They are placed in the task queue.&lt;br&gt;
In the next iteration of the event loop, the task queue is processed, and both setTimeout callbacks are scheduled for execution.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;6 Non-deterministic Order&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;Due to the nature of the event loop and potential browser optimizations, the order of execution for the two setTimeout callbacks with a delay of 0 milliseconds is not guaranteed to be consistent.&lt;/p&gt;

&lt;p&gt;In this case, it might execute &lt;code&gt;console.log('B');&lt;/code&gt; before &lt;code&gt;console.log('D');&lt;/code&gt;, leading to the output &lt;code&gt;AE C B D&lt;/code&gt;.&lt;br&gt;
Remember: The order of setTimeout callbacks with 0 delay is not strictly deterministic in JavaScript. However, Promises take precedence over setTimeout in the event loop, ensuring &lt;code&gt;console.log('C');&lt;/code&gt; executes before both &lt;code&gt;setTimeout&lt;/code&gt; callbacks.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>MongoDB Aggregation Interview Questions (Level: Easy)</title>
      <dc:creator>Shrey Banugaria</dc:creator>
      <pubDate>Tue, 02 Jan 2024 09:13:39 +0000</pubDate>
      <link>https://dev.to/shreybanugariya/mongodb-aggregation-pipeline-interview-questions-level-easy-2274</link>
      <guid>https://dev.to/shreybanugariya/mongodb-aggregation-pipeline-interview-questions-level-easy-2274</guid>
      <description>&lt;p&gt;There are several common concepts which are repeated in interviews from beginners to advanced interviews. Depending on the requirements the questions asked may vary; If working with the large data you might face questions on Sharding, Replication and Managing cluster along with indexing and improving Db queries. Different questions of search applications, transactions and more. The common area is &lt;strong&gt;Aggregation&lt;/strong&gt;. I am covering the basic ones.&lt;/p&gt;

&lt;p&gt;We will start with concepts which could be asked in the interview&lt;/p&gt;




&lt;h2&gt;
  
  
  Frequency
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Q&lt;/strong&gt;. Determine the most frequently occurring city in a collection of customer addresses.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A&lt;/strong&gt;. We will group the documents by city names and count their occurrences and sort the count by descending order and only show the first one which is highest occuring&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;db.customers.aggregate([
  { $group: { 
      _id: "$city", 
      count: { $sum: 1 } 
  }},
  { $sort: { count: -1 } },
  { $limit: 1 }
])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Conditional Group
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Q&lt;/strong&gt;. Calculate the total sales for each product category, but only include products with a price over $50 and a quantity in stock greater than 10.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A&lt;/strong&gt;. Total Sales = &lt;code&gt;Quantity of Product Sold x its Price&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We will find the products which matches the condition and then will group by its category&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;db.products.aggregate([
  { $match: { price: { $gt: 50 }, quantity: { $gt: 10 } } },
  { $group: { 
      _id: "$productCategory", 
      totalSales: { $sum: { $multiply: ["$price", "$quantity"] } } 
  }}
])

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

&lt;/div&gt;






&lt;h2&gt;
  
  
  Moving Average
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Q&lt;/strong&gt; Calculate a 3-day moving average of stock prices. Assume each document has a date and price field.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A&lt;/strong&gt; First we will sort documents date wise, then push the price of the stock in array and take the average of the last 3 values of the array&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;db.stockData.aggregate([
{
  $sort: { $date: 1 } //ASC
},
{
  $group: {
    _id: null,
    priceArray: {$push: "$stockPrice"}
  }
},
{
  $project: {
    threeDayAvg: {
      $avg: {
        $slice: ["$priceArray", -3] // -3 to get last 3 values
      }
    }
  }
}
])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;Other questions consist of the text search with &lt;code&gt;$regex&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;db.users.find([
  $or: [
    { $firstName: {$regex: searchedString }},
    { $lastName: {$regex: searchedString }}
  ]
])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;One with using &lt;strong&gt;Bucket&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Q&lt;/strong&gt;. Group the Students by the total marks range above 70 and count the number of students&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;db.students.aggregate([
 {
   $bucket: {
    $groupBy: 'totalMarks',
    boundaries: [70, 80, 90, 100],
    default: "Other", // We dont want to through error if the student doesnot fall in the marks rage
    output: {
      count: { $sun: 1 }
    }
   }
 }
])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;Q&lt;/strong&gt;. Given a collection of blog posts with tags nested inside an array, find the tags with maximum occurrences.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;db.blogs.aggreagate([
  { $unwind: "$tags" },
  { $sort: -1 },
  { $limit: 1 } 
])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Suggest MongoDB aggregation pipeline questions for medium and advance level and we will solve it together. Till then Happy Coding 🚀👨‍💻&lt;/p&gt;

&lt;p&gt;Ping me on &lt;br&gt;
LinkedIn: &lt;a href="https://www.linkedin.com/in/shrey-banugaria/"&gt;Shrey Banugaria&lt;/a&gt;&lt;/p&gt;

</description>
      <category>mongodb</category>
      <category>interview</category>
      <category>learning</category>
      <category>backenddevelopment</category>
    </item>
  </channel>
</rss>
