Professional C__ - Marc Gregoire [501]
Types of Questions
Interviewers will be on the lookout to see how you report and handle errors. When you are asked to write a piece of code, make sure you implement proper error handling.
You might be asked to give a high-level overview of how stack unwinding works when an exception is thrown, without implementation details.
Of course, not all programmers understand or appreciate exceptions. Some may even have a completely unfounded bias against them for performance reasons. If the interviewer asks you to do something without exceptions, you’ll have to revert to traditional nullptr checks and error codes. That would be a good time to demonstrate your knowledge of nothrow new.
An interviewer can also ask questions in the form of “Would you use this?” One example question could be “Would you use setjmp()/longjmp() in C++, since they are more efficient than exceptions?” Your answer should be a big no, because setjmp()/longjmp() cannot possibly work in C++ because they bypass scoped destructors, and the fact that exceptions have a big performance penalty is a misconception. On modern compilers, exceptions have close to zero cost.
CHAPTERS 11, 12, 13 AND 17: THE STANDARD TEMPLATE LIBRARY
As you’ve seen, certain aspects of the STL can be difficult to work with. Few interviewers would expect you to recite the details of STL classes unless you claim to be an STL expert. If you know that the job you’re interviewing for makes heavy use of the STL, you might want to write some STL code the day before to refresh your memory. Otherwise, recalling the high-level design of the STL and its basic usage should suffice.
Things to Remember
The different types of containers and their relationships with iterators
Basic usage of vector, which is probably the most frequently used STL class
Usage of associative containers, such as map
Usage of C++11 unordered associative containers, such as unordered_map, and the differences with associative containers
The purpose of STL algorithms and some of the built-in algorithms
The ways in which you can extend the STL (details are most likely unnecessary)
Usage of C++11 lambda expressions in combination with STL algorithms
The remove-erase-idiom
Your own opinions about the STL
Types of Questions
If interviewers are dead set on asking detailed STL questions, there really are no bounds to the types of questions they could ask. If you’re feeling uncertain about syntax though, you should state the obvious during the interview — “In real life, of course, I’d look that up in Professional C++, but I’m pretty sure it works like this . . .” At least that way the interviewer is reminded that he or she should forgive the details as long as you get the basic idea right.
High-level questions about the STL are often used to gauge how much you’ve used the STL without making you recall all the details. For example, casual users of the STL are familiar with associative and non-associative containers. A slightly more advanced user would be able to define an iterator and describe how iterators work with containers. An even more advanced user should be familiar with the remove-erase-idiom and should be able to explain in which cases you can use it and what its benefits are. An interviewer might also gauge your knowledge about lambda expressions which are new in C++11. Other high-level questions could ask you about your experience with STL algorithms or whether you’ve customized the STL.
CHAPTER 14: USING STRINGS AND REGULAR EXPRESSIONS
Strings are very important, in every kind of application. An interviewer will most likely ask at least one question related to string handling in C++.
Things to Remember
The std::string class
Differences between the C++ std::string class and C-style strings, including why C-style strings should be avoided
Conversion of strings to numeric types like integers and floating point numbers, and vice versa
C++11 raw string literals
The importance of localization
Ideas behind Unicode
The concepts of locales and facets
What regular expressions