<?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: Ditikrushna Giri</title>
    <description>The latest articles on DEV Community by Ditikrushna Giri (@ditikrushna).</description>
    <link>https://dev.to/ditikrushna</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%2F278996%2Fdbd447aa-eeb9-4710-8280-38fdc463b656.jpeg</url>
      <title>DEV Community: Ditikrushna Giri</title>
      <link>https://dev.to/ditikrushna</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ditikrushna"/>
    <language>en</language>
    <item>
      <title>Nearest Smaller Element on Left  of an Array </title>
      <dc:creator>Ditikrushna Giri</dc:creator>
      <pubDate>Sun, 13 Feb 2022 08:11:07 +0000</pubDate>
      <link>https://dev.to/ditikrushna/nearest-smaller-element-on-left-of-an-array-h53</link>
      <guid>https://dev.to/ditikrushna/nearest-smaller-element-on-left-of-an-array-h53</guid>
      <description>&lt;p&gt;In this post, we are going to explore a very common question on Stack. The questions are the following ;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Nearest Smaller Element on Left &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's assume we have given an array/vector/list with some elements and our job is to figure out the nearest smaller element on the left side of the array.&lt;br&gt;
For instance :&lt;br&gt;
&lt;br&gt;
 &lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let list = [ 4 , 10 , 5 , 8 , 20 , 15 , 3 , 12]
result = [-1 , 4 , 4 , 5 , 8 , 8 , -1 , 3] 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;And the smaller element of right side of an array&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let list = [4 , 10 , 5 , 8 , 20 , 15 , 3 , 12]
result = [3 , 5 , 3 , 3 , 3 , 15 , 3 , 12 ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We will see two ways to solve the problems : &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Brute-force approach (using nested loops)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this approach we will use two loops. Outer loop will iterate over all the array items and inner loop will iterate over all the next elements of currently pointed by outer loop. Inner loop will check, if any smaller element will be found then loop will be terminated and if smaller element is not found then -1 will be added as result.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function nearestSmallerToLeft(list) {
  let result = [];
  for (let indexOne = 0; indexOne &amp;lt; list.length; indexOne++) {
    let flag = false;
    for (let indexTwo = indexOne - 1; indexTwo &amp;gt; -1; indexTwo--) {
      if (arr[indexOne] &amp;gt; arr[indexTwo]) {
        result.push(arr[indexTwo]);
        flag = true;
        break;
      }
    }
    if (!flag) {
      result.push(-1);
    }
  }

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

&lt;/div&gt;



&lt;p&gt;Time Complexity of the above solution would be O(n^2)&lt;br&gt;
Space Complexity would be : O(1) as we are not using any extra space . &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Using stack&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In case of this approach, we are using a stack. and the approach would be to start traversing elements of the given array from the start i.e., leftmost element and we will be putting the smallest element in the stack and whenever we get another smaller element then pop the stack and push the new element. Basically, we will be using a stack to keep values available to the left side and which are the smaller ones.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
class Stack {
    constructor(){
        this.stack = [] ;
    }

    isEmpty() {
        return this.stack.length === 0;
    }
    push(element){
        this.stack.push(element);
    }
    pop(){
        if(this.isEmpty()){
            throw 'Stack UnderFlow';
        }
        return this.stack.pop();
    }

    top(){
        if(this.isEmpty())
        throw null ;
        return this.stack[this.stack.length-1];
    }
}


function nearestSmallerToLeft(list){
    const stack = new Stack();
    let result = [];

    for(let index = 0 ; index &amp;lt; list.length ; index++){

        if(stack.isEmpty()){
            result.push(-1);
            stack.push(list[index]);
        }
        else if(!stack.isEmpty()){
            while(!stack.isEmpty() &amp;amp;&amp;amp; list[index]&amp;lt;stack.top()){
                stack.pop();
            }

            if(stack.isEmpty()){
                result.push(-1);
            }else{
                result.push(stack.top());
            }
            stack.push(arr[index]);
        }
    }

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

&lt;/div&gt;



&lt;p&gt;Time complexity of the above solution would be O(n) And the space complexity O(n) as we are using extra space for stack . &lt;/p&gt;

</description>
      <category>dsa</category>
      <category>leetcode</category>
      <category>javascript</category>
      <category>interview</category>
    </item>
    <item>
      <title>A short note on database design process </title>
      <dc:creator>Ditikrushna Giri</dc:creator>
      <pubDate>Sun, 31 Oct 2021 09:54:37 +0000</pubDate>
      <link>https://dev.to/ditikrushna/short-note-on-database-design-process-1bh1</link>
      <guid>https://dev.to/ditikrushna/short-note-on-database-design-process-1bh1</guid>
      <description>&lt;p&gt;Step - 1&lt;br&gt;
&lt;strong&gt;Requirements Gathering&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;Understanding what you want to do, and what you have is essential before you can dive into designing a database. &lt;/p&gt;

&lt;p&gt;Step - 2 &lt;br&gt;
&lt;strong&gt;Conceptual Design&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We specify the entities, columns, and their relationship. We may use an entity relationship (ER) diagram to visualize the database.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The output is:&lt;/strong&gt; A conceptual schema (described using a conceptual data model like ER model). &lt;/p&gt;

&lt;p&gt;Step - 3 &lt;br&gt;
&lt;strong&gt;Logical Design&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It’s concerned about data model mapping; mapping a conceptual schema (like ER model) into logical schema to provide a much detail description. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The output is:&lt;/strong&gt; A logical schema (described using a logical data model specific to the DBMS like relational model).&lt;/p&gt;

&lt;p&gt;Step - 4 &lt;br&gt;
&lt;strong&gt;Physical Design&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It describes the details of how data is stored. You start by defining (already modeled) tables, how the data is stored, define relationships, … in DBMS.&lt;br&gt;
This requires dealing with the DBMS, and could involve SQL.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The output is:&lt;/strong&gt; An internal (physical) schema (described using a physical data model).&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Multi Thread Model </title>
      <dc:creator>Ditikrushna Giri</dc:creator>
      <pubDate>Mon, 02 Aug 2021 11:10:09 +0000</pubDate>
      <link>https://dev.to/ditikrushna/multi-thread-model-5gn0</link>
      <guid>https://dev.to/ditikrushna/multi-thread-model-5gn0</guid>
      <description>&lt;p&gt;In multi thread model the server assigned incoming request to the new thread . Similar as a client server architecture the user generate a request while interacting with the application and then send to the server . &lt;/p&gt;

&lt;p&gt;The every times a new request comes in the server map a new thread from thread pool to the incoming request then the thread is responsible for process that request and sending back the response . &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;The number of thread is equal to the number of thread used from the thread pools .&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now , the thread present in the thread pools are limited to the based on the resource available . &lt;/p&gt;

&lt;p&gt;If we look closely we will find the limitation of multi thread model . Suppose we have millions of request coming in per second are the thread present in the thread pools are limited number there will be situation where all the thread are exhausted from the thread pools in that situation a incoming request has to wait until a thread process the last request and return back the result . &lt;/p&gt;

&lt;p&gt;&lt;em&gt;This is a situation of scalability which could be resolve by adding more resource and creating more thread inside the thread pools .&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Limitation of Multi Thread Model :&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In multi-thread model , for every request server creates a separate thread which handles that request . &lt;/li&gt;
&lt;li&gt;If a thread acquired a lock in the shared resource and it is 'exclusive lock ' it will block other threads . &lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>
Are you using the right CSS units? </title>
      <dc:creator>Ditikrushna Giri</dc:creator>
      <pubDate>Fri, 12 Mar 2021 14:52:27 +0000</pubDate>
      <link>https://dev.to/ditikrushna/are-you-using-the-right-css-units-25ee</link>
      <guid>https://dev.to/ditikrushna/are-you-using-the-right-css-units-25ee</guid>
      <description>&lt;p&gt;How to pick which CSS unit to use in which circumstance. I wish this was an easy question to answer but it really is one of those depends type of questions but in this case . It does actually depend on something more than others. &lt;/p&gt;

&lt;p&gt;So, In this article, we are going to generalize the above questions.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note: There is always an exception to pretty much all of these rules these are just rules of thumb.&lt;/em&gt; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Are you declaring a font size ?&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;You probably want to go with &lt;strong&gt;rem&lt;/strong&gt; they are &lt;strong&gt;relative units&lt;/strong&gt; but in the case of &lt;strong&gt;rem&lt;/strong&gt;, they are relative to the font size of the root element which is our HTML element most of the time that defaults to 16 px. &lt;strong&gt;rem&lt;/strong&gt; are better than using &lt;strong&gt;pixels&lt;/strong&gt; because they adapt to the user's system and browser preference whereas if you use pixels you going to locks things in and you can overwrite the user's preferences. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Are you declaring a width ?&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;This is one of the harder ones to answer because there are so many different things that you might be a width on but for the most part a &lt;strong&gt;percentage&lt;/strong&gt; is a nice direction to go on often coupled with a max-width. There are times also where a &lt;strong&gt;viewport width&lt;/strong&gt; might be a good unit to use but it can cause some trouble or some unintended consequences. Another unit that could be a really good fit is the &lt;strong&gt;ch&lt;/strong&gt; unit  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Are you declaring padding or margin ?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For the most part, going with either &lt;strong&gt;em&lt;/strong&gt; or &lt;strong&gt;rem&lt;/strong&gt; for these. It depends on whether you want the padding or margin to be consistent despite the element that you are setting it on or if you want it to adjust based on that element's font size. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Are you declaring a height ?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;percentage, rem, or viewport height&lt;/strong&gt; all are a good fit. &lt;/p&gt;

&lt;p&gt;Note: This is an incomplete article. Soon I am going to add more insight on it. If you are up for contribution please add your thought in the comment box. &lt;/p&gt;

&lt;p&gt;Happy Learning ;) &lt;/p&gt;

</description>
    </item>
    <item>
      <title>Introduction to Docker </title>
      <dc:creator>Ditikrushna Giri</dc:creator>
      <pubDate>Thu, 11 Mar 2021 06:11:34 +0000</pubDate>
      <link>https://dev.to/ditikrushna/introduction-to-docker-2b2</link>
      <guid>https://dev.to/ditikrushna/introduction-to-docker-2b2</guid>
      <description>&lt;p&gt;&lt;strong&gt;Introduction:&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;Can you recall how many times have you faced this issue that a code works on a developer system but the same code does not work on the tester system? &lt;/p&gt;

&lt;p&gt;Docker at a very basic level resolves this issue of an application working on one platform and not working on some other platform. However, there is much more to Docker. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Where does docker come into the picture?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;At a very high level the following steps are the stages of a software devlopment life cycle :  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Design &lt;/li&gt;
&lt;li&gt;Development &lt;/li&gt;
&lt;li&gt; Deployment &lt;/li&gt;
&lt;li&gt;Testing/Release &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Docker comes into the picture at the deployment stage of the software development lifecycle.&lt;/p&gt;

&lt;p&gt;Docker helps in making the process of application deployment very easy and efficient and resolves a lot of issues related to deploying applications.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is Docker?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Docker is the worlds leading software container platform.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;What does this mean?&lt;/em&gt; Let's understand in an easy way.&lt;/p&gt;

&lt;p&gt;Today, we talk about software it is not a single piece of code. However, it is an entire software stack a typical software application will consist of a group of front-end, back-end, databases, servers, libraries, and other  environment-dependent components and we also have to ensure that all these components work on our different and wide range of platforms &lt;/p&gt;

&lt;p&gt;It becomes very complex to ensure that each of ours software components works on every possible platform where the application is expected to run. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to solve this problem ?&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;To solve this problem let us look at a similar problem that existed many many years ago in the shipping industry and that was &lt;em&gt;how to transport different goods having different size, shape and requirements to a different location of the world&lt;/em&gt;   again the situations become very complex.  So, how did the shipping industry solve the problem? they solved the problem by using containers. So, containers are the standard boxes where the goods can be packaged in the standard way. Container made the shipping of goods very easy, efficient, and very cheap. And this revolutionized the shipping industry. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Coming to our problem:&lt;/strong&gt; it looks like we have a similar kind of solution and the solution is again using containers and using a standard way of shipping these containers and here comes Docker. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;Docker is a tool designed to make it easier to deploy and run applications by using containers and Containers to allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and ship it all out as one package.&lt;/em&gt; &lt;/p&gt;

&lt;p&gt;Now, you can imagine a developer will package all the software and its components into a box which we call a container, and Docker will take care of shipping this container to all the possible platform in a standard way and this is how we resolve the issue of an application working on one environment and not working another. And you can see there is a clear separation of concerns a developer will now really focus on creating the code and the software and will package the software along with all its dependencies into a container and will not worry about how this will be deployed on what all platform it has to be deployed and so on and docker makes this process of deploying software.  &lt;/p&gt;

&lt;p&gt;Happy Learning ;)   &lt;/p&gt;

</description>
      <category>docker</category>
      <category>devops</category>
    </item>
    <item>
      <title>Events and Event Emitter in Node.js </title>
      <dc:creator>Ditikrushna Giri</dc:creator>
      <pubDate>Wed, 10 Mar 2021 11:45:27 +0000</pubDate>
      <link>https://dev.to/ditikrushna/events-and-event-emitter-in-node-js-2ek5</link>
      <guid>https://dev.to/ditikrushna/events-and-event-emitter-in-node-js-2ek5</guid>
      <description>&lt;p&gt;One of the core concepts of a node is the concept of events. In fact, a lot of nodes' core functionality is based on this concept of events.&lt;/p&gt;

&lt;p&gt;Definition: &lt;br&gt;
The event is basically a signal that indicates that something has happened in our applications.&lt;/p&gt;

&lt;p&gt;Example :&lt;br&gt;
In node, we have a class called HTTP that we can use to build a web server so we listen on a given port, and every time we receive a request on that port that HTTP class raises an event. Now our job is to respond to that event which basically involves reading that request and returning the right response. &lt;/p&gt;

&lt;p&gt;Let's see how we can work with the event emitter: &lt;/p&gt;

&lt;p&gt;Step-1: &lt;br&gt;
 let's load the events modules : &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const  EventEmitter  =  require('events');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Note:&lt;/em&gt;&lt;/strong&gt; In terms of naming the first letter of every word in uppercase this is a convention that indicates that this event emitter is a class, not a function or simple value.&lt;/p&gt;

&lt;p&gt;When we call the &lt;code&gt;require&lt;/code&gt; function we get the event emitter class. Now, we need to create an instance of this class. And that can be done with the following piece of code &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const emitter = new EventEmitter() ;  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This emitter has a bunch of methods use this &lt;a href="https://nodejs.org/api/events.html#events_class_eventemitter"&gt;link&lt;/a&gt; for all list of events that offer emitter class. &lt;/p&gt;

&lt;p&gt;Even though we have more than ten methods most of the time we use only two of these methods one is &lt;strong&gt;emit&lt;/strong&gt; that we use to raise an event. If you do not know the meaning of the &lt;strong&gt;emit&lt;/strong&gt;: means making noise or produce something in our case we are going to making noise in our application. We are signaling that an event has happened.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;emitter.emit('')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;We pass an argument that is the name of the event let say &lt;strong&gt;messageLogged&lt;/strong&gt; in the future we are going to extend our loger module and every time we log a message we are going to raise an event called &lt;strong&gt;message logged&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;Now , if we run the following codes nothing is going to append : &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const EventEmitter = require('events')
const emitter = new EventEmitter();
emitter.emit('messageLogged');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;because we have raised an event here but nowhere in our application we have registered a listener that is interested in that event. &lt;br&gt;
&lt;strong&gt;Listener:&lt;/strong&gt;  is a function that will be called when that event is raised. &lt;/p&gt;

&lt;p&gt;Let's register a listener that will be called when the message log event is raised for that we will use the &lt;strong&gt;on&lt;/strong&gt; method and that takes two arguments first one will be the name of the event in our case &lt;strong&gt;messageLogged&lt;/strong&gt; and the second one is a callback function or the actual listener.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const EventEmitter = require('events')
const emitter = new EventEmitter();
//Register a listener 
emitter.on('messageLogged',function(){
  console.log("Listener is called")
});
emitter.emit('messageLogged');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; The order is important here if you register listener after calling the &lt;strong&gt;emit&lt;/strong&gt; method nothing would have happened because when we call the &lt;strong&gt;emit&lt;/strong&gt; method the &lt;strong&gt;emitter&lt;/strong&gt; iterates over all the registered listeners and calls them synchronously. &lt;/p&gt;

&lt;p&gt;This is the basis of raising events and handling them using event emitter class. &lt;/p&gt;

&lt;p&gt;Happy Coding ;) &lt;/p&gt;

</description>
      <category>node</category>
      <category>backend</category>
      <category>eventemitter</category>
    </item>
  </channel>
</rss>
