This question is often asked in many companies including amazon,goldmann sachs, Microsoft and google. I also encountered this question in my 2nd round with GoldMann Sachhs, Morgan Stanley. Although they take 6 rounds in all and unfortunately due to some HR discussion i came out, so just wanna share some thoughts on this problem.
Sunday, May 4, 2014
Analytical Questions asked in goldmann Sachhs
Recently while giving an interview for GoldMann Sachhs few weeks back, i was encountered with various analytical questions and thought of sharing with you all these.
1) First question was if u have given a cake and you have to cut that cake into 8 equal parts but only by using 3 cuts, how will you do that?
1) First question was if u have given a cake and you have to cut that cake into 8 equal parts but only by using 3 cuts, how will you do that?
Saturday, May 3, 2014
Object Serialization and How it is done?
We encounter many questions on Object serialization and in this blog i am writing my experience of goldmann sachhs interview when i gave it. They asked me questions on serialization in a series of steps.
Firstly they asked have u heard of Serialization mechanism in java? Then they asked if yes, how it is done. That is if i have to serialize an employee object write a program that would serialize the employee object and store it in a file and then deserialize it.
We all know that Object Serialization refers to a mechanism of saving the state of an object when passing it over a network. Now the question arises is when we serialize an object we all know we have to implement a Serializable interface in our Class and give a unique serialVersion UID for that class. Now the next question arises is if i have to save the state of an object in a file, then write a code that would do that and what if i wont give this unique serialVersionUID in my class.
Firstly they asked have u heard of Serialization mechanism in java? Then they asked if yes, how it is done. That is if i have to serialize an employee object write a program that would serialize the employee object and store it in a file and then deserialize it.
We all know that Object Serialization refers to a mechanism of saving the state of an object when passing it over a network. Now the question arises is when we serialize an object we all know we have to implement a Serializable interface in our Class and give a unique serialVersion UID for that class. Now the next question arises is if i have to save the state of an object in a file, then write a code that would do that and what if i wont give this unique serialVersionUID in my class.
Friday, May 2, 2014
Equals Method In Java and == operator
We all know that likewise hashCode() method equals() method is also defined in java.lang.Object class which is the parent class of every class and by default inherited in any class which we write. Now along with the pedagogy of hashCode(), we have equals method to which interviewers often ask and confuse us with "The HashCode() and Equals() Contract".
Before deep diving into "equals()" method of java i will first like to compare the difference between "==" and "equals" methods which might confuse developers at many instances.
Lets say, i have an Integer Object,i.e
Integer i=10;
Integer j=i;
The "==" operator, tests to see if the two object reference variables refer to the exact same instance of an object whereas The equals() method in java tests to see if the two objects being compared to each other are equivalent but they need not be the exact same instance of the same Object.
Before deep diving into "equals()" method of java i will first like to compare the difference between "==" and "equals" methods which might confuse developers at many instances.
Lets say, i have an Integer Object,i.e
Integer i=10;
Integer j=i;
The "==" operator, tests to see if the two object reference variables refer to the exact same instance of an object whereas The equals() method in java tests to see if the two objects being compared to each other are equivalent but they need not be the exact same instance of the same Object.
Thursday, May 1, 2014
LinkedList Traversal and finding nth node from last in a linked list
We all know that java provides with an API inbuild which has LinkedList class implementing the List interface, but here we are not using any inbuilt API and we are creating a linked list by our own and finding the nth node from last using our own linkedlist class.
This question was asked me during goldmann Sachhs interview, as u people know goldmann sachhs has this procedure of having 6 rounds. I went till last round of GS, although due to some HR discussion i didnt joined but thought to discuss with you, the question which they asked.
So the question was: I have a linked list and i want to find the nth node from the last in a linked list.
This question was asked me during goldmann Sachhs interview, as u people know goldmann sachhs has this procedure of having 6 rounds. I went till last round of GS, although due to some HR discussion i didnt joined but thought to discuss with you, the question which they asked.
So the question was: I have a linked list and i want to find the nth node from the last in a linked list.
HashCode and Equals--> All time favourite
We all know that there are two methods hashCode() and equals() present in Object class. We have always heard of that hashCode is used to give the hashcode of an object which is nothing but the memory address of the object stored and equals is used for equivalence relation match or to test whether two objects are equal or not.
Well this sounds good, but a lack of proper knowledge in these aspects will lead you to in trouble and when the interviewer asks about these concepts in detail, you will definitely get confused. So to make it more easy to understand and to practically implement these methods in our application, i have come up with this post.
HashCode method is defined in java.lang package which is by default inherited in any class. The hashCode method returns an integer value which is nothing but it is derived by mapping the memory address to an integer value. It is a native method defined in Object class of java.lang package that is it is implemented directly in the native code in the JVM.
Well this sounds good, but a lack of proper knowledge in these aspects will lead you to in trouble and when the interviewer asks about these concepts in detail, you will definitely get confused. So to make it more easy to understand and to practically implement these methods in our application, i have come up with this post.
HashCode method is defined in java.lang package which is by default inherited in any class. The hashCode method returns an integer value which is nothing but it is derived by mapping the memory address to an integer value. It is a native method defined in Object class of java.lang package that is it is implemented directly in the native code in the JVM.
Finding the number of one's in an array of zeroes and ones with O(n) complexity.
This is although a simple question but many people might not be aware of this one as when we encounter such problems we feel dumbstruck at one instance. So its better to prepare for such questions, although they look easy but its difficult to get an idea at that time.
This one was asked me during manhatan associates interview question.
So the problem is : You have given an array of zeroes and ones. Now you have to calculate the number of ones and zeroes in that array with the most efficient approach. When you are given first time this problem the basic approach with which a fresher or even an experienced guy having a work ex of 2 years also think in this way:
This one was asked me during manhatan associates interview question.
So the problem is : You have given an array of zeroes and ones. Now you have to calculate the number of ones and zeroes in that array with the most efficient approach. When you are given first time this problem the basic approach with which a fresher or even an experienced guy having a work ex of 2 years also think in this way:
Finding the sub array from an array of positive and negative integers havng maximum sum
Few months back, i went to gave GoldMann Sachhs interview and i was puzzled with this interview question:
They told me you have give an array of positive and negative integers and you have to find the sub array from the given array which sums up to the maximum.
For example:- if you have
{-2, -3, 4, -1, -2, 1, 5, -3}
If you see the above series you have a set of negative and positive numbers and now you have to find the first sub array which has the maximum sum, it can contain either one element or a set of elements.
They told me you have give an array of positive and negative integers and you have to find the sub array from the given array which sums up to the maximum.
For example:- if you have
{-2, -3, 4, -1, -2, 1, 5, -3}
If you see the above series you have a set of negative and positive numbers and now you have to find the first sub array which has the maximum sum, it can contain either one element or a set of elements.
Wednesday, April 30, 2014
Comparator Interface -- When we want to sort by our own!
As discussed in my previous blog, we got a brief idea about java.lang.Comparable Interface which is used to sort the elements in a natural ordering. Now in this tutorial, we will learn something new which is comparator interface.
This interface is defined under java.util package so that's why this is sometimes called as a utility interface which should be used as a utility for sorting the objects.
--> Comparator interface in java has a method known as public int compare(Object 1,Object 2) which returns a negative integer, zero or positive integer as obejct 1 is less than, equal to or greater than object 2.
Why Comparator?
Now the question comes , why we want comparator interface when we want to sort the objects by primary key attribute we can do it by comparable Interface. den what is the need of Comparator Interface? And the answer lies here:-
Comparable Interface- a natural Ordering mechanism
We all know about comparable and comparator interface in java a bit but when we deep dive into coding and how they are implemented, there comes the major problem.
Accorind to Oracle Docs http://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html, the natural ordering for a class is said to be consistent with equals if and only if e1.compareTo(e2)==0 has the same boolean value as e1.equals(e2) for every e1 and e2 of class. Note that null is not an instance of any class, and e.compareTo(null) should throw a NullPointerException even though e.equals(null) return false.
Comparator Interface is present in java.lang package which is by default inherited in any class when we write in java. This interface when implemented in any class will help us to compare the current object with any other object of the same class and it imposes a total ordering on the objects of the class that implements it. This ordering is referred to as the class's natural ordering, and the class's compareTo method is referred to as its natural comparison method.
Accorind to Oracle Docs http://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html, the natural ordering for a class is said to be consistent with equals if and only if e1.compareTo(e2)==0 has the same boolean value as e1.equals(e2) for every e1 and e2 of class. Note that null is not an instance of any class, and e.compareTo(null) should throw a NullPointerException even though e.equals(null) return false.
Tuesday, April 29, 2014
First Non Repeating Character in a String
We all know that when we go for companies like amazon or British Telecom, they have a very famous question which is asked very frequently is
Find the first non repeating character in a String which has many repeated alphabets.
I went for amazon interview and encountered such question.
The approach to this question will be first we will think the pseudo -code for that, then we will approach for the exact coding.
So when i was asked this question, i firstly had this pseudo code to make it the most optimize solution for this question.
Find the first non repeating character in a String which has many repeated alphabets.
I went for amazon interview and encountered such question.
The approach to this question will be first we will think the pseudo -code for that, then we will approach for the exact coding.
So when i was asked this question, i firstly had this pseudo code to make it the most optimize solution for this question.
Fibonacci Series with Reading the Number from a file placed in your Local System
One of this question i encountered while giving interview of Oracle, although qualified well, but at times people gets confused with various Stream Classes defined in java.io package. So to make it more easy to understand and to cope up with Question too, i have come up with a technique where people will read the number upto which we want to print the fibonacci series to from a file and then printing the fibonacci series using recursion.
Employee Department One to Many Relationship implemented using hashMap In java
As we all know that When we are preparing for Java Interviews for some big giants including all investment banks, or topmost companies including Amazon,Oracle or Alcatel you will definitely encounter questions from core java and data structures including algorithmic approach of any given problem. To cope up with, i have come up with this blog. When we go for a java interview, recently to GoldMann Sachhs i was able to reach till 6th round and then they put me on halt.