Posts

Knowing too much is bad(in the world of OOP)

In real life, we have heard experts say that knowing too much or sometimes doing too much is bad.  This is the same in the world of Object Oriented Programming, this is called as God Object. The God object is an example of anti-pattern(code that is difficult to understand, refactor and maintain) and a code smell.  We have heard of Single-responsibility Principle(which is "S" in the SOLID principles). The SRP states that each class should have only responsibility and therefore has only one reason to change.  Whenever a code is written, make sure that the class does only one thing. You can refer to the God Object here at wikipedia which gives a brief description: https://en.wikipedia.org/wiki/God_object

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