Tip 1 : Thread concept is must for Morgan Stanley. So prepare that on priority
Tip 2 : Basic concepts of Oops/collection framework should be clear.
Tip 3 : Array based program will be asked so have some hands-on.
Tip 1 : Have at-least 2 good projects explained in short with all important points covered.
Tip 2 : Every skill must be mentioned.
Tip 3 : Focus on skills, projects and experiences more.
Technical Interview round with questions on DSA and Java.
Let’s assume ‘N’ is 5. Initially, all the elements are initialized to zero. we need to perform 2 operations 1 5 and 2 4. In operation 1 5, we will increase all the elements from index 1 to 5 by 1 i.e it becomes [1,1,1,1,1].
In operation 2 4, we will increase all the elements from index 2 to 4 by 1 i.e it becomes [1,2,2,2,1]. So answer in this case will be 2 as 2 is the maximum element of the array/list.
In the above question array/list is assumed to have ‘1’ - based indexing i.e. array/list starts from index 1.
The brute force approach is to do a linear search. Traverse the array and keep track of maximum and element. And finally return the maximum element.
Time Complexity : O(n)
The efficient approach is to use Binary Search. The standard Binary search algorithm can be modified for the given type of arrays :
i) If the mid element is greater than both of its adjacent elements, then mid is the maximum.
ii) If mid element is greater than its next element and smaller than the previous element then maximum lies on left side of mid. Example array: {3, 50, 10, 9, 7, 6}
iii) If mid element is smaller than its next element and greater than the previous element then maximum lies on right side of mid. Example array: {2, 4, 6, 8, 10, 3, 1}
Algorithm :
maxInBitonic(arr[], l, r)
{
while (l is less than equal to r), do :
declare m = l + (r - l) / 2;
/* If there are two elements and first is greater then the first element is maximum */
if ((r == l + 1) and arr[l] >= arr[r])
return arr[l]
/* If there are two elements and second is greater then the second element is maximum */
if ((r == l + 1) and arr[l] < arr[r])
return arr[r]
/* If we reach a point where arr[mid] is greater than both of its adjacent elements arr[mid-1] and arr[mid+1], then arr[mid] is the maximum element*/
if (arr[m] > arr[m + 1] and arr[m] > arr[m - 1])
return arr[m]
// move to left with l and r=m-1
if (arr[m] > arr[m + 1] and arr[m] < arr[m - 1])
update r to m - 1
else
update l to m + 1 // move to right with l=m+1 and r
}
// if we reach here, then element was not present
return -1
}
Internal working of hash map in Java
Index Calculation in Hashmap :
Hash code of key may be large enough to create an array. hash code generated may be in the range of integer and if we create arrays for such a range, then it will easily cause outOfMemoryException. So we generate index to minimize the size of array. Basically following operation is performed to calculate index :
index = hashCode(key) & (n-1).
where n is number of buckets or the size of array.
hashCode() method
hashCode() method is used to get the hash Code of an object. hashCode() method of object class returns the memory reference of object in integer form. Definition of hashCode() method is public native hashCode(). It indicates the implementation of hashCode() is native because there is not any direct method in java to fetch the reference of object. It is possible to provide your own implementation of hashCode().
In HashMap, hashCode() is used to calculate the bucket and therefore calculate the index.
equals() method
equals method is used to check that 2 objects are equal or not. This method is provided by Object class. You can override this in your class to provide your own implementation.
HashMap uses equals() to compare the key whether they are equal or not. If equals() method return true, they are equal otherwise not equal.
Why do we need to override equals and hashcode methods in Java?
Two objects are considered equal only if their references point to the same object, and unless we override equals and hashCode methods, the class object will not behave properly on hash-based collections like HashMap, HashSet, and Hashtable. This is because hash-based collections are organized like a sequence of buckets, and the hash code value of an object is used to determine the bucket where the object would be stored, and the same hash code is used again to find the object’s position in the bucket.
Technical Interview round with questions based around Java.
Print even and odd numbers in increasing order using two threads in Java
The idea is to create two threads and print even numbers with one thread and odd numbers with another thread. Below are the steps:
Create two threads T1 and T2 using the below syntax, where T1 and T2 are used to print odd and even numbers respectively.
Thread T1 = new Thread(new Runnable() {
public void run() { mt.printEvenNumber(); }
});
Thread T2 = new Thread(new Runnable() {
public void run() { mt.printOddNumber(); }
});
where,
printOddNumber() is used to print all the odd numbers till N,
printEvenNumber() is used to print all the even numbers till N.
Maintain a global counter variable and start both threads using the below function:
T1.start();
T2.start();
If the counter is even in the Thread T1, then wait for the thread T2 to print that even number. Otherwise, print that odd number, increment the counter and notify to the Thread T2 using the function notify().
If the counter is odd in the Thread T2, then wait for the thread T1 to print that even number. Otherwise, print that even number, increment the counter and notify the Thread T1 using the function notify().
Internal working of CopyOnWrite ArrayList ?
"Copy on write" means whenever there is write operation such as add, set and so on. it copies the underlying array. So it is a thread-safe variant of ArrayList in which all mutative operations (add, set, and so on) are implemented by making a fresh copy of the underlying array. So basic idea is whenever we add or remove to the CopyOnWriteArrayList, the underlying array is copied with the modification. Remember this point, we'll see shortly in the code how CopyOnWriteArrayList do this.
It means, whenever there is modification done by thread that update the ArrayList, all other threads holding an older copy of different array. This is ordinarily too costly, but may be more efficient than alternatives when traversal operations vastly outnumber mutations, and is useful when you cannot or don't want to synchronize traversals, yet need to preclude interference among concurrent threads.
The underlying array reference is marked as volatile so that readers do not need to use a lock to see changes to the referenced array which means array update is an atomic operation and hence readers will always see the array in a consistent state. When a write operation occurs this volatile reference is only updated in the final statement via setArray() method. Up until this point any read operations will return elements from the old copy of the array.
Also, the write lock is required to prevent concurrent modification, which may result the array holding inconsistent data or changes being lost
Differences between Synchronized Collection and Concurrent Collection in Java
1. Synchronized Collection can be modified by one thread at a time(i.e. it is not possible to modify or access Synchronized Collection by multiple threads simultaneously). Concurrent Collection can be modified by multiple threads at a time(i.e. it is possible to modify or access Concurrent Collection by multiple threads simultaneously).
2. Concurrent Collection has high performance than Synchronized Collection because at a time multiple threads are allowed to operate on an object so it decreases the waiting time of the threads. More than one threads can perform read-write operation concurrently still it provides Thread Safety.
3. SynchronizedMap may allow null keys and null values depend on the real Collections class. ConcurrentHashMap does not allow null keys and null values.
Here's your problem of the day
Solving this problem will increase your chance to get selected in this company
Which SQL keyword removes duplicate records from a result set?