Java questions: Urgent

Discussion in 'Java' started by Nikil Joshi, Jan 31, 2006.

  1. Nikil Joshi

    Nikil Joshi Guest

    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!
     
    Nikil Joshi, Jan 31, 2006
    #1
    1. Advertising

  2. Nikil Joshi

    Roedy Green Guest

    On 30 Jan 2006 20:08:31 -0800, "Nikil Joshi" <>
    wrote, quoted or indirectly quoted someone who said :

    >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
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Jan 31, 2006
    #2
    1. Advertising

  3. Nikil Joshi

    Roedy Green Guest

    On 30 Jan 2006 20:08:31 -0800, "Nikil Joshi" <>
    wrote, quoted or indirectly quoted someone who said :

    >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
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Jan 31, 2006
    #3
  4. "Roedy Green" <> wrote in
    message news:...
    > On 30 Jan 2006 20:08:31 -0800, "Nikil Joshi" <>
    > wrote, quoted or indirectly quoted someone who said :
    >
    >>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


    Isn't the textbook answer an order 0?

    Then as collisions develop . . .

    --
    LTP

    :)
     
    Luc The Perverse, Jan 31, 2006
    #4
  5. Nikil Joshi

    Nikil Joshi Guest

    It is urgent so that I don't repeat same mistakes in my next interviews.
     
    Nikil Joshi, Jan 31, 2006
    #5
  6. Nikil Joshi wrote:
    >
    > 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
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
     
    Thomas Hawtin, Jan 31, 2006
    #6
  7. Nikil Joshi

    Roedy Green Guest

    On Mon, 30 Jan 2006 21:28:51 -0700, "Luc The Perverse"
    <> wrote, quoted or indirectly
    quoted someone who said :

    >
    >Isn't the textbook answer an order 0?

    no. It is somewhere between 0 and n depending on how ineptly you set
    it up.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Jan 31, 2006
    #7
  8. Nikil Joshi

    Roedy Green Guest

    On 30 Jan 2006 20:33:52 -0800, "Nikil Joshi" <>
    wrote, quoted or indirectly quoted someone who said :

    >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"
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Jan 31, 2006
    #8
  9. Luc The Perverse wrote:
    >
    > Isn't the textbook answer an order 0?


    You mean O(1)?

    > Then as collisions develop . . .


    Supposedly it remains O(1), with lumps. I haven't done or seen the
    mathematics to check that.

    Tom Hawtin
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
     
    Thomas Hawtin, Jan 31, 2006
    #9
  10. Paulus de Boska, Jan 31, 2006
    #10
  11. "Thomas Hawtin" <> wrote in message
    news:43def5e8$0$1463$...
    > Luc The Perverse wrote:
    >>
    >> Isn't the textbook answer an order 0?

    >
    > You mean O(1)?


    Yeah

    --
    LTP

    :)
     
    Luc The Perverse, Jan 31, 2006
    #11
  12. Nikil Joshi

    Adam Maass Guest

    "Nikil Joshi" <> wrote:
    > 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.
     
    Adam Maass, Feb 1, 2006
    #12
  13. Nikil Joshi

    Adam Maass Guest

    "Nikil Joshi" <> wrote:
    > 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
     
    Adam Maass, Feb 1, 2006
    #13
  14. Nikil Joshi

    Alan Krueger Guest

    Adam Maass wrote:
    > 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.
     
    Alan Krueger, Feb 1, 2006
    #14
  15. Nikil Joshi

    Adam Maass Guest

    "Alan Krueger" wrote:
    > Adam Maass wrote:
    >> 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.


    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
     
    Adam Maass, Feb 1, 2006
    #15
  16. Nikil Joshi

    Chris Uppal Guest

    Alan Krueger wrote:

    > > 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.


    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
     
    Chris Uppal, Feb 1, 2006
    #16
  17. Adam Maass wrote:

    > 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
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
     
    Thomas Hawtin, Feb 1, 2006
    #17
  18. Nikil Joshi

    Lion-O Guest

    >>> 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.


    > 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
    > 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
    >
    >



    --
    Groetjes, Peter

    ..\\ PGP/GPG key: http://www.catslair.org/pubkey.asc
     
    Lion-O, Feb 1, 2006
    #18
  19. Chris Uppal wrote:

    >
    > 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
     
    Andrea Desole, Feb 1, 2006
    #19
  20. Nikil Joshi

    Chris Uppal Guest

    Lion-O wrote:

    > 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
     
    Chris Uppal, Feb 1, 2006
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sachin Jagtap

    URGENT !! QUEUE STL PROBLEM URGENT!!

    Sachin Jagtap, Apr 28, 2005, in forum: C++
    Replies:
    4
    Views:
    1,047
    Mark Stijnman
    Apr 29, 2005
  2. Shalz
    Replies:
    0
    Views:
    636
    Shalz
    Mar 8, 2008
  3. Isaac
    Replies:
    0
    Views:
    337
    Isaac
    Aug 16, 2010
  4. Rob
    Replies:
    3
    Views:
    443
  5. Replies:
    1
    Views:
    270
Loading...

Share This Page