Sign in to follow this  

Typical (technical) interview questions

This topic is 4394 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm going through the interview process right now and I'm trying to get fully prepared. There are a handful of questions that get asked the majority of the time and I figured this thread could be a good resource. I'm interested in technical questions only. Questions and answers would be best (as this is a study resource). But posting questions for the smart people here at gamedev to answer would also be quite helpful. Even just laying out some examples of tricky/obscure topics would be good (describing c++ casting or the meaning of volitile, etc implementation for example). Have at it! Thanks

Share this post


Link to post
Share on other sites
Here's a question that has been asked I'd say rougly 80% of my interviews. My answers are always pretty rough (and I think un-impressive).

Q: What does a constructor do? A destructor?

A: ?

As low level and as much detail as possible please! I understand at a higher level but I've just never had much cause to dig any deeper (such as what memory management, stack/heap, etc stuff is going on under the hood).

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Hmm.. lots of interviews eh? Interesting.

Explain polymorphism.



Share this post


Link to post
Share on other sites
Here's one that has been asked in 98% of my interviews:

Q: What is the dot product and what is it used for? Cross product?

A: The following is my answer, feel free to comment.

Dot Product: This calculation is commonly used to find the measure of the angle between two vectors. Such a calculation requires that the vectors be unit vectors, however, or else the result will be inaccurate. For certain purposes, such as backface culling, the actual dot product is ignored and only its sign is used. The reason for this is because the sign of the dot product has the following significance:
If V . W > 0 then 0 < 90 	^^ == 1	 (same direction)
If V . W = 0 then 0 = 90 ^> == 0 (orthogonal)
If V . W < 0 then 0 > 90 ^v == -1 (opposite directions)

Therefore, the sign of the dot product can tell you which side of the view vector the polygon resides in.
if θ > 90, don’t cull (or reverse the view vector to remain consistent – θ < 90)
The dot product of two vectors is defined as:
X = V . W = V1W1 + V2W2 + V3W3

u • v = |u| |v| cos θ (if u and v are unit vectors, the magnitudes are 1 and can be ignored)
A vector dotted by itself measures the square of its length. |v| = (v • v)


Cross Product: of two vectors is defined as:
X = V x W
= (X1, X2, X3)
= (V2W3 - V3W2, V3W1 - V1W3, V1W2 - V2W1)
= (YVZW – ZVYW), (ZVXW – XVZW), (XVYW – YVXW) [if V = (XV, YV, ZV) and W = (XW, YW, ZW)]

i j k To find i component, (V2 * W3) – (V3 * W2)
V1 V2 V3 To find j component, (V3 * W1) – (V1 * W3)
W1 W2 W3 To find k component, (V1 * W2) – (V2 * W1)

This cross product can be used to find the normal of a polygon in 3D. The normal of any surface is a vector perpendicular to the surface. The normal can be calculated by taking the cross product of two vectors (V1 and V2), which are defined from three (non-collinear) vertices of the polygon.
Because the normal is exactly perpendicular to the polygon, it can be used in such processes as light source shading and backface culling. However, certain processes (such as light source shading) require that the polygon normal be a unit vector, in which case you must normalize the polygon normal after the cross product calculation.

The magnitude of the cross product is area of the parallelogram between the two vectors.
Area = | u | | v | sin θ

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
Hmm.. lots of interviews eh? Interesting.

Explain polymorphism.


I'm a professional when it comes to being laid off. :( I'm quite used to the interview process although I still never seem to get any better at it. I'm in the job hunt at least once a year -- sometimes more.

I have a review sheet I use so I'm just copying off. I've been asked about polymorphism pretty often as well so here is that section hehe:


VIRTUAL:
A virtual function allows derived classes to replace the implementation provided by the base class. The compiler makes sure the replacement is always called whenever the object in question is actually of the derived class, even if the object is accessed by a base pointer rather than a derived pointer. This allows algorithms in the base class to be replaced in the derived class, even if users don't know about the derived class.

The derived class can either fully replace ("override") the base class member function, or the derived class can partially replace ("augment") the base class member function. The latter is accomplished by having the derived class member function call the base class member function, if desired.

Dynamic binding/static typeing
When you have a pointer to an object, the object may actually be of a class that is derived from the class of the pointer (e.g., a Vehicle* that is actually pointing to a Car object; this is called "polymorphism"). Thus there are two types: the (static) type of the pointer (Vehicle, in this case), and the (dynamic) type of the pointed-to object (Car, in this case).

Static typing means that the legality of a member function invocation is checked at the earliest possible moment: by the compiler at compile time. The compiler uses the static type of the pointer to determine whether the member function invocation is legal. If the type of the pointer can handle the member function, certainly the pointed-to object can handle it as well. E.g., if Vehicle has a certain member function, certainly Car also has that member function since Car is a kind-of Vehicle.

Dynamic binding means that the address of the code in a member function invocation is determined at the last possible moment: based on the dynamic type of the object at run time. It is called "dynamic binding" because the binding to the code that actually gets called is accomplished dynamically (at run time). Dynamic binding is a result of virtual functions.

Share this post


Link to post
Share on other sites
I've been asked this once:

C++ casting

static cast
Conventional casting of one type to another, where there is some meaning to what the cast will do. (Thus, normally you can't cast from a Foo to a Bar, unless there's a definition for how to do this). Casting from one type to another is interesting, particularly, casting float to int. Like the unary minus, the casting operation does NOT change the value of the variable it is casting. Instead, like unary minus, it creates a temporary value which is the casted result.

dynamic cast
Meant for downcasting from a base class to a derived class. This check is done at runtime (as opposed to static casting which can generate the necessary code to do the casting at compile time). Will return NULL if unable to cast.

const cast
To temporarily add or remove constness from a const variable. Mostly used to get rid of warnings about violating constness.

reinterpret cast
To allow you to cast one pointer type to another pointer type to another. Since all pointers are addresses, and all addresses have the same number of bytes (at least, on a given ISA), the casting does not convert any bits. C++ decided to call this pointer-to-pointer casting, reinterpret_cast. What's being reinterpreted is the meaning of the bytes. When reinterpeting that pointer to a Bar * pointer, those bytes of the Foo object are unchanged.

Share this post


Link to post
Share on other sites
Are there any free sources where one can study the low-level details of these higher-level features of OOP which we take for granted? Is there a single textbook reference (nonfree) which is recommended?

Share this post


Link to post
Share on other sites
Quote:
Original post by helix
Here's a question that has been asked I'd say rougly 80% of my interviews. My answers are always pretty rough (and I think un-impressive).

Q: What does a constructor do? A destructor?

A: ?

As low level and as much detail as possible please! I understand at a higher level but I've just never had much cause to dig any deeper (such as what memory management, stack/heap, etc stuff is going on under the hood).


A constructor is a function that is run on a section of memory that is allocated, either on the stack, or the heap. It's purpose is to set up an object of whatever class it is part of. Using the constructor, data members of a class can be initialized and their value changed from whatever garbage value is given to the memory by the operating system before it was allocated. The constructor is run before the object it is intended to construct can be used.

A destructor is called on an object when it is deleted. This means whenever a local variable of a class type that has a destructor defined goes out of scope, or an object is delete from the heap. They are usually used to delete memory that was dynamically allocated for that object.

Share this post


Link to post
Share on other sites
Q: What is a pure virtual method in C++?

Me: You dont have to define a pure virtual method of a class, only declare it, but you have to define it in a child class to instanciate it. Usually it is used to create interfaces of classes.

Q: Ok, but CAN you define it in the mother class?

Me: ... why would you do that?

I felt a bit of an idiot not knowing that one. Yes, you can :P Beware of the answers you give! little side-story:

In my first year of college, I had a very tough technical interview for an internship. Didnt get it (got 4th place, a third year got the job). Three years and a half later, I interview them again for my final internship. Guess what, they had a list of every question they asked me three years before with the answers I gave them then. Told me they wanted to see if I learned anything lol! (got 2nd place, too bad).

Share this post


Link to post
Share on other sites
common ones I've been asked:

what is virtual inheritence?
What's the difference between a pointer and a reference?
what's the difference between prefix and postfix operators?
what is a virtual constructor?
why shouldn't you inherit from the standard library containers?


all usually followed by:
and what makes you think we're gonna pay you that much? [grin]

Share this post


Link to post
Share on other sites
Quote:

Q: What is a pure virtual method in C++?

Me: You dont have to define a pure virtual method of a class, only declare it, but you have to define it in a child class to instanciate it. Usually it is used to create interfaces of classes.

Q: Ok, but CAN you define it in the mother class?

Me: ... why would you do that?


Actually, technically you can't, because then it's not a PURE virtual method anymore. A pure virtual method is declared in the base object, has no definition, and has the syntax: virtual return-type method-name(parameter-list)=0;

A class can not be instanciated (is considered abstract) if it has one or more pure virtual functions that aren't yet defined in the class hierarchy.

Share this post


Link to post
Share on other sites
Quote:

what is virtual inheritence?


Virtual inheritance solves the "diamond inheritance" problem, where there is some class A, which is the base class of two classes B and C, and a fourth class, D, inherits from both B and C. Using non-virtual inheritance, an object of type D has two copies of A in it, and must be qualified as D::B::A and D::C::A. If B and C virtually inherit from A, then class D will only have one, unambiguous copy of A and referenceing A does not require such explicit qualification.

Quote:

What's the difference between a pointer and a reference?


A reference is simply another name for an object, and does not occupy a place in memory. Thus, you cannot take the address of a reference, because it doesn't actually exist at runtime.

Quote:

what's the difference between prefix and postfix operators?


Postfix operators (like increment) require storing a temporary copy of the object being incremented, incrementing the actual object, and returning the temporary copy as a result of the expression. Prefix operators (like increment) increment the actual object and evaluate to the actual object.

Quote:

what is a virtual constructor?


There is no such thing. People wanting behavior that would seem like a 'virtual constructor' are usually talking about something like the factory pattern.

Quote:

why shouldn't you inherit from the standard library containers?


Becuase they don't define their destructors as virtual, and therefore their destructors will not be called when your derived version of them is destructed.

Share this post


Link to post
Share on other sites
Quote:
Quote:
Quote:

why shouldn't you inherit from the standard library containers?


Becuase they don't define their destructors as virtual, and therefore their destructors will not be called when your derived version of them is destructed.


This is true if you have a pointer to the base container class, the destructor for the derived class will not be called. If you have a pointer / reference to the derived class, then it will be destroyed in the correct order. Because you can never be sure if you have the top-most derived class, it isn't safe to inherit from them. OpenSceneGraph has several classes that inherit from the container classes, but you don't have to believe me. Try it yourself.

Share this post


Link to post
Share on other sites
Deception666 - right, sorry. -1 for me :) Though I still think it's a bad idea to inherit from them, even if YOU take care to only delete them through a pointer or reference to the derived container class, someone else might not be so careful.

Share this post


Link to post
Share on other sites
Quote:
Original post by RDragon1
Quote:

Q: What is a pure virtual method in C++?

Me: You dont have to define a pure virtual method of a class, only declare it, but you have to define it in a child class to instanciate it. Usually it is used to create interfaces of classes.

Q: Ok, but CAN you define it in the mother class?

Me: ... why would you do that?


Actually, technically you can't, because then it's not a PURE virtual method anymore. A pure virtual method is declared in the base object, has no definition, and has the syntax: virtual return-type method-name(parameter-list)=0;

A class can not be instanciated (is considered abstract) if it has one or more pure virtual functions that aren't yet defined in the class hierarchy.

No, you can. The catch is that it is never implicitly called...concrete derived classes are still required to implement the function, and so those implementations will be called. At that point, you are free to explicitly call the base class's implementation. It is odd, and rare, but you can do it.

CM

Share this post


Link to post
Share on other sites
Quote:
Quote:
Quote:

What's the difference between a pointer and a reference?


A reference is simply another name for an object, and does not occupy a place in memory. Thus, you cannot take the address of a reference, because it doesn't actually exist at runtime.


Yes, a reference is simply another name for an object, and it DOES occupy a place in memory. You CAN take the address of a reference, because it DOES exist at runtime.

class IntRef
{
public:
IntRef( int * pInt ) : m_rnInt( pInt ) { };

private:
int & m_rnInt;
};



Assuming the compiler byte aligns to 4 bytes, this class would be 4 bytes in size, so a reference DOES occupy a place in memory. If I took the address of m_rnInt, I would have a pointer to a pointer of int. I would not be able to take the address of the reference if it did not exist at runtime, but because it is possible, it HAS to exist.

Share this post


Link to post
Share on other sites
Wow, this is some good stuff. Lots of ammunition to bone up on. :)

Quote:
Original post by EndarA constructor is a function that is run on a section of memory that is allocated, either on the stack, or the heap. It's purpose is to set up an object of whatever class it is part of. Using the constructor, data members of a class can be initialized and their value changed from whatever garbage value is given to the memory by the operating system before it was allocated. The constructor is run before the object it is intended to construct can be used.

A destructor is called on an object when it is deleted. This means whenever a local variable of a class type that has a destructor defined goes out of scope, or an object is delete from the heap. They are usually used to delete memory that was dynamically allocated for that object.


This is typically about as far as I get when asked. What's going on with the memory allocation etc (ie, what sort of operations are running affecting the performance)? I once answered with roughly the same response as above and then was prompted for more information. All I could provide was a blank stare and some "hemming and hawing."

I just had a nightmare thought, I was JUST asked this question in an interview last week and I glossed over most of this stuff. I was assuming that he wanted to know the nitty gritty details. I hope he doesn't think I'm a complete fool who doesn't understand the most basic details!

Share this post


Link to post
Share on other sites
Quote:
Original post by Deception666
Yes, a reference is simply another name for an object, and it DOES occupy a place in memory. You CAN take the address of a reference, because it DOES exist at runtime.

The standard does not require that references occupy any space, and you are prohibited from creating references to references, or pointers to references. A reference *might* exist at runtime, but if the compiler is able to do away with that requirement it will do so.

CM

Share this post


Link to post
Share on other sites
Quote:
Original post by Conner McCloud
Quote:
Original post by Deception666
Yes, a reference is simply another name for an object, and it DOES occupy a place in memory. You CAN take the address of a reference, because it DOES exist at runtime.

The standard does not require that references occupy any space, and you are prohibited from creating references to references, or pointers to references. A reference *might* exist at runtime, but if the compiler is able to do away with that requirement it will do so.

CM


After consulting a few reference materials, you are right. I can see where the compiler could remove the physical memory location of a reference within a function, but how about a class reference member variable? There has to be memory involved here? Does the compiler consider the reference to be a pointer but allow it to be treated as an object. Thanks.

Share this post


Link to post
Share on other sites
Quote:
Original post by RDragon1
Quote:

Q: What is a pure virtual method in C++?

Me: You dont have to define a pure virtual method of a class, only declare it, but you have to define it in a child class to instanciate it. Usually it is used to create interfaces of classes.

Q: Ok, but CAN you define it in the mother class?

Me: ... why would you do that?


Actually, technically you can't, because then it's not a PURE virtual method anymore. A pure virtual method is declared in the base object, has no definition, and has the syntax: virtual return-type method-name(parameter-list)=0;

A class can not be instanciated (is considered abstract) if it has one or more pure virtual functions that aren't yet defined in the class hierarchy.


Actually, you can define a virtual function in the base class:


//Header

class foo
{
public:
virtual void print_foo()=0;
};

//Source
void foo::print_foo()
{
std::cout << "foo";
}




This is useful when declaring a class that you want to be abstract, but which requires a default implementation for each function. The solution to the problem is to declare a pure virtual destructor - which needs to be defined or you'll get an unresolved external.


Quote:
why shouldn't you inherit from the standard library containers?

You might want to point out that privatley inheriting from the std library conatiners in permissable.

Share this post


Link to post
Share on other sites
Quote:
Original post by Conner McCloud
No, you can. The catch is that it is never implicitly called...concrete derived classes are still required to implement the function, and so those implementations will be called. At that point, you are free to explicitly call the base class's implementation. It is odd, and rare, but you can do it.

CM


If you want an abstract class but have no methods that are going to be pure virtual, use an pure virtual destructor!. Note - it must have an implementation, and that implementation will be implicitly called =)

Share this post


Link to post
Share on other sites
Quote:
Original post by helix
I'm going through the interview process right now and I'm trying to get fully prepared. There are a handful of questions that get asked the majority of the time and I figured this thread could be a good resource. I'm interested in technical questions only. Questions and answers would be best (as this is a study resource). But posting questions for the smart people here at gamedev to answer would also be quite helpful. Even just laying out some examples of tricky/obscure topics would be good (describing c++ casting or the meaning of volitile, etc implementation for example).

Have at it!

Thanks


A good interviewer isn't going to drill you on domain knowledge on C++ language features, you'll only get a 'smoke' test here. Good questions often involve pointer arithimetic and casitng - just to make sure you really know what a computer does, not so much random facts about a language. After all, languages come and go.

I wouldn't get obsessed with every feature of C++ because 1) There are probablly fewer people who "know" c++ then you have fingers 2) Clearly, this hasn't stopped people from making programs ;) Also, a word of advice, the worst thing you can do in an interview is claim you know something 100% and then not know it. Be humble.





Share this post


Link to post
Share on other sites
Quote:
RDragon1:
Virtual inheritance solves the "diamond inheritance" problem, where there is some class A, which is the base class of two classes B and C, and a fourth class, D, inherits from both B and C. Using non-virtual inheritance, an object of type D has two copies of A in it, and must be qualified as D::B::A and D::C::A. If B and C virtually inherit from A, then class D will only have one, unambiguous copy of A and referenceing A does not require such explicit qualification.


How do you do this in C++? Can I see a sample of this?

~guyaton

Share this post


Link to post
Share on other sites
A lot of places will show you a very simple class and program that will compile, link and run but has one crash bug and various other mistakes. Often they are looking for knowledge beyond just knowing that a class contains methods and variables. Things like Copy Constructors, const correctness, use of references versus pointers. These things aren't the meat of interviews (I'd hope) and are generally shown to everyone who comes through the door, regardless of level. Depending on the level you come in at and what you have said on your CV they will have different expectations. The real interview questions are the more benign ones about development methodology, debugging and opmisation practices and the general tests of problem solving ability.

The real hard ones tend to be optimising simple C functions like memcpy for cross-platform code. They are expecting explanations along the lines of memory alignment, cache misses and the ability to batch process the majority of the data.

Evil maths questions can also be expected. For example I was recently asked how you'd find if the line of intersection between two planes intersects a sphere. This was after nearly four hours of various assessment! If you are going for a games related position, definately expect some kind of geometry type question. Intersection of a triangle and line in 3D is a common one and something most people will have come across before (hopefully!).

I've recently been on the interview circuit and have got two offers out of it. These are probably the most salient points I can come up with (as well as an example of how evil the questions can be).

Share this post


Link to post
Share on other sites

This topic is 4394 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this