Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I have been recently assigned the task of taking interviews in my company. I have been taking interviews of developers and engineers with atleast 5-6 years of experience.

One of my favorite question is to ask whether they have used any kind of patterns...and the default answer has always been "Singleton Pattern"

Since it is the most simple pattern, I always end up asking them to write a code for it, and only 1-2 out of 5 end up getting it right, but get confused when I start asking about thread safety, double-checked locking issues and etc..

My questions are:

  1. Is it fair on my part to expect a person with 5-6+ years of experience to know how to write a Singleton pattern?
  2. Is it fair on my part to judge a person upon their ability to code a Singleton class?

EDIT: Some clarifications. - When I ask if they know anything about design patterns, I would like to know what kind of GoF patterns they have used, why they have used and how they have used them. And with 5-6 years of development experience, I would expect a person to know about GoF patterns...correct?
- The first answer which everyone say when I ask about patterns is "I have used and written Singleton Classes". Singletons are at the cusp of being declared an anti-pattern...with asking them to write about it, I try to gauge 1. If they can code, 2. If they can code what they claim they could code, 3. Tell me the problems with Singleton...

Do I sound more reasonable now? :)

share|improve this question

migrated from stackoverflow.com Aug 31 '11 at 18:45

This question came from our site for professional and enthusiast programmers.

    
Relevant - ibm.com/developerworks/java/library/j-dcl/index.html ("Double-checked locking and the Singleton pattern - A comprehensive look at this broken programming idiom") –  Steve314 Aug 31 '11 at 19:50
    
Peter Haggar is my god, and I have read that article a thousand times :) thanks for sharing! –  topgun_ivard Aug 31 '11 at 20:05
    
If singelton is the only one they know then you can drop them. This may be the first pattern (anti-pattern) you learn but you should have learned a few more since then. –  Loki Astari Aug 31 '11 at 21:43
    
I was once asked in an interview to describe/explain any common design patterns I was familiar with -- "except for Singleton", the interviewer added with a sigh. –  Ismail Badawi Aug 31 '11 at 22:22
3  
with 5-6 years of development experience, I would expect a person to know about GoF patterns...correct? In my experience, someone with 5-6 years experience talking about development on the internets will certainly know about GoF patterns. 5-6 years actual development experience? Probably not. I've never seen any evidence that GoF pattern theory has made any impact in the average dev shop. –  Carson63000 Aug 31 '11 at 23:47

8 Answers 8

up vote 7 down vote accepted

Yes, it is fair to expect them to know how to write a singleton, in particular if they claim they have used it.

Do they need to code singletons where you work? If the answer is "never", then no, it is not fair.

You should ask them things that you expect them to do in the line of work. Asking theoretical questions that have no bearing on the real work expected from them is in my opinion a bad way to interview someone for their technical fit.

I would ask why they used a singleton and whether it was really needed...

share|improve this answer
    
That part of the interview is taken care by another person, I like to see how well they can write code and explain things which they claim to know! –  topgun_ivard Aug 31 '11 at 20:07
    
@topgun_ivard - Well... what exactly are you trying to evaluate then? –  Oded Aug 31 '11 at 20:14
    
how well they can code and explain their thoughts and views... –  topgun_ivard Aug 31 '11 at 21:04
    
I agree. I have been a developer for about 20 years and have used many different patterns, but I don't know their names. Hell I've even written my own Javascript MVC lib before it became mainstream! If I read about them I know immediately where and when I used some pattern, but didn't really know the name of it. Good developers know how to do stuff not to name them correctly. Singleton pattern is so common (as are Factory, Builder, etc) that everyone knows what they are. –  Robert Koritnik Sep 1 '11 at 21:46

Well, you've gotten a bunch of answers from folks who don't think design patterns are all that useful. I, on the other hand, find that patterns are in fact very useful as short hand for solutions. If you can simply indicate that you want a visitor here or a strategy there, you've expedited your communication in a big way.

That said, the singleton question is a poor one--simply because it's become the standard design pattern interview question and is all over the web as such. Someone who can answer the question hasn't proved much (although if they can't, they're unprepared to say the least). I'd rather ask about a USEFUL pattern.

share|improve this answer
    
I would love to ask about useful patterns if only they tell me they know about any!! I guess singleton is the easiest to talk about in an interview...I am waiting for someone to say strategy, facade, abstract factory or even MVC! –  topgun_ivard Sep 6 '11 at 17:56

While there is a religious war over whether using a singleton is appropriate or not, if you were to assume that using a singleton is acceptable then I still disagree with the premise of writing a "thread-safe" singleton instance method as being necessary. Which by your post seems to be your gauge for if a person knows how to implement one correctly or not.

I am having a very hard time thinking of a situation where a singleton is used that it isn't created at application startup and it doesn't remain the same instance throughout the life of the application. In that case there is no need for thread safety in retrieving the instance. So all you are doing by creating a thread-safe singleton is needlessly using up processing cycles. I'm sure there are some special cases where it is appropriate, but for the other 99.99% of the time people use singletons, there is no need for thread safety in retrieving the instance. Now making method calls on the singleton instance is another topic entirely. Thus, I don't see the relevance in your asking your thread-safe singleton questions unless those people read the articles that you have read or you are trying to assess the person's understanding of multithreading/processing issues in general.

share|improve this answer

I would say no. I don't really know anything about implementing the Singleton pattern, because it's a pattern that I would never, ever implement. Would you also ask me to implement goto error handling? Or how about manually implementing multiple inheritance, or exceptions? You're talking about the implementation of something that should never be implemented (in the average programming job, of course- you'd have to implement exceptions if you're writing a compiler). Therefore, you're only talking about experience doing bad things. I'm not ashamed to admit it- I wouldn't want to work for an employer who wants me to have experience doing bad things. You should ask about implementing patterns that add value to the code base, not remove value from the code base.

On top of that, a candidate's ability to implement various patterns isn't really that helpful. You can Google double-checked locking. Even if you can't, when you find out there's a problem, you can just go back and re-implement it. You can never design it back out because the design is fundamentally wrong. The important skills are whether or not your candidate knows that Singletons are bad, and when to use a Factory, or any other pattern.

share|improve this answer
1  
Singletons aren't necessarily a bad thing if you thoroughly understand them and dangers in using them. –  Demian Brecht Aug 31 '11 at 21:11
    
@DeadMG: I feel that patterns are useful stuff to know. I ask candidates about what patterns they know, and if they come up with Singleton as their first answer and only answer sometimes, than I am forced to make them understand the pitfalls in it! I don't judge a candidate by how well they can write a program for singleton, but I look for how well they can write A program and tell me what is wrong with it...fair argument? –  topgun_ivard Sep 6 '11 at 18:00

Yes, Absolutely...

...if your company manufactures singletons!

Otherwise... focusing on a single-point concept in an interview is unlikely to reveal much about the capabilities of the applicant.

If you're interviewing for an intermediate position, are design patterns even relevant to the scope of work? Is your code base riddled with design patterns? Are you trying to find out if the applicant has... read a book? Or applied the techniques?

Why not focus on iterators instead? Or lambda expressions? Or the HAVING clause in SQL?

Frankly if I was in an interview where the interviewer seemed to be obsessed with singletons, I would be afraid, very afraid to work there.

[Don't be lazy, conduct a proper technical interview!]

share|improve this answer
    
The state of the industry is so bad that most probably couldn't describe what the HAVING clause does. We were interviewing for database specialists and only one of the candidates had a clue as to what a correlated subquery was and he had no idea that they often cause performance issues. And none the candidates knew that "from table1 t1 left join table2 t2 on t1.t1id = t2.t1id where t2.myfield = 'test'" would change the join to an inner join. –  HLGEM Aug 31 '11 at 20:09
    
my company does not manufacture singletons, but you see, I ask this question since the first thing they say when I ask about Design Patterns is Singleton –  topgun_ivard Aug 31 '11 at 21:05
2  
@topgun_ivard: do you actually use design patterns in development or is this an academic question? if the former, ask about the patterns that are important; if the latter, ask something else that's more relevant 8D –  Steven A. Lowe Aug 31 '11 at 23:06
    
Yes, we do try to use design patterns wherever possible...and I end up asking about Singleton because that is the only one which most say they know!! –  topgun_ivard Sep 1 '11 at 0:21
    
@topgun_ivard: but do you actually use singletons? if not, when they mention it, ask them instead about the ones you really care about –  Steven A. Lowe Sep 1 '11 at 16:47

If they bring up the concept, then I say that it is definitely fair game. You want someone who is honest about what they can actually get done. At the same time, being able to write code that follows the singleton pattern specifically should not be a requirement for every position. Singleton is not even used that often in an appropriate context.

One of my favorite question is to ask whether they have used any kind of patterns...

I am assuming you mean GoF patterns specifically. Personally, I care a lot less about whether or not someone knows the details of a specific pattern, and more about if they can solve a problem on the fly that does not readily fit into any sort of cookie-cutter pattern. It does not matter if they can solve a problem that already has a clear solution that they happen to remember, which is the basic nature of these sorts of questions.

Bottom line: Should someone with that much experience be able to explain and implement a singleton? Yes. Does it actually prove that the person is a good fit? No. I have seen too people who can talk your ear off about the abstract factory pattern, but cannot seem to churn out some basic CRUD code to put any value on this metric.

share|improve this answer
    
Unfortunately looks like Singleton is the most popular pattern which comes to the mind of everyone I ask. My aim in asking about patterns is to know how they can apply it to solve problems or how and why they would use it...fair argument? –  topgun_ivard Aug 31 '11 at 22:10
    
I only regret that I have but one upvote to give. Programming is so much more than just "let's see which collection of GoF-blessed Patterns(tm) I can shoehorn this problem into". –  Dave Sherohman Sep 1 '11 at 8:56
1  
@topgun- fair enough, but finding out that someone can solve an already solved problem, because they read "Programming Interviews Revealed" does not really tell you much about the person. It shows they have good memorization skills, which is ok I guess, but its not exactly what makes a stellar programmer. Instead look for a passion to solve problems, a history of solving problems, and an explanation of how they have solved a couple especially tough problems. –  Morgan Herlocker Sep 1 '11 at 13:13
    
@ironcode: I always follow up with some puzzles and few more theory aspects of Java. I totally understand that a person can be good at few things and not so good at few others –  topgun_ivard Sep 6 '11 at 18:02

In my opinion, the ability to write a good implementation of a singleton is not something you can reasonably expect from a 5-6 year programmer. The ability to write a singleton that is reasonably good is what I expect.

Also, if I asked somebody to demonstrate a singleton pattern and they said something like "I don't implement singletons, instead I use a dependancy injection framework so I don't worry about stuff like that" or "I have not written a singleton in years (if ever). I'd do a google search before attempting it." I would score them as passing on the question.

I suspect that you would be better off asking your candidates to write a fizzbuzz solution. If they failed that, end the interview and move on to the next candidate.

share|improve this answer
1  
If you asked me to write something as silly as fizzbuzz, I would end the interview myself. –  DOK Aug 31 '11 at 19:05
    
I was asked to write fizzbuzz once at an organization that I would never expect to ask such a question. I offered to show them my post on a Stack Exchange site (SO, perhaps) where I corrected a solution, posted my own, and was well upvoted. I turned that job offer down for other reasons, but I probably wouldn't accept a position at a company that uses fizzbuzz to screen candidates, especially graduates of respected software engineering programs with 2 years of professional experience and over 6 calendar years of programming experience across half a dozen languages. –  Thomas Owens Aug 31 '11 at 19:17
2  
I was recently asked to write a fizzbuzz solution ...along with about 3 other algorithms including Fibonacci. I think it is a reasonable question to determine someone's thinking processes - then move to something harder. –  IAbstract Aug 31 '11 at 19:17
2  
@IAbstract If someone is being hired as a developer and they get to a face-to-face interview, they should have already somehow demonstrated their capabilities as a developer. For an intern, maybe fizzbuzz would be acceptable in an interview. If I was in a face-to-face and was asked to write fizzbuzz, I would question the entire hiring process used by the organization. –  Thomas Owens Aug 31 '11 at 19:26
1  
I agree that FizzBuzz is not a strong discriminator, but I believe that it is a good first discriminator. If somebody fails at writing a FizzBuzz, then there is no reason to continue the interview (for a programmer). Also, if I ask you (as the first question) to write a FizzBuzz and you end the interview, then I believe it was a successful question; you don't want the job. –  DwB Aug 31 '11 at 21:36

Would the candidate get extra points for pointing out that the use of singletons is controversial? Here is one of many articles on the subject.

share|improve this answer
    
Good point @DOK. +1. Sometimes the answer is a question, which in turn addresses a doubtful issue of the question. :) –  Saeed Neamati Aug 31 '11 at 19:08
    
yes, I look forward for them to tell me that it is hard to maintain them, test them and various other issues...but the fact that it is the simplest pattern helps me understand how well verse they are with programming –  topgun_ivard Aug 31 '11 at 20:04
    
Without reading the linked articles, I would also look for an experienced engineer to know that singletons are horrible if they are stateful in any way, which can cause horrendous problems if you application is multithreaded. –  Demian Brecht Aug 31 '11 at 21:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.