In the first cracking the coding interview video Ms. McDowell talks about candidates that begin by writing test cases in whiteboard interviews. How would one go about this?

For example, the derivative problem in the video (the candidate is asked to write a function to take derivatives of a list of "Term" structs, which houses two integers, one for coefficients, and one for exponents), would you create a list of Terms, pass it into derivative(), and write the expected solution before even writing the code?

I'm thinking something along the lines of:

derivative([(10,50),(10,0)]) -> [(500,49), (0,0)]
derivative([(5,2),(10,1),(5,5)]) -> [(10,1),(10,0),(25,4)]


is this what she means by writing test cases prior to writing code?

  • 2
    please don't cross-post: workplace.stackexchange.com/questions/64700/… "Cross-posting is frowned upon as it leads to fragmented answers splattered all over the network..." – gnat Apr 4 '16 at 5:05
  • 2
    I don't think it's possible for us to answer this, since only she knows what she meant, and whether this is a good idea is entirely up to the interiewer and not something we can possibly guess. The only thing I can say about it is that it seems pointless to "write a test" you can't actually run, unless your intent is to "manually run" the tests in your head to confirm you got the code right and you needed to write down the correct numbers in advance for some reason. – Ixrec Apr 4 '16 at 7:31
  • Perhaps it would be easier to understand if you think of those as "examples". For example, if you look at competitive programming, every question comes with a few "sample inputs". This is very different from "test cases", which focus on exposing the corner cases that may be inadequately covered by the code implementation. – rwong Apr 24 '16 at 16:49
  • On the other hand, the ability to identify corner cases is not a "teachable" skill. Some people can do this in the blink of an eye, and we don't really understand why some people can do it while others can't. – rwong Apr 24 '16 at 16:52

Certainly this is how I present test cases in an interview context (though I'm the interviewer in these cases). Doing this ahead of time would be a very positive thing. Identify the likely corner cases, bases cases, etc. and then move on to the design. You could ask the interviewer if they want you to talk more about testing the code before you proceed, but I'd guess they'll likely say "no" (though personally I think it's good to ask).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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