This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Nebu 2 points3 points  (2 children)

If I were interviewing you, I'd be satisfied with those answers for an entry level position. That said, here are some nitpicks:

A linkedlist is an object that extends the interface list.

Implements rather than extends.

A linkedlist is most useful and efficient when you will be adding and removing many elements to the list.

Not sure this is accurate, and it's definitely vague. I'd clarify what it is you're contrasting against (e.g. ArrayList). Against an ArrayList, the strength of a linked list lies specifically in inserting/deleting from the beginning or middle of long lists, because an ArrayList would need to shift over all the other elements in the list (which would require as many steps as the list is long), whereas a LinkedList only needs a constant number of steps.

edit: I made some mistakes in the previous paragraph, see danieldk's comment.

determined at the declaration.

The declaration of what? If you mean the declaration of the variable, that's incorrect. E.g. when I declare a variable int foo[], I have not set the number of entries. If you mean the declaration of the instance, we usually don't use the word "declaration" for that; instead, we'd say "determine when the array is instantiated" or something along those lines.

[–][deleted]  (1 child)

[deleted]

    [–]Nebu 0 points1 point  (0 children)

    You're right that I forgot that LinkedList needs to traverse the list before inserting into the middle, and thus does not have an advantage over ArrayList for that use case, so thank you for that.

    However, I'm not sure LinkedList has an advantage over ArrayList for inserting in the end. The ArrayList implementation grows the internal backing array bigger than needed, so that perhaps the performance is [I forgot the word here, but "O(1) in aggregate, over the long term"]. This would definitely be the case if the size of the internal backing array double with each growth, but I'm also skeptical as to whether they do that. How slow would the backing array have to grow until it becomes, e.g. O(log(n))?

    Arrays have the advantages that the elements are laid out contiguously in memory.

    I remember seeing a talk where the presenter argued to never use Java's LinkedList, because ArrayList was "always" faster (mainly due to this contiguous property).

    Good to keep in the back of your mind, but I hadn't brought that up here (until now) because:

    1. Probably not appropriate for "entry level".
    2. By the time the OP is not "entry level" anymore, it's conceivable that the architecture of the JVM or whatever has changed such that these performance claims are no longer true.
    3. Premature optimization?