Showing posts with label Interview Companies. Show all posts
Showing posts with label Interview Companies. Show all posts

Monday, October 30, 2017

Maximum and Minimum values of a shared resource if shared within 5 threads

Since int is a primitive and since we want a sharable resource , so it must be a reference which needs to be shared within 5 threads.

If Integer being used, since Integer is Immutable hence any change on Integer reference will create a new Object , so it must be avoided.

Its desirable to create own NonMutableInteger reference and share the reference to multiple thread using Threadpool

/* To check the minimum and maximum value of count
 * Minimum - Nothing to be Guaranteed(Race condition can happen)
 * Maximum  - 25*/

public class ThreadMinMaxCount {
     public static void main(String[] args) {
         
          /* Integer is an Immutable class and hence not used */
          NonMutableInteger count = new NonMutableInteger(0);
          ExecutorService service = Executors.newFixedThreadPool(5);
          for (int i = 0; i < 5; i++)
              service.execute(new Task(count));
         
          System.out.println(count.i);
          service.shutdown();
     }
}

class Task implements Runnable {

     NonMutableInteger count;

     public Task(NonMutableInteger count) {
          this.count = count;
     }

     @Override
     public void run() {
          for (int i = 0; i < 5; i++)
              count.increment();
     }
}

class NonMutableInteger {
     int i;

     public NonMutableInteger(int i) {
          this.i = i;
     }

     public int getMutableInteger() {
          return i;
     }

     public void increment() {
          i++;
     }
}

Output (Run multiple times)
10
5
20

Saturday, September 16, 2017

Nagarro Interview Pattern


Round 1) Psychometric test                                               Elimination round
12  minutes, 50 Questions – No negative marking

Round 2) Coding – Hacker Rank                                      Elimination round
3 questions, 18 Test cases – Have to pass minimum 12-13 TC to qualify to next round

Question 1
Reverse first N numbers in an array – Total 8 test cases
array -[1,2,3,4,5,6,7,8]
reverseNumbers(array,4) - > [4,3,2,1,5,6,7,8]
int[] reverseNumbers(int array, int N){
}

Question 2
Sort date arrays – Total 5 test cases

Question 3

All possible sub-strings of a String - Total 5 test cases
String str=”abc”

Output- [a,ab,abc,b,bc,c]
String str=”cat”

Output- [a,at,c,ca,cat,t] <- Do note that array shows order in alphabetical order and thus use of TreeSet is required.

String str=”nnn”

Output- [n,nn,nnn] <- No duplicate elements again in output array

Round 3) Technical round (1:15 Hr)                        Elimination round
A lot has been asked from current project and emphasis was on java frameworks, 1 MVC framework and 1 ORM framework.
     a)    Convert Set<String> to String[]
     b)    Hibernate – n+1 issue
     c)     Hibernate vs normal SQL
    d)    Struts 2 flow and role of Spring DI in Struts 2 framework
    e)     Does Struts 2 action class provide Singleton object or not?
    f)      Flow of strut2 framework, starting from writing a form and press submit.
    g)    Transaction module in Hibernate
    h)    hibernate.cfg.xml and Type of primary key auto generation in Hibernate
    i)      Lazy and Early fetch in Hibernate

Round 4) Technical round (Architect)                     Elimination round
     a)    Tree Map working(AVL and Red-Black tree)
     b)    Spring AOP backend working
     c)     Proxy design pattern and real time usage
     d)   Transaction attributes and Transaction propagation.
     e)     Serialization, why suid is static?
     f)      JDK Dynamic proxy and CGLIB proxy library

     g)    Aka and Actor framework

Tuesday, July 18, 2017

Generate unique random numbers in java without using in build function

Pseudo code to generate unique random number :

a) Add entries in an array of size n.
b) Put an maxIndex on last value of array.
c) Find random index from 0 to maxIndex.
d) Swap array[random] and array[maxIndex]
e) maxIndex--

f) This way we get random number and we're putting random number in last of the array and subtracting size of array.

package com.corejava;

/* @Description- Class Generating unique random Generator
 * @author- Paras
 * */

public class RandomUniqueGenerator {
     static int maxI;
     static int uniqueArray[]= new int[11];
     public static void main(String[] args) {
          /* Create an array of 0 to 10 number*/
          for(int i=0;i<=10;i++)
              uniqueArray[i]=i;
         
          /*maxI denotes last index*/
          maxI=uniqueArray[uniqueArray.length-1];
         
          while(maxI>=0){
              /* pick any random index */
              int randomIndex = (int)(Math.random()*maxI);
              System.out.println("Index comes out to be "+ randomIndex);
              System.out.println("Random number found to be "+ uniqueArray[randomIndex]);
              swap(randomIndex);
     };
     }
     static void swap(int index){
          /*swap array[r] and array[maxI]*/
          int temp=uniqueArray[maxI];
          uniqueArray[maxI]=uniqueArray[index];
          uniqueArray[index]=temp;
          maxI--;
     }
}

Output

Index comes out to be 5
Random number found to be 5
Index comes out to be 7
Random number found to be 7
Index comes out to be 6
Random number found to be 6
Index comes out to be 2
Random number found to be 2
Index comes out to be 5
Random number found to be 10
Index comes out to be 0
Random number found to be 0
Index comes out to be 2
Random number found to be 9
Index comes out to be 1
Random number found to be 1
Index comes out to be 1
Random number found to be 3
Index comes out to be 0
Random number found to be 8
Index comes out to be 0
Random number found to be 4


Censhare Interview Pattern


Round 1 – Written test

1) Write a program to design a factory pattern class
           
           2)     Write a program to design Singleton pattern class.
       
           3)     Print a String in reverse order without using in build function
           Input “Hello how are you” 
           Output “you are how Hello”
    
          4)     How to generate unique random numbers in java without using in build function
      
          5)     Write a program to Sort Map by Value
      
          6)     Write a program to get unique values from employee list on the basis of roll         number and sort using name, roll no.
      
           7)     Given an unsorted array of integers, find the length of longest consecutive    elements sequence.

         Input [100,4,200,1,3,2] 
         Output [1,2,3,4]

Round 2 – Technical Interview

1)     HTTP request and response

2)     HTTP response header

3)     Difference between REST WS and Servlets

4)     Difference between Filter and Servlet

5)     What are Interceptors  and two types of interceptors – Pre Interceptor and Post Interceptor

6)     Association in hibernate -1 to 1 ,1 to many and many to one