Posts

Showing posts from April, 2020

Reverse a LinkedList Algorithm

Reversing a Linked List Problem Statement:  Reverse a Linked List.  Given a Linked List of Nodes  :  1->2->3->4->5->6 , reverse it so that the output is 6->5->4->3->2->1 My Approach:  For this approach, we cannot the same LinkedListNode since it would point to the same memory location. In the above example, 1 should point to null of the new List. 2 which is 1.next (original) should point to 1 of the new List. 3 which is 2.next (original) should point to 2 of the new List. 4 which is 3.next (original) should point to 3 of the new List. 5 which is 4.next (original) should point to 4 of the new List. 6 which is 5.next (original) should point to 5 of the new List. This is where our logic ends. 1) LinkedListNode Class  This class has 2 Components of a Singly Linked List.  Value of the node Pointer to the Next Node.           public class LinkedListNode ...

Two Sum Problem

Two Sum Problem  A bit of diversion today. I am going to solve the Two-Sum problem.  Question:(You can find it in leetcode). The problem statement is shown below: Given an array of integers, return  indices  of the two numbers such that they add up to a specific target. You may assume that each input would have  exactly  one solution, and you may not use the  same  element twice. Example: Given nums = [2, 7, 11, 15], target = 9, Because nums[ 0 ] + nums[ 1 ] = 2 + 7 = 9, return [ 0 , 1 ]. My approach: We need some way to store  the indexes of the values which sums up to target.  Track the sum of 2 numbers and check if the sum is equal to target. Brute-Force Approach: Loop through the array to check if the sum of 2 numbers is equal to target.  public static int[] CalculateTwoSum(int[] arr, int d) { for (int i = 0; i < arr.Length; i++) ...

Singleton Pattern - C#

                              Singleton Pattern in C# What:  Singleton pattern is part of Gang Of Four Creational Pattern. Ensures that a class has only one instance.  Must have global access point to create the instance.  Singleton should control across access to shared resource.  Where: Singleton pattern is used for Logging, Driver Objects, Caching, Thread Pool to name a few. Database Connection. Printer Spooling. How:   Singleton pattern does not allow any parameters to be specified when creating the instance.  There are different ways to implement Singleton Pattern in C#  Simple Thread-Safe Lazy Loading High Performance  Though there are different ways to implement Singleton Pattern, they have the same characteristics: Single Constructor- Parameterless Single Constructor - Private  Sealed class  Static Variable re...

Chain Of Responsibility Pattern in C#

                                                                 Chain Of Responsibility Pattern in C# Definition:                     Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it. Details:                 Client: Initiates a request to a ConcreteHandler object on the chain.                  Handler: defines an interface for handling the requests.                   ConcreteHandler: handles requests its responsible for.  can access its successor....