Interview questions: HashMap and Interface Serializable
807606Feb 15 2007 — edited Feb 15 2007Hi all,
I had a phone interview yesterday and the interviewer asked the following questions; I didn�t ask him if I answered correctly or not but am interested to know the correct or the best answer.
1) We have a HashMap with one million objects. (The key used to store objects in HashMap is an Object). What�s the worst case scenario of finding a value?
My answer: it depends on the HashCode() method you provide in order to store the objects in the Map. The best-case scenario is that no collision happens and you get the value with one search. The worst-case scenario happens when you have many collisions which means that we have more than one object stored in the same node of internal array and we have to use equal() method to find out which value is the value of the key we are looking for.
Is my answer correct?
He said that assume HashCode() method returns 5, and asked me to give him a numbder for the worst case scenario which I was not able to answer!
2) If I Serialize and object and then change the definition of the Class that object belongs to, do I get the correct results when I deserialize the object?
My answer: we serialize an object and write the state of the fields we want to serialize, to OutputStream using the writeObject() method; when we want to deserialize, we use the method readObject() to read the state of fields in the same order we used in writeObejct(); so if we change the definition of class after we sterilized the object, we won�t get correct results back.
Is my answer correct?
Any help is greatly appreciated.