12 ms·
C++ Programming Questions to Ask on Interview (2017)
- mnemotronic 8y agoWow. Haven't gone near C++ for 12+ years. It done got weird.
- r_c_a_d 8y agoThat's exactly what I thought. Not used it professionally for a while (and then older compilers) and vaguely knew there were a lot of changes around memory management. But was it that hard before? Constructors, destructors and operators were fine... weren't they?
- rafiki6 8y agoThis is why Go and Rust are coming into their own. C++ is a clusterf*ck of complicated design decisions (note, I didn't say bad, just complicated). It's certainly a powerful language for systems development but one has to wonder if it needs to be. Anytime I see a language overdo it on the features, my sense is that they need to step back and survey the landscape of developers before they decide to add more stuff.
- slavik81 8y ago> cannot return 0 from a void [main] function "A conforming implementation may provide more versions of main(), but they must all have return type int." ~ Bjarne Stroustrup - http://www.stroustrup.com/bs_faq2.html#void-main http://www.stroustrup.com/bs_faq2.html#void-main
- ajross 8y agoSome of this is straightforward. The rest.... eesh. I mean, yeah, I guess you can quiz your candidates on their knowledge of stuff like enable_if and ability to write templates based on traits like is_integer, but don't expect that to be a good filter for anything but language minutiae. And question 10... what on earth is a "max heap". I'm gathering from context that they are asking about a heap in the sense of the packed tree data structure. I've used those, and written those on multiple occasions, so it's not an impractical subject. But (1) if you asked me the question the way it's phrased here I'd look at you blankly and wonder if you were talking about some kind of custom allocator, and (2) they don't even want the candidate to talk about the data structure! They just want a class definition written for the thing with a handful of obvious interfaces (add, get maximum, y'know... it's just a heap). How is that useful to anyone?
- flafla2 8y agoA "max heap" is a heap that has O(1) access of the maximum priority element, as opposed to a "min heap" which returns the lowest priority. It's largely a semantic distinction, as a max heap = a min heap with negative keys. Also a heap isn't just a packed tree, it's referring specifically to a heap data structure [1] (which is often implemented using a packed tree, but not necessarily). This is very common terminology, and honestly I'd expect any CS grad past an sophomore undergraduate level to understand exactly what a max heap is, especially in an interview setting. [1] https://en.wikipedia.org/wiki/Heap_(data_structure) https://en.wikipedia.org/wiki/Heap_(data_structure)
- ajross 8y ago> I'd expect any CS grad past an sophomore undergraduate level to understand exactly what a max heap is Never once heard the term, and I've written several heap-based priority queues over the course of more than two decades of professional work. So... yeah, I'm too dumb for that filter I guess.
- banachtarski 8y agoSurely you've heard the distinction between the max-heap and a min-heap before... And honestly, I'm sure if you asked what it was in an interview, you would get a straight answer and not be dinged for it. I would filter based on your saltiness level though.
- dev_dull 8y agoSounds like their would be mutual filtering.
- gcb0 8y agosounds like all those people are filtering for is who went to the same university, at around the same time as they did.
- 8y ago
- edflsafoiewq 8y agoI like that each question has a purpose. It's an interesting problem to determine a (small) set of questions that "cover" the (large) space of C++ knowledge in the sense that if someone can answer them all, they likely have a strong knowledge of the language. > The interview candidate might think that returning a noncopiable object from a function is also a compiler error, but in this case it’s allowed, thanks to copy elision. You can ask the candidate under what conditions copy elision is performed. This isn't copy elision. Copy elision isn't even permitted here because the returned expression is the name of a function parameter. The two-stage overload resolution that tries a move constructor first is defined in the same section of the spec, [class.copy.elision], as copy elision but the rules are different and it is always performed, even if the implementation doesn't do copy elision.
- crb002 8y agoGood way to weed out bad employeers who treat programmers like janitors with no hope of career advancement.
- liquidify 8y agoWhy is checking for self assignment necessary?
- banachtarski 8y agoThere are pathological cases where due to layers of abstraction (in a data structure say), an element gets moved to itself. The algorithm is unaware of this but it happens anyways. If you fail to check for self assignment, you will clear the "other" owning pointer/resource and this will result in a leak, as well as resulting in an ill-formed object.
- olliej 8y agoMy reading of their moving assignment operator is that it is wrong, or at the very least unclear. Basically they do std::swap on query field. In that case you are relying on the destructor of the moved object performing the delete (if necessary). I would argue that that falls into the “too clever” spectrum, and also kind of assumes that no one is going to refactor the destructor at some point in such a way that some other property will result in them incorrectly thing that the query is null, and so leaking it. Having now read through this, I agree with others assessment - these questions are questionable at best. What is it they’re proven to do?
- banachtarski 8y agoI think relying on the destructor of the moved-from object running feels idiomatic to me. That's what the destructor is there for after all.
- olliej 8y agoThe destructor deleting its /own/ data. But here we are moving a bunch of the fields (and so clearing them) but swapping one of them. That seems like bad coding, and doesn’t match any idiom I’ve seen :-/
- jcelerier 8y agofor vectors it makes std::move a very fast operation
- AstralStorm 8y agoVector has a good move assignment which also works. Why swap when you can move? Swap is additionally risky because someone can ADL specialize it for their own purposes. (which is the point) And suddenly your move assignment is broken. So people work around it by adding using std::swap clause there... And then you're calling all those destructors.
- olliej 8y ago
- cjensen 8y agoSo in the first question, they test to make sure the candidate knows the minutia that std::unique_ptr<> has no copy constructor. And in the followup, they assume the candidate will just assume that "SearchQuery", whose header is not shown, will have a copy constructor. Just no.
- slavik81 8y agoI looked at that first one and didn't realize it wouldn't compile simply because I would never write something like that anyways. That function should just take an int&. However, understanding that SearchQuery is broken is pretty important. Breaking the Rule-of-3 is probably the #1 mistake made by junior C++ developers trying to write object-oriented code, and the consequences are pretty nasty. You'll get corruption and crashes from use-after-free and double-free errors and nothing will make sense because the problem will move around as you create temporary copies while trying to debug. That being said, I wasn't familiar with that sort of stuff when I was first hired as a C++ developer. I was very fortunate to start my career with a company that was willing to hire people and train them.
- AstralStorm 8y agoIt is usually called rule of five, 3 is half of it and makes people sometimes forget to write move assignment. I'm pretty sure business do not like to actually train people nowadays. :)
- slavik81 8y agoI originally wrote "Rule-of-Three / Rule-of-Five / Rule-of-Zero" but regrettably, I cut it for brevity.
- jemfinch 8y agoBy volume of content it's "usually" called the rule of 3, because the rule of 5 didn't exist until C++11, and most of the C++ content was written before moving was a thing. It's now called the rule of 5.
- 8y ago
- deleted 8y ago[deleted]
- rationalfaith 8y agoVery out of touch. Slew of codebases not relying on std libs. Oh well, another company promoting whiteboard interview mental masturbation type questions to make a quick buck.
- jeorgun 8y agoThese questions seem pretty good! In the max heap question, I'd argue that add should take a parameter by value, rather than a const reference, since this prevents having to needlessly copy temporaries (and is less verbose than overloading for T&&). List of iterator categories is missing input iterators.
- lordnacho 8y agoI write c++ high frequency trading systems, but I think I'd have to look up the answer to most of these. I've heard of all of them, though, and I know roughly what the point of them is. Your problem when interviewing someone is that in real life, you are relying on the person being quick to learn, since they are unlikely to have the answer in cache. Maybe if they'd just read Stroustrup they'd know some of the specifics, but most coders would know what the point was and google it. You have to wonder that a fair question is. I supposed you should know of a bunch of things about c++ (why you're using it, major features like templates), without knowing all the specifics (SFINAE/CRTP examples). For instance it's fair to ask someone what a memory barrier is. But not to list all the varieties (relaxed, acquire, etc). I've hired a number of c++ devs in the last few months, and all of them have turned out fine. I never asked anything particularly specific like this list of questions, just poked around the very large house that is c++. What does the candidate think changed in c++11? Why are we using it for HFT? What kind of thing will slow us down? I think if I'd asked these questions I'd have dropped a lot of perfectly fine candidates.
- Uberphallus 8y agoRegarding HFT, how much is C++ and how much is FPGA? I always thought even CPUs often turn out too slow for that.
- steveklabnik 8y agoThere’s HFT systems in Java, and OCaml. I’m not an expert in these domains but since markets are only open for certain hours of the day, you can pre-allocate enough memory and just not collect till after the bell rings. In my understanding.
- jki275 8y agoI'm glad to see this as I'm interviewing for c++ dev position after working with it non-commercially for years and I didn't even recognize a couple of the questions and wouldn't have any idea how to answer several of them off the top of my head. Certainly I could look any of them up and implement them if necessary, I really rarely code without having a book open next to me to look up the finer details of things I haven't memorized yet.
- BrissyCoder 8y agoI wrote C++ for relatively complex embedded guidance/positional apps for 5 years. I think I did it reasonably well - definitely better than 80% of my colleagues. Since then (10 years or so) I've been C# almost exclusively. This makes me hope I never have a need to go near that language ever again.
- Const-me 8y agoOK I'll bite. 1 - knowing this is 100% useless information, in reality I have a compiler to tell me that. 2 - Seriously? new/delete in 2018? In very rare cases when I need a raw pointer I call unique_ptr::release(). 5 The question is so-so but the answer's wrong. Iterators don't need to point anywhere. You can make an iterator generating values instead of referencing container. Not necessarily a good idea but still. 6 Same. Just because it's not specified in C++ standard doesn't mean it's bad, or should be avoided. E.g. const uint32_t n = 11; printf( "Eleven: %d\n", n ); is UB but the code is 100% fine to my standards. Stuff like OpenMP or intrinsics is not specified either, does it mean we should avoid them? 8-9 IMO template metaprogramming should be avoided. If you want your template data structure to behave differently for pointers and non-pointers, expose another template argument for that, there's too many corner cases like handles (technically pointers, logically values) and smart pointers (the opposite). 10 I don't think the sorted vector listed in "possible answer" is a max heap. Conclusion: if the OP claims the questions are "Proven", I'm curious about provement methodology.
- gpderetta 8y ago> 1 - knowing this is 100% useless information, in reality I have a compiler to tell me that. A clever compiler might tell you that adding std::move will make the code compile. But if you do not understand what std::move does it will lead to more bugs as you might not expect the ptr to suddenly be empty in main.
- Const-me 8y agoI agree with what you're saying but they aren't asking what it does. They're asking "What happens when?" giving code that doesn't compile. Nothing happens.
- MaxBarraclough 8y ago> is UB but the code is 100% fine to my standards Strongly disagree - I'd never let that through code-review. When living in C/C++ land, we should be extremely aware of the differences between the integer types, and handle their conversions carefully and explicitly. Given that the "%d" format-specifier maps to `int`, not to `int32_t` (let alone `uint32_t`), that code really could wreck things if your platform has an `int` type which isn't 32 bit - https://stackoverflow.com/a/3168293/ https://stackoverflow.com/a/3168293/
- reacweb 8y agoI remember a time where mastering Stroustrup and the two Scoot Meyer was enough to be considered an expert. Even as a Perl lover, the level of intricacy of modern C++ makes me very reluctant to choose it on a new project.
- partycoder 8y agoWriting safe, idiomatic C++ is hard. Most C++ code found in college books is outdated and not idiomatic, driving students away from the language. But it is true that C++ can also be your worst nightmare if you don't have discipline. C++ will turn your bad coding habits into infinite frustration... and that is great. C++ is the proof you really like programming.
- tragomaskhalos 8y agoSlightly off topic, but a candidate applying for a general programming position who claims knowledge of C++ in their CV is presenting an absolute gift for interviewers: because, if they actually do know their C++, then they are likely to be a strong candidate overall; conversely, if they are bullsh1tting you it is very easy to find this out quickly. True story: once interviewed a guy who claimed to know C++; set him a challenge that he couldn't answer, simplified it a bit, still couldn't answer, we iterated down until I discovered he didn't even know how to declare an int variable. Turns out he had been a tester on a system written in C++ ...
- xsr2 8y agoI'm a C# developer and I've been strongly considering learning C++ in detail because I feel like it would make me a better programmer in general. However getting started with such a complex language seems daunting. Anyone know where one should start? C++11? C++14? Something even older?
- jdlyga 8y agoThe way that I learned C++ was starting from how the language was developed. With functions, arrays, basic variables. Then moving onto classes and polymorphism. Then with C++11 and C++14 newer features.
- adamnemecek 8y agoIt doesn’t matter that much. If I were you I would pick a project you care about and read the source.
- MauranKilom 8y agoI very much agree that it helps you understand the "programming stack" much more intimately. If that's what you are after, you'll also have the parts that are relevant to you covered relatively quickly (compared to fully "knowing" C++ which would probably take at least a decade). I also agree with the other reply that the strongest learning experience is basically in the chronology of the language development. It takes running into a case where they are the best solution to really grasp "why include guards?", "why templates?", "why smart pointers?", "why iterators?" and so on, and you also gain a lot of appreciation for what compilers in other languages do for you behind the scenes. I also highly recommend https://godbolt.org/ https://godbolt.org/, which allows you to see what machine code is produced in the end. Seeing all the template magic getting distilled to clean asm is quite amazing.
- alexeiz 8y agoThese questions are quite simple. I wouldn't use them to find good C++ developers. However, it's quite hard to come up with good C++ interview questions. You want to test for fundamental knowledge of C++, for the practice of using C++ for developing code, but avoid impractical cases that nobody remembers in their right mind. Every interviewer has his own favorite C++ trick. The fact that an interviewee doesn't know or use that trick doesn't mean that his C++ skills are inadequate. It means that the interviewer's trick may not be as commonly used as he may think.