Posts

Singleton Pattern Examples and Implementation

Implementation of Singleton Design pattern   To implement a singleton pattern, we have different approaches, but all of them have the following common concepts.   --Private constructor to restrict instantiation of the class from other classes.   --Private static variable of the same class that is the only instance of the class.   --Public static method that returns the instance of the class, this is the global access point for the outer world to get the instance of the singleton class.   Let’s put them in Action.     Approach-1    Eager Initialization   In eager initialization, the instance of the singleton class is created at the time of class loading. The drawback to eager initialization is that the method is created even though the client application might not be using it. Here is the implementation of the static initialization singleton class   public class EagerInitializedSingleton {   private static final EagerInitializedSingleton instance = new EagerInitializedSingleton();     /

Microservices

Let’s Assume that we have a User and a Department so we can make two services that is User service and Department service. So, the key is how to manage two different service (user and department), There is a Service Registry that is the third service where the above two services will be registered and making API gateway where all requests will be received first and will be forwarded to respected services (will work as a mediator) Currently the Services count we have been is 1) User Service 2) Department Service 3) Service Registry 4) API Gateway Now the Service 1 and 2 and 4 will be the service which will be registered under Service R egistry , A service Registry is a service which keeps track of all the service which are registered to it. So, for service 1 and 2 that is the user and department service will communicate as independent service, just think you have User service and many of your other project has the usage of user, in fact without user nothing is possib

Data Structures and Algos problem-5

Question (Asked in Google) Find the max from the contiguous  sub arrays if the integer array is [-2,2,5,-11,6] . answer : 7 Answer Lets understand first what the question means? the Contiguous sub array means we can take the sub array as lets say [-2,2] or say [2,5] means all the possible array out of the given array So lets say  [-2,2] i.e -2+2=0 [2,5] i.e 2+5 =7    this is the answer [5-11] i.e 5+-11= -6 [-11,6] i.e -5 like wise we can take [-2,2,5] i.e 5 but the number must be contiguous i.e we cant take [2,6,5] that's not allowed So lets move on to the solution int [] array= {-2,2,5,-11,6};       int max_sum=array[0];    //i.e -2       int current_sum=max_sum;  //i.e -2       for(int i=1;i<array.length;i++) { // we count from index 1 that is from 2           current_sum  = Math.max(array[i]+current_sum, array[i]);  //i.e 2 + -2=0,2 (we start looping from 1)           max_sum=Math.max(current_sum, max_sum); //i.e (0,2) so obviosuly 2        }       System.out.println(max_sum)

Data Structures and Algos problem -4

Question Given the set of Arrays (Asked in Amazon) int [] a= {0,0,-5,30212}; int [] b= {-10,40,-3,9}; we need the sum of elements from the arrays to be -8 the time complexity must less than O(n*2) Answer Here given that we have two arrays and we need to find two elements from the array that is one from each in a way that sum comes out to be -8 so here we need to solve the problem in the one go boom not taking any extra loops or anything as we need to solve them in an optimize way but can take some more extra space complexity let do it We would minus the each element from the array a  with -8 and prepare a list with the compliments and  compare the item of the compliment with the array bas compliment array is -8-0,-8-0,-8-(-5),-8-(30212) So the compliment array would be (-8,-8,-3,-30220) And array b is {-10,40,-3,9}; so yeah -3 of the compliment array is present in the b is the answer So its solved orally lets have the code below    int [] a= {0,0,-5,30212};         int [] b= {-10,40,-3

Algos and Data Structures problem -3

Question F ind the second highest and the third highest number from int[] numbers = { 2, 3, 4,5,6,7,8} Answer Now the given question says that we not need to find the largest or the highest number out of the given array but the second highest and the third highest with O(n). Actually O(n) not given in the question but we need to solve in an optimal way  So the solution of it is easy just we need to take some extra variables and need to update it. Here code goes int[] numbers = { 2, 3, 4,5,6,7,8}; // you need to get the second and the third highest number of it         Integer max_num = null;         Integer secondMax = null;         Integer thirdMax = null;         for (Integer num : numbers) {             if (num.equals(max_num) || num.equals(secondMax) || num.equals(thirdMax)) {                 continue;             }// this condition is for checking the repetitive numbers must not be processed             if (max_num == null || num > max_num) {                 thirdMax=secondMax;

Algos && Data structures problem -2

Question (Leet code) Sort the array by parity int[] nums={2,3,4,5,6};  to {2,4,6,3,5} //might be sorted or not sorted by O(n) time complexity and No extra space complexity. Answer So basically we are given the Array which can be sorted not we don't know we need to arrange the elements as from even to odd like stated in question so how can we do that? Basically the first thing that we can do it the brute Force method like check by the entire array taking elements one by one and checking the odd and the evens and can take some extra array to add first the even ones and after that the odd ones but here we are said that we need an optimal soln by no extra time and space so lets do that int[] nums={2,3,4,5,6}; //{2,4,6,3,5} //might be sorted or not sorted         int i=0;                 for (int j=0;j<nums.length;j++) {            if(nums[j]%2==0) {// if this matches so the num i even                int temp=nums[i];                nums[i]=nums[j];                nums[j]=temp;      

Data Structures && Algo Problems-1

Question-1(asked in Facebook) Given a sorted array find the square sorted array out of it. Input elements int [] array={-6,-5,-4,1,2,3,4} Output  {1 ,4 ,9 ,16 ,25 ,25 ,36 } Answer Now we are stated that we will be given a sorted array as input. Now simply squaring that will change the elements value and that array wont be sorted anymore. So squaring the element gives you 36,25,16,1,4,9,64 so that wont be sorted anymore Now the key is how to perform this kind of problems with optimal solution We will try to solve this problem by O(n) time complexity We will use some extra space complexity. So know the key thing is each one of us knows that we will simply first square the elements  and will apply the sort taking the sort function by Arrays.sort(array) but lets make this problem optimal in a single go..!! So firstly will traverse the array taking the left pointer and the right pointer like {-6,-5,-4,1,2,3,4}           ^                    ^          left