Asking the right questions while interviewing developers

Just another fizzbuzz interview question

I really hate interviews regardless on which side of the table I am sitting during them(Ok, it is a bit easier when you interview candidates 🙂 )

One of the main reasons why I hate them is the stupidity of the programming trivia and questions being usually asked: all sorts of binary tree related traversals, converting numbers from one base to another, number sequences  etc… I mean, who really cares about that stuff in everyday’s work?? Am I applying for mathematician job or for a developer one?
I have a feeling those questions are created to fresh grads because they don’t have much real world experiences but that’s just me guessing..,

But, not all of the interview questions are stupid and the most famous example of what I find to be a good type of interview question is famous “fizzbuzz problem” described by Jeff Atwood and Scott Hanselman which is an example of very simple task which resolution is purely related on persons real-world like thinking skills and not related at all to any other type of knowledge.

Couple of years ago I’ve stumbled on a video recording of a session made by Brad Adams regarding his excellent Framework Design Guidelines book where he mentioned an example which looked so trivial to me that I quickly discarded it as very obvious to literally everyone. (I couldn’t find that link for this blog post :()

Couple of months after that I was interviewing a candidate for one senior position and due to the fact he gave fuzzy answers making me wondering if he gets heap and stack so I took Brad’s example idea and came out with a simple question looking something like this

namespace InterviewTrivia
{
	public class ClassA
	{
		public override string ToString()
		{
			return "Hello from class A";
		}
	}

	public class ClassB : ClassA
	{
		public override string ToString()
		{
			return "Hello from class B";
		}
	}

	public class ClassC : ClassB
	{
		public override string ToString()
		{
			return "Hello from class C";
		}
	}
}

Nothing fancy here just 3 classes inheriting from each other and each one overriding object ToString() method

Now the console application Program class can be done something like this:

using System;
namespace InterviewTrivia
{
	class Program
	{
		static void Main(string[] args)
		{
			ClassA first = new ClassC();
			ClassC second = new ClassC();
			ClassB third = (ClassB)second;
			ClassA fourth = (ClassA)third;
			object fifth = (object)fourth;

			Console.WriteLine("1: " + first.ToString());
			Console.WriteLine("2: " + second.ToString());
			Console.WriteLine("3: " + third.ToString());
			Console.WriteLine("4: " + fourth.ToString());
			Console.WriteLine("5: " + fifth.ToString());
			Console.ReadLine();
		}
	}
}

As you can see there, first 5 lines are doing different variants of casting class c instance and then printing out the result of ToString() method.

I wrote this sample on a whiteboard (to avoid having R# helping him with the answer) and ask the candidate what would be the resulting output of an app.

Why is this fizzbuzz question?

Because it is:

  • very simple problem and code sample
  • focused on concrete programming scenario pinpointing the (IMHO) important OOP concepts which need to be understood by senior developers
  • in order to solve it one doesn’t need any knowledge other then programming skills.
  • it is white board friendly – very important attribute for DEV interviews

So, what happened?

To my surprise that candidate bluntly failed the test reporting as a result:

  1. “Hello from ClassA”
  2. “Hello from ClassC”
  3. “Hello from ClassB”
  4. “Hello from ClassA”
  5. “InterviewTrivia.ClassA”

I was done with the interview convinced this candidate was exceptionally bad and I’ve stayed convinced in that until accidentally I’ve run the same sample with couple of other developers and in most cases got the same answer as the candidate gave. That fact is maybe just the result that we .NET developers are really spoiled and allowed to be blissfully ignorant about how stack and heap in .NET works (which is btw the thing I strongly disagree), but that is not the point of this blog post.

The point is that with properly chosen trivia and programming questions we are using in interviews we can avoid loosing good candidates just because they don’t know for sure how to convert –1234.56 to hexadecimal base number (even on paper) and focus on getting information on really important attributes a new member of the team would be bringing to the team (or not) once joining the company.

(Sample code of today’s post can be found here)

Technorati Tags: ,,

Share this post :

Posted on January 8, 2010, in Uncategorized. Bookmark the permalink. 14 Comments.

  1. You have an error (copy paste artifact?), line 20 should say "fifth".

  2. Sergey, corrected!
    Thanks for the tip

  3. I believe this has more to do with issues around virtual than stack/heap.
    Since methods are not virtual by default in .Net, I'm not surprised to see developers answering as if ToString were not virtual.  For that reason I think a hint would be in order just to jog memories.
    Still, thanks for this question, I'll be using it with my candidates.

  4. Jason,
    I agree that this can be answered from HOW c# point of view (understending how virtual works) or from WHY stackheap point (realizing that all we do is creating multiple pointers on stack pointing to the same heap object) and that is also a nice extra info to get from candidate regarding how involved he is in achieving crafstaman level 🙂

  5. I am not a programmer, just out of curiosity, what would be the result?

  6. Actually nor I, developer with more than 20 years experience cannot easily answer, because actually it doesn't matter. (Or we have to differentiate programmers and developers?)
    Because most of mine time is architecture debates, selecting right solution and configuring. To see what will happens when someone overload some method – it is not actual for current development process at all.
    For mine personal opinion most important is capability of candidate to choose good solution that can be implemented in specified time and as related process candidate's experience.

  7. Andrea,
    The result would be 5 times "Hello from class C" because there are two instances of class C on the heap and 5 stack pointers of different type to that two instances.
    In other words, it is the heap type which matters not the type of pointer on stack.
    Pavel,
    I understand what are you saying – I’ve been doing the high level stuff myself for years but that sounds to me more like an architect then like a developer area of responsibilitty and I agree there are different sets of questions to ask in that context (I guess, what I call a developer is what you call programmer I guess).
    As II said this question is (IMHO) of fizzbuzz type so you can ask it during the short phone screening before real interview etc…
    IMHO, even in managed world understanding of principles of memory allocations, garbage collector mechanism etc is essential for senior+ developersprogramers because sooner or later would be required in debuging memory leaks, tuning up performance etc…
    I might be wrong in expecting this , but as I said – just my oppinion 🙂

  8. I don’t see how this sample is linked to heap and stack. It’s more on how virtual methods are handled. Or maybe you mean the value type and reference types? All types are classes (ref type).

  9. Just saw your answer regarding heap/stack. I don’t think you got it right. It does not matter where the instance is stored, heap or stack. It’s the virtual method behavior. Try another method without “virtual” keyword in the root class’ method definition, you’ll be surprised (don’t try overriding the ones from object, they are all marked with virtual). If it is not a virtual method (unfortunately by default in .NET, the opposite in Java), your candidate’s answers are most correct.

    • Like with the most things in IT, “there are many ways to skin a cat”.

      First to answer you on how it is related to stack & heap..
      My dad tought me that the rule #1 of defensive in footbal is to keep an eye on the ball and ignore the “dancing legs” of an attacker. In this sample the ball is a single allocation on heap occuring in the line #1. All of the casts, different types of pointers etc are just leg dancing which doesn’t change the fact that there is just a SINGLE memory allocation and thus the question becomes very easy to be answered.
      (For noobies like me interested in more details, I did a blog post about this subject 3 years ago http://blog.vuscode.com/malovicn/archive/2008/01/13/net-foundations-memory-model-part-2.aspx)

      Now the second part…
      Yes, you can look at this problem also from a language perspective and still get the right answer, but IMHO reasoning from that angle on HOWs disregarding the WHYs is much harder and not allways applicable with an ease.

      Check out the sample from C# spec bellow. I invite you to wake a shoot at it from your language based perspective and then to compare your solutions with the program output once typed in. If you would solve it, you and your approach trully rock:)

      Anyhow, thank you for your comments!

      class A
      {
      public virtual void F() { Console.WriteLine(“A.F”); }
      }
      class B: A
      {
      public override void F() { Console.WriteLine(“B.F”); }
      }
      class C: B
      {
      new public virtual void F() { Console.WriteLine(“C.F”); }
      }
      class D: C
      {
      public override void F() { Console.WriteLine(“D.F”); }
      }
      class Test
      {
      static void Main() {
      D d = new D();
      A a = d;
      B b = d;
      C c = d;
      a.F();
      b.F();
      c.F();
      d.F();
      }
      }

      • Marcel Doru Popescu

        First, there are actually two allocations, not one:

        ClassA first = new ClassC();
        ClassC second = new ClassC();

        (The “new” keyword gives it away :P)

        Second, you think binary tree traversals are useless, but knowing how a specific compiler on a specific platform organizes memory is not? Wow.

        By the way, I grew up programming on mainframes, then I moved to minis and “micros”. It was years before I saw my first 8080. I still know how to convert (mentally) hex to decimal and viceversa. If I went by the same rule (as in, what I know is important, what I don’t is not), I’d say that base conversion is absolutely vital.

        In any case. While I agree with the example – a programmer in an OO language should be familiar with basic OO rules – that is because I *dis*agree that the example has anything to do with the way memory is organized in .NET.

      • Marcel,
        off course you are right – I was responding without reading the post I wrote a 1+ year ago based on what I remembered it was on sample. To be honest, I haven’t been reading comments on a blog for a long time at all, but migrating from commerce server to wrod press got me back in so I am still getting used 🙂

        Anyhow, 1 or 2 allocations – it doesn’t change my point that someone like me who has basic understanding of memory model can *easy* answer this type of questions. I dare to say that the level of knowledge required for that is far away from any specifics on how compiler organizes memory – just basic stuff heap gets allocations, stack gets pointers to that allocations – that’s all. Spending an hour with http://www.amazon.com/CLR-via-Dev-Pro-Jeffrey-Richter/dp/0735627045/ref=dp_ob_title_bk/184-4591057-3148339 was more then enough for everyone I know to really grok those things. Really, there is nothing advanced at all in it.

        The main reason why I think binary trees are useless exercise is that *in the real world* they are not something web developers would be implementing (in my personal case: never in 13 years of being payed for coding and counting). They are w
        In that time I was sustaining couple of serious issues (memory leaks, threading issues etc) caused by folks not having basic understanding on stack & heap and thus comes my opinion regarding binary trees, stack and heap…

        To be fair here I have to say that binary trees are not always *useless* in interviews, it all depends on on hiring policy a company has. If the hiring policy is focused toward recent graduates then off course you have to ask them to spit out the things they memorize in university. If the hiring policy is focused on senior folks then IMHO a combination of more pragmatic questions is maybe more appropriate.

        Thanks a lot for your comment!

      • Marcel Doru Popescu

        I had to implement a binary search at some point, a few years ago – Delphi 5 didn’t have a dictionary. However, it was just one time, so in general I agree – though I believe some of my colleague’s total lack of theoretical knowledge is an impediment, I couldn’t indicate why.

        As for memory – yes, a basic understanding is necessary. I was objecting to the level of detail in your other article 🙂

      • Me too. I DISagree the example has anything to do with the way memory is used. It’s how the compiler handle the virtual methods.
        The default .net compiler will put (in most cases, but not always. where the int in a class put?) value types in heap, and ref type in stack. But no one prevents you from writing a compiler which does not have even concept for stack and heap. But if you want it to be C# compatible compiler, it must behave the way when it meets virtual methods.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: