Java questions: Urgent

N

Nikil Joshi

Hi All,

I was asked following questions in my interview so, I would appreciate
that if anyone of u would answer them:

1, how fast is hash map? explain in terms of time complexity?
2.how would u group multile statements as one transaction in JDBC?
3.how woould map inheritance to DB schema?
4.Which method do u overide in hashmap?something related to keys? I may
not be explaining it correctly.

Thanks!
 
R

Roedy Green

1, how fast is hash map? explain in terms of time complexity?

It depends on how relatively full it is. If you keep it less that 3/4
full your chains will be short and it wont' take any more time to look
up in a big list as a small. But if you let it approach 100% full,
then chains can get very long. In the most pathological case when all
your hashes are equal the lookup time is proportional to N, the size
of the list since all item are one one chain.

See http://mindprod.com/jgloss/hashcode.html
 
R

Roedy Green

4.Which method do u overide in hashmap?something related to keys? I may
not be explaining it correctly.
you don't override methods in HashMap. You override Object.HashCode
 
L

Luc The Perverse

Roedy Green said:
It depends on how relatively full it is. If you keep it less that 3/4
full your chains will be short and it wont' take any more time to look
up in a big list as a small. But if you let it approach 100% full,
then chains can get very long. In the most pathological case when all
your hashes are equal the lookup time is proportional to N, the size
of the list since all item are one one chain.

See http://mindprod.com/jgloss/hashcode.html

Isn't the textbook answer an order 0?

Then as collisions develop . . .
 
T

Thomas Hawtin

Nikil said:
I was asked following questions in my interview so, I would appreciate
that if anyone of u would answer them:

So why is it urgent?
1, how fast is hash map? explain in terms of time complexity?
2.how would u group multile statements as one transaction in JDBC?
3.how woould map inheritance to DB schema?
4.Which method do u overide in hashmap?something related to keys? I may
not be explaining it correctly.

4. is probably asking which methods from Object would you override when
writing a class designed to be used as a key for a HashMap.

Tom Hawtin
 
R

Roedy Green

It is urgent so that I don't repeat same mistakes in my next interviews.

You wont get the same questions. You should have answered "hashCode
and equals"
 
A

Adam Maass

Nikil Joshi said:
Hi All,

I was asked following questions in my interview so, I would appreciate
that if anyone of u would answer them:

1, how fast is hash map? explain in terms of time complexity?
2.how would u group multile statements as one transaction in JDBC?
3.how woould map inheritance to DB schema?
4.Which method do u overide in hashmap?something related to keys? I may
not be explaining it correctly.


Indeed, very common interview questions. I feel almost as bad answering
these as I would doing someone's homework, but for the benefit of the group:


1. The time complexity of HashMap is O(1) -- it's constant. (Read "big-oh of
one.")


2. Grouping multiple statements in JDBC:

Connection cnxn...

cnxn.setAutoCommit(false);

// do a bunch of stuff with Statements and/or PreparedStatements and/or
CallableStatements

cnxn.commit();


3. Mapping inheritence to a DB schema.

There are several (many) strategies for doing this, and trying to summarize
here is only going to lead to confusion. Try reading the Hibernate
documentation on subclassing, and understanding the many different ways
Hibernate supports for mapping subclasses.


4. What do you need to do to make a class you write usable as a key in a
HashMap?

You must override both hashCode() and equals(). hashCode() should be
implemented to provide as near a unique number based on the values of the
fields of the object as is reasonably efficient to compute. If equals()
returns "true" on an object, hashCode() on the two objects must return the
same integer.
 
A

Adam Maass

Nikil Joshi said:
Hi All,

I was asked following questions in my interview so, I would appreciate
that if anyone of u would answer them:

1, how fast is hash map? explain in terms of time complexity?
2.how would u group multile statements as one transaction in JDBC?
3.how woould map inheritance to DB schema?
4.Which method do u overide in hashmap?something related to keys? I may
not be explaining it correctly.


I've been interviewing for a new job recently (tons of jobs out there these
days; it looks like Silicon Valley is finally coming alive again), and my
favorite line of technical questions went like this:


What does System.identiyHashCode() do?
What does Object.hashCode() do?
What does String.hashCode() do?
Can you override String.hashCode()?
Why (or why not)?


-- Adam Maass
 
A

Alan Krueger

Adam said:
I've been interviewing for a new job recently (tons of jobs out there these
days; it looks like Silicon Valley is finally coming alive again), and my
favorite line of technical questions went like this:

What does System.identiyHashCode() do?
What does Object.hashCode() do?
What does String.hashCode() do?
Can you override String.hashCode()?
Why (or why not)?

The problem with questions like these, IMHO, is that these issues don't
generally affect most development and seem adequately covered by the
Javadoc. I'd be more concerned that the candidate knows when to use
synchronization, when to leave it off for efficiency, when to use each
kind of container class, and so on.
 
A

Adam Maass

Alan Krueger said:
The problem with questions like these, IMHO, is that these issues don't
generally affect most development and seem adequately covered by the
Javadoc. I'd be more concerned that the candidate knows when to use
synchronization, when to leave it off for efficiency, when to use each
kind of container class, and so on.

I definitely disagree. (That is, if the questions are asked in-person and
the candidate is not allowed to use the javadocs.) These issues can affect
development -- if you're doing certain things. This line of questions pretty
rapidly establishes the depth of the candidate's fluency with:

* The libraries
* The concepts of inheritence, overloading, and polymorphism
* The concepts of value-based classes, final classes, and immutibility.

All without picking on anything but classes present in java.lang!


Of course, synchronization and the collections framework should also be
probed during a technical interview...


-- Adam Maass
 
C

Chris Uppal

Alan said:
The problem with questions like these, IMHO, is that these issues don't
generally affect most development and seem adequately covered by the
Javadoc. I'd be more concerned that the candidate knows when to use
synchronization, when to leave it off for efficiency, when to use each
kind of container class, and so on.

My own (rather limited) experience of interviewing is that candidates who can't
answer questions like Adam's tend not to understand the basics of anything. I
don't know about Adam's questions /specifically/ (though they seem reasonable
to me), but in general easy-but-low-level questions seem quite good at exposing
the programmers who have learned by rote, or have been subjected to
"task-oriented" education. Precisely the ones who will not have the (very
moderate) understanding necessary to handle the kinds of issues you mention.

-- chris
 
T

Thomas Hawtin

Adam said:
2. Grouping multiple statements in JDBC:

Connection cnxn...

cnxn.setAutoCommit(false);

// do a bunch of stuff with Statements and/or PreparedStatements and/or
CallableStatements

cnxn.commit();

When I skimmed through the questions, I read that as referring to batch.
Auto-commit off is just automatically assumed reflex.
3. Mapping inheritence to a DB schema.

There are several (many) strategies for doing this, and trying to summarize
here is only going to lead to confusion. Try reading the Hibernate
documentation on subclassing, and understanding the many different ways
Hibernate supports for mapping subclasses.

The three common ways:

o Everything in one table with a concrete class id which indirectly
indicates which fields are valid (the constraints are a nightmare...).
o A table per concrete class. Base class columns are repeated
throughout. Not so good with auto-identity columns. PostgreSQL's INHERIT
comes in useful.
o A table per extension. All objects are represented in the base class
table. Subclasses each have their own table with their private fields
and a primary key that is a foreign key linking back to the base table.
Again, not so good with auto-identity columns. Lots of joins, but not
necessarily too bad in terms of performance.

Martin Fowler's Patterns of Enterprise Architecture has a section on
this sort of thing.
4. What do you need to do to make a class you write usable as a key in a
HashMap?

You must override both hashCode() and equals(). hashCode() should be
implemented to provide as near a unique number based on the values of the
fields of the object as is reasonably efficient to compute. If equals()
returns "true" on an object, hashCode() on the two objects must return the
same integer.

Or nothing at all if the identity behaviour is required...

Tom Hawtin
 
L

Lion-O

What does System.identiyHashCode() do?
I definitely disagree. (That is, if the questions are asked in-person and the
candidate is not allowed to use the javadocs.) These issues can affect
development -- if you're doing certain things.

Being new on the platform I can relate to many of the described situations (and
its becoming clear that I still have a lot to learn) but I can't help wonder
about something here... You state that your line of questioning will help give
the questioner insight to the understanding of some basics like:
* The libraries
* The concepts of inheritence, overloading, and polymorphism
* The concepts of value-based classes, final classes, and immutibility.

But doesn't it also implying that the person in question needs to know certain
libraries and their inner workings from mind ? I somewhat cannot fully place
that, since Java is so extensive (from my point of view at least) that I find
it rather hard to be able and grasp the meaning / inner workings of certain
classes / methods without the Javadoc API specs.

I can't help wonder if focussing on understanding rather than knowledge isn't a
big issue here as well. For example, I might have used 'String.hashCode' a few
times now and thus know from mind that its returning an hash code (which is a
little obvious) yet don't know 'String' well enough to know from mind about its
final state. But I do know that 'final' means it can't be overridden.

Wouldn't you rule out some people who might have a certain potential here?
Unless ofcourse you'd allow answers like "I have no idea if it can it, but if
its final it cannot".

I can't help wonder if a question in the likes of "When can you override a
method and when not?" would point out a more global understanding since now
you're asking about the 'why' instead of focussing on a single entity which
people may or may not know from mind.

--
Groetjes, Peter

..\\ PGP/GPG key: http://www.catslair.org/pubkey.asc




This line of questions pretty
 
A

Andrea Desole

Chris said:
My own (rather limited) experience of interviewing is that candidates who can't
answer questions like Adam's tend not to understand the basics of anything. I
don't know about Adam's questions /specifically/ (though they seem reasonable
to me), but in general easy-but-low-level questions seem quite good at exposing
the programmers who have learned by rote, or have been subjected to
"task-oriented" education. Precisely the ones who will not have the (very
moderate) understanding necessary to handle the kinds of issues you mention.

I think the question is how easy and how low-level the questions must
be. Personally, in 4 1/2 years of Java:

- I have never used System.identityHashCode()
- I have never used Object.hashCode()
- I might have even never implemented a hashCode() method
- I might have never looked at the performance of a hash map (I think I
did, but I'm not sure)

Still:

- I know what hashCode() does
- I would probably be able to find out the faster algorithm

I guess I can now assume that I wouldn't be hired everywhere, but I
would say that there are a lot of things you never really use, and you
never look at them, or you look at them and then you forget. There might
be jobs where questions like these are extremely important, and you
really have to know it, but I don't think this is the usual case.
Knowledge of the concepts and attitude are the most important thing; I
personally prefer someone who doesn't know how fast a hash map is but
writes his code with comments, just to make an example. One of the best
developers I have met didn't even know what design patterns were.
And, of the questions I have seen, the one I appreciate most is the
question about inheritance-DB mapping. Although it's not really Java related
 
C

Chris Uppal

Lion-O said:
But doesn't it also implying that the person in question needs to know
certain libraries and their inner workings from mind ? I somewhat cannot
fully place that, since Java is so extensive (from my point of view at
least) that I find it rather hard to be able and grasp the meaning /
inner workings of certain classes / methods without the Javadoc API specs.

That's a reasonable attitude, but I think that Adam's questions are a bit more
subtle than you might think. I'll try to give answers to them (not the best
possible answers, just acceptable ones), to say why those answers must be true,
why I would expect almost any Java programmer to be able to answer them
correctly, and why I think they do probe some aspects of real understanding.

BTW, if you aren't yet able to answer these yourself, please don't take it
personally ;-) You've said that you're a beginner here, and everyone has to
start somewhere.

It's easier to take the questions out of order.

Q What does Object.hashCode() do?
A Gives you an integer that can be used to locate objects in hashed collections
like HashSet or HashMap.
Since HashSet and HashMap are important utility classes, I would expect anyone
to have at least some experience with them. More on this below.

Q What does String.hashCode() do?
A It gives you a number to use as a hash code which depends only on the
characters in the String.
It may seem surprising that this is something I regard as basic understanding,
So basic that I would /require/ someone to know it or be able to work out the
answer. The logic is as follows:
Strings are absolutely central to Java programming.
There are some basic facts about Strings that everyone
/has/ to know.
One of those facts is that you shouldn't compare Strings
using ==. You must (nearly always) use the equals()
method, which overrides the Object.equals() method to
compare the two strings character-by-character.
Therefore (see below) Strings must also provide their
own override of hashCode() too, which is compatible with equals(). (To use
the technical term; you are not requred to know the jargon ;-)
To be compatible with equals() requires that the hashCode()
only depend on the characters in the String.
Therefore that's what String.hashCode() does.
(A more knowledgeable programmer might know more, but anyone should be able to
answer that much -- at least if they weren't too nervous to think.)

Q Can you override String.hashCode()?
A No.
If the answer was because String.hashCode() was declared final then it's not
something I would regard as particularly indicative -- it would just be one of
those facts that you either know or don't know. But the actual reason is that
the String class /itself/ is final, and /that/ is one of the basic facts about
Strings that I think everyone should know (another such fact is that Strings
are immutable).

Q What does System.identiyHashCode() do?
A It tells you a hash code for an object that only depends on /which/ object
it is. So that it is compatible with ==, in the same way as hashCode() is
expected to be compatible with equals().
To be honest, I wouldn't call this question as relevant as the others. I can
easily imagine someone being moderately competent and never having come across
the need for hashing based on identity. OTOH, even a moderate curiosity about
the java.util package would lead a programmer to discover these things -- and
java.util is one of the packages that I would expect /any/ Java programmer to
be curious about.

One assumption that I've made in the above is that you are expected to know
about the relationship between hashCode() and equals(). The reason for that is
simply that I would expect any programmer to have used the hashed collections,
I would expect any programmer to have had some experience of creating their own
classes, and I would consider it extraordinary if that programmer had never had
to put instances of his or her classes into a collection nor ever had to
compare them for equality. These are basic skills. And in order to do these
things, you need to understand equals() and hashCode() and the relationship
between them.

Obviously those questions don't cover all the basics. There are probably other
(equally small) sets of questions that would probe a selection of basics that
I'd prefer. But -- as I've said -- my experience is that people tend either to
have the kinds of minds, experience, and training, that emphasise
/understanding/ and therefore naturally cover all the basics, or they have
missed out badly somewhere. And in the latter case, I would not care to employ
them.

-- chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top