<?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: Balamurugan</title>
    <description>The latest articles on Forem by Balamurugan (@balasr21).</description>
    <link>https://forem.com/balasr21</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%2F309767%2F3bd955f8-d14b-4c05-bf38-d4d5e311133f.jpeg</url>
      <title>Forem: Balamurugan</title>
      <link>https://forem.com/balasr21</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/balasr21"/>
    <language>en</language>
    <item>
      <title>Conway's Game Of Life - Coding Challenge - React &amp; SpringBoot
</title>
      <dc:creator>Balamurugan</dc:creator>
      <pubDate>Sun, 21 Mar 2021 09:32:41 +0000</pubDate>
      <link>https://forem.com/balasr21/conway-s-game-of-life-coding-challenge-react-springboot-1gcd</link>
      <guid>https://forem.com/balasr21/conway-s-game-of-life-coding-challenge-react-springboot-1gcd</guid>
      <description>&lt;p&gt;The Game of Life is not a game in the conventional sense. There are no players, and no winning or losing but its a good coding challenge to solve :)&lt;/p&gt;

&lt;p&gt;The universe of the Game of Life is an infinite two­dimensional orthogonal grid of square cells, each of which is in one of two possible states, alive or dead. Every cell interacts with its eight neighbours, which are the cells that are horizontally, vertically, or diagonally adjacent.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Rules:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;At each step in time, the following transitions occur:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Any live cell with fewer than two live neighbours dies as if caused by under­population.&lt;/li&gt;
&lt;li&gt;Any live cell with two or three live neighbours lives on to the next generation.&lt;/li&gt;
&lt;li&gt;Any live cell with more than three live neighbours dies, as if by overcrowding.&lt;/li&gt;
&lt;li&gt;Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
The Game of Life can be simulated using multiple patterns, I have set the default pattern as Glider Pattern (more info can be found here &lt;a href="https://en.wikipedia.org/wiki/Glider_(Conway%27s_Life)"&gt;https://en.wikipedia.org/wiki/Glider_(Conway%27s_Life)&lt;/a&gt;) but it can be modified to any pattern before starting the game.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I have tried to solve this challenge with the backend using Spring Boot and the front end using React&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4MXtsbxC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2a7088t1rrawref9vbc2.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4MXtsbxC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2a7088t1rrawref9vbc2.jpeg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As seen in the above diagram, the user needs to input data to start the game&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;User can modify the dimension of the matrix ( by default 20*20 )&lt;/li&gt;
&lt;li&gt;User can modify the initial generation of live cells (by default glider pattern will be created at the top of the matrix)&lt;/li&gt;
&lt;li&gt;Click START&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Based on the input data selected, the front end makes a request to the backend to retrieve the next generation of live cells which is populated in the UI.&lt;/p&gt;

&lt;p&gt;If the validations are successful (matrix dimension is valid), in order to generate the next set of generations below steps are performed.&lt;/p&gt;

&lt;p&gt;Consider eg an 8 * 5 dimension with initial live cells as [10, 19, 25, 26, 27]&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For each of the initial generation, identify its nearest unique neighbours considering border conditions. In the above diagram example, [1, 3, 6, 7, 8, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33]&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It's best to use a HashSet in this case as this data further will be used for searching and searching in a HashSet will result in O(1)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For each of the identified cells, check whether they are going to be alive in the next generation by applying rules by identifying the neighbour for each cell and count the number of cells which are ALIVE in the current generation (by comparing against input).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For eg, Cell 1 (current dead cell) -&amp;gt; neighbours are 2(L), 9, 10 and only one cell is currently live and hence as per rules, it won't survive the next generation.&lt;/p&gt;

&lt;p&gt;Also for cell 10(current dead cell), -&amp;gt; neighbours are 1, 2(L), 3, 9, 11(L), 17, 18, 19(L) and exactly 3 cells are currently live which will make 10 to be alive in next generation&lt;br&gt;
Also for cell 19( current live cell), -&amp;gt; neighbours are 10, 11(L), 12, 18, 20, 26(L), 27(L), 28. We will have 3 cells that are currently live and as per rules, this can survive in the next generation.&lt;/p&gt;

&lt;p&gt;Similarly for each cell, their next-generation status is identified.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Collect the cells which are alive in the next generation and return the result set&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This solution is available in &lt;a href="https://gameofapi.web.app/"&gt;Game of Life UI&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Regardless of the size of the matrix, this solution would take constant time(considering a finite number of alive cells) because the size of the matrix is not used in any calculation apart from identifying the neighbour cells which will be constant. If you have solved this problem in a more efficient approach, Please share it here I would like to know.&lt;/p&gt;

&lt;h2&gt;
  
  
  Repository :
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/balasr21/gameoflife-ui"&gt;UI&lt;/a&gt;&lt;br&gt;
&lt;a href="https://github.com/balasr21/GameOfLife"&gt;API&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.gameoflife.balasr.com/swagger-ui/"&gt;Swagger for API&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>react</category>
      <category>interview</category>
      <category>springboot</category>
    </item>
    <item>
      <title>Understanding equals and hashCode in Java </title>
      <dc:creator>Balamurugan</dc:creator>
      <pubDate>Sun, 31 Jan 2021 23:39:39 +0000</pubDate>
      <link>https://forem.com/balasr21/understanding-equals-and-hashcode-in-java-33b8</link>
      <guid>https://forem.com/balasr21/understanding-equals-and-hashcode-in-java-33b8</guid>
      <description>&lt;p&gt;&lt;em&gt;This post was originally published here at &lt;a href="https://balasr.com/blog/2n857rotH9da7mBF5ZWO2V" rel="noopener noreferrer"&gt;https://balasr.com/blog/2n857rotH9da7mBF5ZWO2V&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This is going to be one of the series of posts related to Interview topics that I would be writing. In this post, we are going to explore the importance of equals and hashCode methods in Java.&lt;/p&gt;

&lt;p&gt;equals() and hashCode() are two methods that are part of every class in Java. Object class is the superclass for all other classes in Java. equals and hashCode are the methods that are part of the Object class&lt;/p&gt;

&lt;p&gt;As a general rule,&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;If two objects are equal according to the implementation, their hashCode should match!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let us approach understanding this with a scenario, Have you ever noticed what happens when an identical order is placed in Dominos app within a stipulated time by mistake?&lt;/p&gt;

&lt;p&gt;Any duplicate orders will be rejected as they suspect it could be due to a mistake.&lt;/p&gt;

&lt;p&gt;It's recommended that both equals and hashCode methods need to be overridden to achieve better business decisions. So why its best to override these implementations? Before that, we need to understand what does the base implementation does.&lt;/p&gt;

&lt;center&gt;**equals() base method**&lt;/center&gt;




&lt;p&gt;As Java is an Object-oriented programming language, except for primitives everything else is an object in Java.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public boolean equals(Object obj) {
        return (this == obj);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The base implementation compares and identifies whether the current object and the passed object are one and the same i.e., they share the same memory location as in the below diagram. Both the objects have the same reference in the heap memory&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fe5r3ggwm5kdhi8d8tyjq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fe5r3ggwm5kdhi8d8tyjq.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;center&gt;**hashCode() base method**&lt;/center&gt;




&lt;p&gt;This method returns the hash value of the given object as an integer value. Within the application lifetime, it's expected that the hashCode value of an object remains the same regardless of the however number of times it got invoked.&lt;/p&gt;

&lt;p&gt;Hence if 2 objects are considered equal according to the default equals method then their hashCode value will always be the same.&lt;/p&gt;

&lt;center&gt;**Overridding equals() method**&lt;/center&gt;




&lt;p&gt;Coming back to our scenario, according to our requirement, any 2 orders are equals if their attributes (ordering user, order items, quantity, and delivery address) are the same. So let's override the equals method to reflect the same&lt;br&gt;
equals method alone overridden as below&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F0muw1qo6u3rmp0t9bdem.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F0muw1qo6u3rmp0t9bdem.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Result when the equal method is overridden&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F00phn0fgo0kue5nrpvqh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F00phn0fgo0kue5nrpvqh.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;center&gt;**Impact if hashCode() method is not overriden**&lt;/center&gt;




&lt;p&gt;The impact would be visible if the orders are stored in hash-based collections. For instance, consider the below example where orders are maintained in HashSet and we try to perform whether the newly received order is already present with us. This would be a problem as we have not overridden the hashCode method yet&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F2941jviacdhgku52wxq7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F2941jviacdhgku52wxq7.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see in the above image, 2 orders even as per our requirements are equal, it's being considered as not equal.&lt;/p&gt;

&lt;center&gt;**Overriding hashCode() method and equal()**&lt;/center&gt;




&lt;p&gt;Let's override the hashCode method to include a combination of our attributes (this is one of the ways) for comparison&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fpr643zr69p0ezp9lhhnj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fpr643zr69p0ezp9lhhnj.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now let us compare orders with both equal and hashCode overridden as per our requirement&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ftllgs7zb4q5sutovfd4h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ftllgs7zb4q5sutovfd4h.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As we can see, overriding equals and hashCode method results in exactly what we are looking for in case of any type of collections the orders are being stored&lt;/p&gt;

&lt;p&gt;So always respect the contract, it's binary either both methods (equals and hashcode) should be overridden or none of both methods.&lt;/p&gt;

&lt;p&gt;This will be one among the series of posts that I would be writing which are essential for interviews. So if you are interested in this thread and found it useful, please follow me at &lt;a href="https://balasr.com" rel="noopener noreferrer"&gt;my website&lt;/a&gt; or &lt;a href="https://dev.to/balasr21"&gt;dev&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>interview</category>
      <category>tutorial</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Understanding Hashmap</title>
      <dc:creator>Balamurugan</dc:creator>
      <pubDate>Sun, 03 Jan 2021 22:29:03 +0000</pubDate>
      <link>https://forem.com/balasr21/understanding-hashmap-519o</link>
      <guid>https://forem.com/balasr21/understanding-hashmap-519o</guid>
      <description>&lt;p&gt;This is going to be one of the series of posts related to Interview topics that I would be writing. In this post, we are going to explore hashmap. I couldn't remember any interview without Hashmap being involved.&lt;/p&gt;

&lt;p&gt;So what is Hashmap?&lt;/p&gt;

&lt;p&gt;In Simple terms, Hashmap stores data in key-value format.&lt;/p&gt;

&lt;p&gt;Let's take an example of key-value pairs&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;myCloset={
"RoundNeckTShirts":5,
"VNeckTShirts":3,
"NightPyjamas":5,
"Trousers"5
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each and every value inside the object will be stored as a key-value in the form of an array, but how to identify which pairs are stored in which array index?&lt;/p&gt;

&lt;p&gt;That is where Hash functions come into the picture&lt;/p&gt;

&lt;p&gt;Hash Functions uses the key to calculates the index in which the key-value pair needs to be stored. Various programming language uses a different hash function to calculate the array index. Let's take the below example for instance&lt;/p&gt;

&lt;p&gt;Imagine you have a chest of drawers and you want to arrange your kid's clothes in it category wise(as shown in cover pic), For eg, if its a Nightdress, it should go into the Pajamas drawer, if its a V Neck Top, it should go into T-Shirts drawer, etc&lt;/p&gt;

&lt;p&gt;Similarly, the hash function helps identify the array index (drawer in this example) where the key-value pair(clothes in this instance) needs to be stored.&lt;/p&gt;

&lt;p&gt;Hence regardless of the number of inputs to be stored, big O notation for insert will always be o(1) because the amount of work and the time required to perform the operation(Identify hash -&amp;gt; Identify index position -&amp;gt; Store the value ) doesn't change based on the volume of data&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hash Collision&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Obviously, you will have more than one set of clothes under one category(in this example) like below which is quite possible but it should be avoided. This is termed as Hash Collision&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1BC4ZFHO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/plb5wqwoqkbj8xz88la3.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1BC4ZFHO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/plb5wqwoqkbj8xz88la3.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;During data insertion, If more than one data returns the same value from a hash function then hash collision happens and the data is stored in the same bucket with reference to one another&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--oPWOQCjU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/8n8vz8dt1s234m35nymb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--oPWOQCjU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/8n8vz8dt1s234m35nymb.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;During data retrieval, if there are multiple nodes present in each bucket, then each of them is compared against the target value to retrieve the data from the bucket. Hence the best case(if there are no collisions) Big O would be o(1) and worst-case ( if there are multiple collisions and more data are stored in the same bucket) then each item will be compared against the target from the top to bottom until an element is found which might lead to o(n) - depending on the number of elements&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Load Factor&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The Initial capacity of buckets in this example is 5(number of chest drawers), Load factor is the measurement of how much the buckets can be filled before additional capacity is increased. This factor differs for different programming languages. In Java, it is 0.75f with an initial capacity of 16 buckets hence 0.75*16= 12. After storing 12 keys, an additional 16 buckets will be added to the capacity.&lt;/p&gt;

&lt;p&gt;So overall HashMap/Table is one of the most efficient data structures, it becomes worse if there are more collisions. Below Big O cheatsheet can be found here&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KDlJkV01--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/c6b3hjatudc0b3aadvf8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KDlJkV01--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/c6b3hjatudc0b3aadvf8.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here is a &lt;a href="https://www.bigocheatsheet.com/"&gt;link&lt;/a&gt; where you can visualize the manipulation in the hashmap&lt;/p&gt;

&lt;p&gt;This will be one among the series of posts that I would be writing which are essential for interviews. So if you find are interested in this thread and found it useful, please follow me at &lt;a href="https://balasr.com"&gt;my website&lt;/a&gt; or &lt;a href="https://dev.to/balasr21"&gt;dev&lt;/a&gt;&lt;/p&gt;

</description>
      <category>computerscience</category>
      <category>interview</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
