« JSR 311 not RESTful? | Main | Introductory REST Article »

2007.11.30

Comments

Feed You can follow this conversation by subscribing to the comment feed for this post.

Anonymous
write a service locator instead?? but the service locator ends up being a singleton. definitely no hire.
Achille
Singletons in java are harmful.

a) a singleton pattern in java does not mean only one class per VM, it means only one class per classloader, you can have multiple classloaders in EE. A truly single singleton is impractical as requires searching of the entire heap space.

b) You are mixing use and management in a class. Making the class responsible for its own management is not not advised, a factory pattern is better.

c) A singleton can slow down the garbage collector as it may allow a dead (dereferenced) class to find the singleton and crawl out to life.
JD00D
Echoing Achille, based on your knowledge of the Singleton pattern and its real world usage, I would ironically pass on *you* as a hire. Google can educate you out of your incompetence and reduce that confidence.
Thomas Lockney
Good points from Achille. I would add to the list of questions: 7. What are the benefits of using a singleton? Why might a singleton be dangerous to use?
cr
"Ignorance is bliss" is a famous phrase of George Orwell's 1984.
Joseph Roman
This is disgraceful. Job interviews are hard enough without putting us through this kind of added stress. Why stop there? Why not push me into a cage of man-eating tigers and ask me to wrestle with them, or fire a submachine gun at me and see if I can dodge the bullets. All I'm asking for is a job to put some food on the table, I don't see that you have to put me through some kind of intellectual assault course.
pb
Yes, you should be hired because of your need of food on your table. Probably bang out some nice code, or so we hope. Hell let's just hire you and find out.
Robert
The one push back I would have given is that in Java singletons are not really singletons (as most already know due to classloaders), but the _pattern_ of it is used quit often. No they may not be true singletons, but unless you are doing something potentially dangerous the singleton pattern is quite helpful. Or useless if you are playing devil's advocate... Your use of the question is highly relevant in an interview however and can show just how much a candidate knows about the subject of patterns and Java for the matter. I completely disagree with Joseph Roman. These types of questions show not only what you know right away but also if you know enough to intelligently come up with alternatives or the merits of one approach over another.
Reg Braithwaite

Mr. Roman: I sympathize with you. I have had problem interviews myself, I recall being unable to write a Java program to print the first 100 primes!

But what, do you suppose, will your first day on the job be like? Won’t it be stressful to be asked to implement Resource Discovery or Dependency Injection?

Won’t you be asked to discuss your implementation in detail at your first code review?

Job interviews are stressful. But so are jobs. The questions above are all reasonable ones to ask of someone writing code as part of a code review.

We can quibble about the choice of subject matter: many people I respect feel the question is actually too easy to be useful in an interview. But the overall approach is appropriate for a job as a programmer.

I encourage you to overcome the very natural aversion to stress in interviews. One of the best ways to start is to practise. Research various questions and tests like this, and then sit down in a quiet place and actually practise writing the code from scratch and answering the questions.

It will make you more comfortable in actual interviews, and you will get the job you deserve.

Best of luck.

Anonymous
Who was the pompous, self righteous, git? The candidate or the project manager?
Jon H
I wrote a blog post some time ago complaining about interviewers who think that whiteboarding code is a decent interview technique: http://codeeleven.blogspot.com/2007/11/want-job-reverse-string-sad-state-of.html and a follow up: http://codeeleven.blogspot.com/2007/11/whiteboarding-code-part-duex.html
Justin George
Or you could use a language with powerful enough idioms that Singleton is unneeded.
bhaskar
If someone asks me a question "Is this class thread safe ?", I will walk out right at that time. There can't be a more idiotic question , than that.Whether some piece of code is thread safe or not, depends on whether it accesses class level or instance level variables. To correctly phrase your question, "Is the singleton initialization, thread safe ?"
BBG
@bhaskar I am deliberately ambiguous on that point when I ask the question and as it happens, a lot of people ask me to clarify. I take it as a very positive sign when people do that. I'm sorry that such an idiotic question is an insult to your intelligence. Perhaps you are right, perhaps you aren't, but the fact that you find the question so insulting that you would walk out of an interview seems to me to be a career limiting attitude. Wouldn't you want to at least engage in a conversation about it first?
bhaskar
Gavin, Let me give you a quick example of the other side of the coin. I was once asked at an interview that if s1 and s2 are java strings, then will "s1 == s2" return true, if both have the same value. I said that depends, if s1 and s2 both point to the same instance, then yes it will indeed return true. Even before I could complete the answer that you should use the .equals to compare 2 strings , the interviewer cut me off, and pointed out that I should use .equals instead. I politely reminded him that the question was "Will s1 == s2 return true ?". And I was listing the possible case when that expression would evaluate to true. I have never walked out of interviews once in my life. But when I start to get the feeling that the interviewer is more interested in showing how talented he his (even though he may not be), then what's the point in being bashful about your own abilities.
raveman
wow, people really dont know that? but to be fair, it useless knowlage, sorry but i think it will never help with anything. you can always use debugger. i never understood why people love singleton so much, i prefer util class, it cant be extended too and its also functional programming.
Mark
Two points: I would pass on your Mom as a candidate (no offense) because "Ignorance is Bliss" is a commonly misunderstood and misused cliche... The full quote is "where ignorance is bliss, 'Tis folly to be wise.", (Thomas Gray)
I've developed software for over 15 years, started 3 successfull companies, developed several e-commerce web sites and NEVER used a singleton. I guess you would pass on me, but I'm not sure what this question would say about me as a successfull contributor that can get the design and implementation done properly
BBG
@Achille, JDOOD, Mark - It is funny how people think I am somehow advocating the use of the Singleton. I explicitly stated in the post that "I think the Singleton is pretty much irrelevant". I use that pattern because everyone knows it and can write one up in a couple of minutes. Then the discussion opens up and you can have all sorts of interesting dialogue. Some of the topics I've had fun discussing include inheritance versus composition, Inversion of Control frameworks, JRuby, how it would work in an OSGi container, and the list goes on. It's not about the Singleton! If you have a suggestion for something else that everyone knows, fits on one page, and can be written in a couple of minutes, then I'd love to hear it.
Mark
Thanks for the good discussion. My point is that asking "irrelevant" tech questions during interviews produces "irrelevant" results. If you're going to ask general logic questions then I think it's better to aks questions like "why are manhole covers round", or "how many piano tuners are there in NYC" etc. This removes "domain specific" knowledge requirement. My general interviewing technique (for small teams) is (after selecting someone on the basis of their resume) to determine if the personal chemistry is a good fit. Then I ask them about the work that they have done, often asking them detailed and challenging questions about what they have done. If they thoroughly explain what they did and why they did it then I'm confident that they will be able to find the appropriate use of a Singleton should the need arise.
Andy Tripp
Very nice post. I go through the same kind of questions when interviewing. I always enjoy asking tougher and tougher questions to make sure the candidate is able to say "I don't know". I'm amazed at how many people will make something up or just get flustered when they don't know. Also, the responses here are similar to responses in a real interview. You get the guy who rants against singletons and never answers the question. I wouldn't hire him - he'll spend his time researching singletons in various languages rather than getting the job done. You get the guy who doesn't know any answers and (presumably) can't even hold a decent conversation about them - probably not qualified. You get the guy with the attitude who's offended that you'd ask such a "stupid question" - don't hire him. Same for the guy who suggests just using a different language ("how's that worked out for you in the past?"). And you get the guy who's full of himself, who corrects a cliche just so he can show that he knows the original quote, yet never says how he avoids the singleton. So I think you're right on: ask those questions, look for a couple of right answers and a lot of good discussion, and watch out for bad attitude, people who can't say "I don't know", and people who can't control their need to show you they know more than you.

The comments to this entry are closed.