Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java interview - Do I need to know how iterators are implemented ?

 
Andy Jack
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was looking at the Java code for some iterators. I am not sure if this is something I should know well. Is it okay if I know only what an iterator can do for me and how to use it ?

Thanks
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe it is much more important to know how to use an Iterator than it is to know how to implement one, especially for an entry level position.

That said, I love to ask questions that the interviewee doesn't expect and can't know to prepare for. So that would make a quick "implement an Iterator for a Vector, assuming that there is not one already" a good test. I'd give you the Javadoc for Iterator, and give a whiteboard.
 
Andy Jack
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pat Farrell wrote:I believe it is much more important to know how to use an Iterator than it is to know how to implement one, especially for an entry level position.

That said, I love to ask questions that the interviewee doesn't expect and can't know to prepare for. So that would make a quick "implement an Iterator for a Vector, assuming that there is not one already" a good test. I'd give you the Javadoc for Iterator, and give a whiteboard.


Thanks. You mentioned that application is more important for an entry level position. So, would the inner details of iterators be important for higher positions ? If so, then why ?
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andy Jack wrote: So, would the inner details of iterators be important for higher positions ? If so, then why ?

Suppose you were interviewing at Google for their Guava team. (if you don't know about Guava, google for it, its a great library). They write a lot of classes that implement the Collection interface, so they have to implement an Iterator for their new classes. Nearly everything that Google does is about searching through lists and producing new result lists. So for that job, you would have to at least be able to clearly define and implement a good iterator.

More importantly, the whole point of Object Oriented Programming is to use abstraction to hide implementation details. A more senior position would often create the abstractions and implement them, while more junior folks use them.

If you look at the source code to Google's Guava (its open source, easily available) you will see examples of good professional software. Its well documented in the complete Javadocs. The code itself is concise and aims for maximum reuse of other classes, and reflects the clean design of the whole package that is Guava. You would expect that the Iterator implementation for an ImmutableList would be very similar and have a lot of reuse of code from say their ImmutableSet class.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34837
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a different opinion, I wouldn't expect someone to know how existing iterators are implemented unless you say you've written an Iterator. It's not something most people give deep thought.

I could envision an interviewer asking it though. Most people should be able to figure it out.
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andy Jack wrote:I was looking at the Java code for some iterators. I am not sure if this is something I should know well. Is it okay if I know only what an iterator can do for me and how to use it ?

Thanks


Yes it is ok. Until an interviewer asks you how to implement one

Most interviewers will not ask you how the internals of any system / utility class works. However it is always nice to know. Iterators have several interesting properties to explore.

1. How do you make sure iterators work properly when used across threads. Should you care ?
2. Does the immutability of a data structure affect how iterators behave ?
3. When should an iterator throw ConcurrentModificationExceptions ?
4. How do system classes know that the data structure that the iterator is operating on has changed ?
5. How can you make any arbitrary class function inside a for-each loop ?

and so on...

Explore several iterators before saying you know how to implement one. Try implementing an iterator to boost your confidence.
 
Timothy Brown
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My experience in interviews so far has been more along the lines of recreating elements in the Java API's, not using them. I don't want to state the exact question, because I signed a form that I wouldn't, but essentially, say you didn't have the Collections API and really needed one of the commonly used data structures. Ok, now build me one, handle collisions, and while you're at it, make it fully generic. A different interview had me check to see if a tree was in balance. Another one yet was to count unique shortest-paths on a graph using a depth first search - recursively, and then prove mathematically my answer was correct. So, to answer your questions specifically, don't worry so much about knowing Java as a language, know how to program USING Java as your language. The questions you'll likely get will not matter what language you use at all.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic