Posts

Showing posts from 2020

Count the Prime Numbers

Question: Count the number of prime numbers less than a non-negative number,   n . Input : 10  Output: 4  Explanation: There are 4 prime numbers less than 10 - 2, 3, 5, 7  Please refer leetcode for more info:  https://leetcode.com/problems/count-primes/

Algorithm : Longest Substring Without Repeating Characters

    Algorithm : Longest Substring Without Repeating Characters Problem Statement: Given a string, find the length of the  longest substring  without repeating characters.  This question can be found in leetcode #3.      Example: if the input string is "sreenath", then the output should be: 5      Reason\Explanation:  sre - First three characters, until the next e.  - 3 re. -   we move the next substring which is re until  - 2 since no words can be formed using starting with ee. -0  enath   -  with no repeating characters. - 5      As you can see that the maximum number of characters is 5.       Logic:     Sliding Window Technique:          We have a window just like how you have a Window at home, if you want to open the window fully,       you basically slide the window to the fullest. If you have t...

Python- Binary Search(Recursive and Iterative)

          Binary Search(Recursive and Iterative) in Python I am recently venturing into Python Language. Learning and comparing them with C# Skills I already possess.  Here's my first code in Python where I will discuss about Binary Search using Iterative and Recursive Approach.  As we know, Binary Search is one of the algorithm where user can search for an item in  O(1) - Best Case  O(log n)- Average Case  Binary Search is based on Divide and Conquer Strategy. Basic Concept of Binary Search. :       1)  You have a low index      2)  You have a high index.      3)  We calculate the mid value(initially: mid=low+high/2) and then depending on the whether the key that we are searching for is lesser than low or greater than high, we change the low and high values accordingly.      4) If the Key Value is less than Mid, we set high=mid-1;     5) If the key...

Binary Search in C#

          Binary Search in C# When you want to search for an element in an array, Binary Search comes into major play.  Most common logic for Binary Search is : 1) Sort the elements in an array 2) Using Loop or Recursion, split the array in half  3) Check for the element in the split array(either left or right).  PseudoCode:  Left =0 - Set Left index to begining of the array.  Right= Length of the array.  loop through the elements until left <= right  calculate mid - mid = left+right/2; if element we are searching is mid element, we  found the element and we exit.  if element we are searching is less than the mid element, we set the right Index to mid-1  if element we are searching is greater than the mid element, we set the start index to mid+1 public static int binary_search(int[] arr, int item) { int low = 0; int high = arr.Length; while (low...

SortedDictionary in C#

Sorted Dictionary in C# SortedDictionary in C# is useful in C# if you want to sort the elements based on the key.  Some important points to consider: Keys are always sorted.  SortedDictionary is a binary search tree with O(log n) retrieval time. It is used when you need faster insertion and removal.  Keys are always sorted in Ascending order. Some of the functionalities are common among Dictionary and Sorted Dictionary: Add() - To add key-value pair.  Example: Add(100, "Test"); Clear() - Removes all the keys from the SortedDictionary. Remove(TKey) - To remove the specified key from the SortedDictionary. ContainsKey(TKey) - To check if there is an element in the SortedDictionary with the specified value. ContainsValue(TValue)- To check if there is an element in the dictionary with the specified value. Disadvantages :  SortedDictionary has a slower retrieval time ( O(log n)) compared to Dictionary which has O(1)- Constan...

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....

COALESCE in SQL Server

COALESCE in SQL Server COALESCE -  Returns the first non-null value in a list. SELECT COALESCE(NULL, 1, 2 , NULL); Returns 1 as the answer. COALESCE(val1, val2, ...., val_n) If all values are NULL, COALESCE returns NULL.         

Delegates in C#

A delegate is a type-safe function pointer that can reference a method that has the same signature as that of a delegate. In short: Rather than pointing to the actual function, you point to a delegate. Delegates : Used to define callback methods  Implement event handling Declared using "delegate" keyword.  It allows us to pass methods as parameter.  Types of Delegates:  Simple/Single Delegate - When delegate takes reference with single method.  Multicast Delegate - When delegates takes reference with multiple methods. You can make use + and\or (-) sign to add and subtract methods respectively. This creates an invocation list and is called upon the order of addition.  Generic Delegate  Different ways of creating a delegate in C#:  Action - Action takes up to 16 input Parameters and doesn't return a value. Example: delegate int Action (T1 arg1, T2 arg2);  Func Predicate Lambda Anonymous Types. Fun Fact...