<?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" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Programming Patterns - Medium]]></title>
        <description><![CDATA[Learn data structures and algorithms by  understanding and analyzing the patterns in the problems - Medium]]></description>
        <link>https://medium.com/programming-patterns?source=rss----877139141b5c---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Programming Patterns - Medium</title>
            <link>https://medium.com/programming-patterns?source=rss----877139141b5c---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 22:02:01 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/programming-patterns" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Docker — a containerization approach]]></title>
            <link>https://medium.com/programming-patterns/docker-a-containerization-approach-9ade479f2d32?source=rss----877139141b5c---4</link>
            <guid isPermaLink="false">https://medium.com/p/9ade479f2d32</guid>
            <category><![CDATA[docker]]></category>
            <dc:creator><![CDATA[Saurabh Singh]]></dc:creator>
            <pubDate>Sun, 08 Aug 2021 17:37:47 GMT</pubDate>
            <atom:updated>2021-08-08T17:37:47.745Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>Docker — a containerization approach</strong></h3><p>This article will provide the conceptual &amp; practical knowledge of Docker and its top commands docker, containerization, virtualization, top docker commands.</p><p>Docker is a tool that makes it easier to create, deploy and run applications using the containerization approach. In layman terms, it is a platform where you can build your application and package all the dependent assemblies into a container and these containers can ship to run on machines (either cloud or on-premise).</p><p><strong>Containerization is an enhanced or evolved version of virtualization (Virtual Machines)</strong></p><p>It’s similar to a virtual machine but instead of doing set up an operating system, we can run these containers at the kernel level and that’s the reason these are cost-effective and we also do not need to pre-allocate RAM to each and every container.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*qdKjdmM2fIAQEwtd-Opbog.png" /></figure><p>In the above diagram, we can see that each application will run on separate containers and have their own set of dependencies, it means they are independent of other applications. This is one of the fundamentals of Microservice architecture as well.</p><p><strong>Why do we need Docker?</strong></p><p>Let’s assume we are building an enterprise application and we have n number of components and all these components have multiple dependencies with different environments, for example, dev, test, and production. So, the team will spend a good amount of time in deployment.</p><p>Similarly, when a new developer joins the team, he/she will have to invest a lot of time and energy to prepare a developer machine, and nowadays, developers would like to use different languages &amp; technologies. For example, few prefer to write API in C#, Java, node.js which is efficiently possible if we are running our application in containerization approach.</p><ul><li>It’s lightweight and takes significantly less time to start than Server machines.</li><li>It also helps to increase the performance because resources are not wasted running unnecessary services.</li><li>It’s also the first choice for Microservice architecture.</li><li>It enables application portability.</li><li>The number of systems can be scaled up and down easily as per the requirement.</li><li>It also helps to reduce the production issues due to different environment setups. For example, we have seen multiple times developer complaints that code is working fine at my end but not at QA’s end.</li></ul><p>We can achieve this using virtual machines as well, but it won’t be very efficient.</p><p><strong>Before Docker?</strong></p><p>We used Virtualization before containers, we can create a guest operating system on top of the host operating system. This helps developers to run multiple virtual machines on top of the same host.</p><p>We can install virtual-box from Oracle and create different virtual machines and similarly, there are multiple software available that helps to achieve virtualization.</p><p><strong>Hypervisor</strong> is an important component that decouples the virtual machine from the host and dynamically allocates computing resources.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/497/1*vzuJZ2SbT-NNUam-Dih0nw.png" /></figure><p><strong>Key Advantages:</strong></p><ul><li>Run multiple OS on one physical machine.</li><li>Provide fault and security isolation at the hardware level.</li><li>Move and copy the state of virtual machines</li></ul><p><strong>Key Disadvantages:</strong></p><ul><li>Performance degradation, as system resources are divided between virtual machines.</li><li>The bootup process takes time.</li></ul><p><strong>How to setup Docker:</strong></p><p>If we are using Windows OS or Mac OS then Docker desktop is a way to go. Make sure Hyper-V and Containers Windows feature must be enabled.</p><p><strong>Windows</strong>: <a href="https://docs.docker.com/docker-for-windows/install/">https://docs.docker.com/docker-for-windows/install/</a></p><p><strong>Mac</strong>: <a href="https://docs.docker.com/docker-for-mac/install/">https://docs.docker.com/docker-for-mac/install/</a></p><p><strong>*Please follow the steps mentioned in the above links.</strong></p><p>Once installation is done, open your favorite command prompt and type<strong> docker –version</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/370/1*_hNidgc-p55QRRPiKf0YKA.png" /></figure><p>On the taskbar, there will be a small ship icon, right-click on it and there will be a Dashboard menu, After clicking on that it will open a dashboard and will show you the running containers if any, like the below image<strong>.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*aixboTjdysIT5ZF1XWB6Ig.png" /></figure><p><strong>Top Docker Commands:</strong></p><p><strong>Docker –version: </strong>This command is used to get the currently installed version of docker</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/477/1*Dr09sB95MKSK4XBNpRbS4w.png" /></figure><p><strong>Docker pull: </strong>This command is used to pull images from the docker hub</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*fFBsH6tTHDef2UMnDSOzMw.png" /></figure><p><strong>Docker ps: </strong>This command is used to list all the running containers:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*r2RH7Tt8xkRhoRVqLSQmvQ.png" /></figure><p>Use <strong>docker ps -a</strong>: To check how many containers are available(exited ones), up and running.</p><p><strong>Docker run: </strong>This container is used to create a container from an image</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*3NGLMnwDKrXxRdQwNJGA6w.png" /></figure><p><strong>Docker start: </strong>docker <strong>start </strong>&lt;Container-Id&gt; -&gt; To Start Container.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/633/1*QCudiccMJzxGkQDUnSqxzQ.png" /></figure><p><strong>Docker exec:</strong> This command is used to access the running container and takes control on it.</p><p>Docker exec -it&lt;container id&gt; bash</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/634/1*cBn_KOqqSiFhsRhrK0pOKA.png" /></figure><p><strong>Docker Stop: </strong>docker stop &lt;container id&gt; to stop container when running</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/630/1*8g53qvujeF504izAjWXhoQ.png" /></figure><p><strong>Docker Restart: </strong>docker restart &lt;container id&gt; to restart the container</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/630/1*4zw5Rqo7TEA-1jQNJ9D1RA.png" /></figure><p><strong>Docker RM: </strong>docker rm &lt;container id&gt;<strong> to</strong> delete docker container</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/835/1*lf7tEhkDJyOFrQ1fxzHuRw.png" /></figure><p>It shows the error so stop the container first and then remove it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/756/1*e-nefw0vkeA7kFZY6-CwIQ.png" /></figure><p><strong>Docker rmi: </strong>docker rmi &lt;image id&gt; to remove docker image</p><p><strong>Docker Kill:</strong> docker kill &lt;Container Id&gt; -&gt; To Stop Container Forcefully.</p><p><strong>Docker inspect: </strong>docker inspect&lt;container id&gt; to get all the info about the container.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/607/1*_NgXSlya4IjlHeo4eysXXA.png" /></figure><p><strong>Docker Login: </strong>This command is used to login to the docker hub repository</p><p><strong>Docker Push: </strong>This command is used to push an image to the docker hub repository</p><p><strong>Docker Build:</strong> This command is used to build an image from a specified docker file.</p><p>That’s all for this article. Hope you have got an overall picture of the docker.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9ade479f2d32" width="1" height="1" alt=""><hr><p><a href="https://medium.com/programming-patterns/docker-a-containerization-approach-9ade479f2d32">Docker — a containerization approach</a> was originally published in <a href="https://medium.com/programming-patterns">Programming Patterns</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Bitwise Hacks Part-2]]></title>
            <link>https://medium.com/programming-patterns/bitwise-hacks-part-2-6bc0fc600385?source=rss----877139141b5c---4</link>
            <guid isPermaLink="false">https://medium.com/p/6bc0fc600385</guid>
            <category><![CDATA[bitwise]]></category>
            <category><![CDATA[programming-interviews]]></category>
            <category><![CDATA[bitwise-operator]]></category>
            <category><![CDATA[programming-pattern]]></category>
            <dc:creator><![CDATA[Saurabh Singh]]></dc:creator>
            <pubDate>Sat, 27 Feb 2021 09:25:52 GMT</pubDate>
            <atom:updated>2021-02-27T09:40:29.478Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/708/1*2ZGVF1vKwxNg2vqEi2EbDA.png" /></figure><p>Part 1: <a href="https://medium.com/programming-patterns/bitwise-hacks-part-1-1f46f9ab1ec">https://medium.com/programming-patterns/bitwise-hacks-part-1-1f46f9ab1ec</a></p><p>In the previous article, we talked about basic bitwise operators, In this article, we will explore the most common problems that can be solved using bitwise operators and this will motivate you to use bitwise operators more &amp; more in your daily programming tasks.</p><p><strong>Question</strong>: Check a number is Even or Odd?</p><p><strong>Approach1</strong>: We can take mod of 2 and say that number is even if the value is 0 otherwise odd</p><p><strong>Approach2</strong>: If the rightmost bit of the number is 1 it means it is even otherwise odd.</p><p>Let’s see the code here :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/59805ba4ff32bb30ee89bc6375ab3ee5/href">https://medium.com/media/59805ba4ff32bb30ee89bc6375ab3ee5/href</a></iframe><p><strong>Question: </strong>Find the number occurring the odd number of times?</p><p><strong>Approach 1: </strong>We can run two nested loops, the First loop will check each element one by one and the inner loop will check the occurrences of the selected element in loop 1</p><p><strong>Approach 2: </strong>Can we use XOR here: We know that XOR says that the result is 0 if both numbers are the same, so in this case we can take XOR of each element and the same numbers will become 0 and the odd one will remain left.</p><p>Let’s look at the code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/43f168430767f3fbbc802973dfb0d5bc/href">https://medium.com/media/43f168430767f3fbbc802973dfb0d5bc/href</a></iframe><p><strong>Question: </strong>Find the number of bits in a number?</p><p><strong>Approach 1: </strong>We can run a loop and right shift the number by 1 and count. We know that the right shift is equivalent to dividing a number, so when we have counted the bit we will get rid of the same.</p><p><strong>Approach 2:</strong> We can take a log of the number base 2 and add 1 to it.</p><p>Let’s see the code :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8fa38f006cb6de6cd25e19ae82b2c974/href">https://medium.com/media/8fa38f006cb6de6cd25e19ae82b2c974/href</a></iframe><p><strong>Question: </strong>Find the number of set bits in a number?</p><p><strong>Approach 1</strong>: We need to count the number of 1 bit in a number, so one approach could be to convert the decimal number into binary and then count the bits.</p><p><strong>Approach 2: </strong>A better approach can be, Apply bitwise and operator with 1, and if the value is 1 then count and then get rid of that bit.</p><p>Let’s see the code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/46622b94a862fe4d07a36511c128dcf7/href">https://medium.com/media/46622b94a862fe4d07a36511c128dcf7/href</a></iframe><p>Another similar question to the above problem is:</p><p><strong>Question: </strong>Find the kth bit is 1 or not?</p><p><strong>Approach: Right Shift: </strong>I hope this will become easy now, we can right shift up to kth digit and then take and with 1 if the value is 1 then the bit is 1 otherwise not.</p><p><strong>Approach: Left Shift: </strong>Another approach is with the left shift operator, we can left shift 1 up to kth digit and then take &amp; with n if the value is 1 then the bit is 1 otherwise not.</p><p>Let’s look into the code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1d2c70e8aa21addc4bb42e40626c0924/href">https://medium.com/media/1d2c70e8aa21addc4bb42e40626c0924/href</a></iframe><p>I hope it’s making sense now. In the coming series of Bitwise Hacks, we will talk about similar programming patterns.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6bc0fc600385" width="1" height="1" alt=""><hr><p><a href="https://medium.com/programming-patterns/bitwise-hacks-part-2-6bc0fc600385">Bitwise Hacks Part-2</a> was originally published in <a href="https://medium.com/programming-patterns">Programming Patterns</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Bitwise Hacks Part-1]]></title>
            <link>https://medium.com/programming-patterns/bitwise-hacks-part-1-1f46f9ab1ec?source=rss----877139141b5c---4</link>
            <guid isPermaLink="false">https://medium.com/p/1f46f9ab1ec</guid>
            <category><![CDATA[bitwise]]></category>
            <category><![CDATA[bitwise-operator]]></category>
            <dc:creator><![CDATA[Saurabh Singh]]></dc:creator>
            <pubDate>Sat, 27 Feb 2021 08:27:48 GMT</pubDate>
            <atom:updated>2021-02-27T09:58:23.569Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/714/1*89Uju-a9CUwiFK4xjDXAYw.png" /></figure><p>As a developer, we have always used different data types like integer, float which are base 10 numbers but the computer stores these values using base 2. Now the question must come what is Base 10 or Base 2.</p><p>Base 10 numbers are 0–9 and Base 2 numbers are 0–1</p><p><strong>Binary Representation Of Number</strong></p><p>The numbers stored in our memory are stored in as 0 or 1 and each digit is called a bit and 8 bits become 1 byte. Commonly -used bit lengths for integers are 8 bit, 16 bit, 32 bit or 64</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ryMWuoh57nBXed8NHZvSow.png" /><figcaption>Basic Representation of Binary System</figcaption></figure><p><strong>Bitwise Operators :</strong></p><p>Let’s now take a look at what are the different bitwise operators available:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*prrC7cT5VZptKqUbp-M2kg.png" /><figcaption>Bitwise Operators</figcaption></figure><p>Let’s talk about these operators in details:</p><p><strong>AND (&amp;)</strong></p><p>The and-operator is used with 2 binary numbers and returns a new binary number. The output of bitwise And is 1 if both bits are 1, otherwise 0</p><p>Let’s say : x = 3 and y = 6 so x &amp; y will give 2 as output</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/499/1*0aB4wbAI9yRCkxPT3AjP9w.png" /></figure><p><strong>Or (|)</strong></p><p>The or-operator is used with 2 binary numbers and returns a new binary number. The output of bitwise or is 1 if either of the bit is 1 otherwise 0.</p><p>Let’s say : x = 3 and y = 6 so x | y will give 7 as output, Below is the 32 bit representation:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/460/1*w2lkTGN7M3_jMTLTRDhuLA.png" /></figure><p><strong>Xor (^)</strong></p><p>The Xor-operator is used with 2 binary numbers and returns a new binary number. The output of bitwise Xor is 0 if both bits are 1 otherwise 1 if either of the bit is 1.</p><p>Let’s say : x = 3 and y = 6 so x ^ y will give 5 as output.</p><p><strong>Bitwise Not (~)</strong></p><p>The Not-operator is used with 1 binary number and it’s applied to a single operand to its right. Bitwise Not operator changes each bit to its opposite.</p><p>Let’s say x=3, below is the representation :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/750/1*I_sigTq9JJDKWDLqcl_Rhg.png" /></figure><p>It’s a bit confusing now? How come a positive number becomes negative: This is because numbers are mostly represented with two’s complement and the left-most digit represents the positive or negative value. For example :</p><p>The number is 1101, Let’s say how does it represent in Base 10 &amp; Base 2:</p><p><strong>Base 10</strong>: 1: 10³ = 1000 , 1:10² = 100, 0: 10¹=0, 1:10⁰= 1 = 1000+100+0+1= 1101</p><p><strong>Base 2: </strong>1: 2³ = 8, 1:2² = 4, 0:2¹ = 0, 1: 2⁰ = 1 = 8+4+0+1 =13</p><p>If I am taking two’s compliment here then the left-most digit will be negative if the value is 1 otherwise positive:</p><p><strong>2’s Compliment: </strong>1: 2³ = -8, 1:2² = 4, 0:2¹ = 0, 1: 2⁰= 1 = -8+4+0+1 =-3</p><p>Now the million-dollar question is 1101 should always be read as : -3 or 13</p><p>Here comes the Signed and Unsigned numbers, Signed numbers are always represented in 2’s compliment and unsigned one’s are plain Base2</p><p>Now the question is how can we smartly calculate the 2’s compliment of a number :</p><p>Let’s say X= 3 , we can simply add 1 to x and add negative sign = -4</p><p>Let’s say X=-4, we can simply add 1 to x and add negative sign = 3</p><p><strong>Left Shift Operator:</strong></p><p>The left-shift operator causes the bits to shift left by the number of positions mentioned in the expression:</p><p>For example, We need to shift 3 &lt;&lt; 1</p><p>3 : 00000000………00000011 &lt;&lt; 1 = 00000000……..00000110 = 6</p><p>If I see then we are multiplying the value by 2^positions so in the above example : 3* 2¹ = 6</p><p>3 &lt;&lt; 2 =&gt; 3*2² = 12. Let’s see how it will be represented :</p><p>3 : 00000000…..00000011 =&gt; 00000000…..00001100 = 8+4 = 12</p><p>So In general we can say whenever we are doing the left shift by some position then it means we are multiplying the number by that position.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/354/1*a04qwzMmkrRVfAyoNg9XKA.png" /></figure><p><strong>Right Shift Operator:</strong></p><p>The Right-shift operator causes the bits to shift right by the number of positions mentioned in the expression:</p><p>For example, Let’s shift 3 &gt;&gt; 1</p><p>3: 00000000…..00000011 =&gt; 00000000…..00000001=&gt; 1</p><p>3 &gt;&gt;2 : 00000000…..00000011 =&gt; 00000000…..00000000=&gt; 0</p><p>Can we say here we are dividing the value by power of 2?</p><p>3/2 = 1 and 3/2² = 0</p><p>So In general we can say whenever we are doing the right shift by some position then it means we are diving the number by that position.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/336/1*3pw9hK2DPfyunNsZnJf6_A.png" /></figure><p><strong>Why is this useful?</strong></p><p>As a programmer, if you are thinking like this then you are not alone, Let me share some questions about where these operators will make sense:</p><p><strong>Problem: Find if the number is a power of 2 or not?</strong></p><p><strong>Approach1</strong>: We can take mod with 2 and find out the answer.</p><p><strong>Approach2</strong> : We can take n &amp; (n-1) which is in this case : 2 &amp; 1 and return true if the value is 0.</p><p>2 can be represented as 00000000….00000010</p><p>1 can be represented as 00000000….00000001</p><p>The result is 0 then it is the power of 2.</p><p><strong>Let’s find out 3 is a power of 2 or not?</strong></p><p>3: 00000011 and 2: 00000010 the result will be 2 so this is not the power 2.</p><p>Let&#39;s see the code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9ee3242b08022fdc2390e0df08edb4ea/href">https://medium.com/media/9ee3242b08022fdc2390e0df08edb4ea/href</a></iframe><p>Bitwise operations are always efficient to perform. Hope you like the article. In the next series of this article, we will talk about the programming problems which can be solved using Bitwise operations.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1f46f9ab1ec" width="1" height="1" alt=""><hr><p><a href="https://medium.com/programming-patterns/bitwise-hacks-part-1-1f46f9ab1ec">Bitwise Hacks Part-1</a> was originally published in <a href="https://medium.com/programming-patterns">Programming Patterns</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>