8

If I am asked in an interview to write down some code, should I completely stick with C++ or is it acceptable to "mix" the two to write easy code as it comes to my mind?

I'll explain it: if I'm asked to create a "stack" data type, a C++ class is perfectly suited, while if I need to parse some strings a strtok/sscanf is far easier than STL. On the other hand, if I need a generic container STL and templates are the way to go.

Everything can be done in C or in C++, but in an interview I can't code everything or remember every standard/STL function. What is an acceptable tradeoff?

6
  • 1
    Most C++ programmers would say you shouldn't mix in pure C without a really damned good reason (which you better be able to justify).
    – Rig
    Aug 30, 2012 at 19:36
  • 2
    I guess a "I can't remember the STL function to handle the string splitting" isn't great, is it?
    – John Smith
    Aug 30, 2012 at 19:42
  • 14
    That you know enough to even ask this question would be a positive sign in my book if I was the interviewer. Just ask them what they want. Aug 30, 2012 at 19:42
  • 2
    sprintf and FILE can make my life easier at times in C++ world Aug 30, 2012 at 20:10
  • If I was an interviewer I wouldn't be impressed if someone answered this question by saying what function they'd call.
    – James
    Aug 30, 2012 at 21:22

6 Answers 6

13

To be really pedantic, the C++ offers supports for C, natively, maybe only a subset, but if the topic is the C++ the C programming language is included, and this is not an opinion, i would expect no arguments on that in a professional environment.

1
  • Agreed. What's more, C is kinda part of C++ so why not use it to benefit? Sticking purely to one part of the language and not touching the other is not a good use of the language, in my opinion.
    – Pijusn
    Aug 31, 2012 at 5:17
17

Unless specified, most programming questions will let you use whatever language you want when answering a specific question. You may want to explain which language you are using to answer the specific question to avoid confusion and explain why you are using it.

If you are not sure if this will be OK with the interviewer, ask - it is absolutely acceptable to ask if you can answer different questions using different languages (in most on-paper answers, pseudo-code will be more than acceptable).

3
  • 4
    Yes, that's what I've been thinking: pseudo-code. I don't have a knowledge base in my mind and I tend to memorize techniques and way of using stuff rather than syntax or API declarations (which may change). If I can pseudo-code it and I have a "decent" amount of programming experience with a language, doesn't matter what it is I think I can code it.
    – John Smith
    Aug 30, 2012 at 19:30
  • 2
    It would be a positive sign in my book if the candidate was easily able to switch between C | C++ | C++ & STL and could explain why they chose a particular path.
    – user53019
    Aug 30, 2012 at 19:32
  • 6
    @JohnPell - In my experience understanding principles and how and when to apply them trumps deep language knowledge every time.
    – Oded
    Aug 30, 2012 at 19:33
10

You should just ask to the interviewer what he prefer, if you know how to use both.

Depending on his answer you can discover:

  1. if he really know the differences between C and C++;
  2. if he knows about C++ standard library;
  3. if it's important to him.

There is no generic answer to this question. It's too dependent on the job and the objectives of the recruiter. If he wants you to show language knowledge then he will be specific. If he want you to show you problem solving skills, and communication skills, then he will not really care.

8

Unless your interviewer explicit request that you code in C or C++, I would just use what ever is appropriate for the interview.

For me, if my candidate was mixing the two up, I would be more interested in his strategy or reason for mixing the two language. Having someone on my team that can provide a solution and be able to justify the solution is extremely valuable.

1
  • 1
    +1, Justifying is the key. I'd say this is the key to being a good developer in general (besides programming skills, of course).
    – K.Steff
    Aug 30, 2012 at 20:08
1

If I am asked in an interview to write down some code, should I completely stick with C++ or is it acceptable to "mix" the two to write easy code as it comes to my mind?

That entirely depends on what kind of job you are applying for and which language they expect you to use. Generally, if you are writing C++, it is best to stick to C++ as far as possible. I would however expect every C++ programmer to know at least a bit of C (and vice versa).

I'll explain it: if I'm asked to create a "stack" data type, a C++ class is perfectly suited, while if I need to parse some strings a strtok/sscanf is far easier than STL. On the other hand, if I need a generic container STL and templates are the way to go.

If they expect you to write a whole working data type during the interview, you should be wary. It is good if they ask you about technical details, but if they expect you to spend hours of writing programs during the interview, that likely says something bad about the employer.

If you use scanf(), you have probably made a big mistake no matter if C or C++. You better explain to the interviewer that you are just writing something quick & dirty and would never use scanf in production code. There are many pit falls with various C library functions.

The interviewer shouldn't expect you to remember every function in the C and C++ standard librarier, but they should expect you to know which ones that are blatantly dangerous. gets(), getchar(), scanf(), setjmp() etc.

1
  • totally agree, it was just to make an example, I would AT LEAST use scanf_s, and if I can I would TOTALLY avoid it in production code
    – John Smith
    Aug 31, 2012 at 13:54
0

I think that when you are actually explicitly asked to do a short assignment in C++, it would be of utmost importance to actually stick with C++ and not mix C with it. Modern C is not a subset of modern C++, as such mixing the two would only imply that you aren't very familiar with what clean, modern C++ is like.

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