Sample C++ Programmer Interview Questions and Answers from Goldman Sachs Equity Platform Tech
The phone interview questions can be found at www.careercup.com. Below are the on-site questions.
Show me how virtual function works in C++
class base{
public:
virtual void foo(){}
};
class derived: public base{
public:
void foo(){}
};
base * a, *b;
a = new base();
a->foo() // Calls base’s foo()
b = new derived();
b->foo() // Calls derived’s foo()
What if the function is not declared virtual?
The base class function would have been called in both times, because the function
address is statically bounded during compile time.
What if you declare void foo(int) in the base class and call b->foo(int)?
It’ll result in compile time error because derived::foo() hides foo(int) in base class
Use the keyword using
class derived: public base{
public:
using base::foo;
void foo();
}
What problems do you see from the above base and derived class?
Destructor must be virtual in base class
Hash table vs. STL map
map: no need to have a hash function, don’t need to handle collision, O (log N) insert and lookup, inserted key/data in sorted order. If you need to find a max/min element, use map instead of hash table.
hash table: transforms a key into position within a table. O(1) lookup and insert in most cases, doesn’t insert hashed key/data in sorted order
How is hash table implemented?
You need a good hash function (i.e. % prime #) to ensure the hash values are uniformly distributed. If collision occurs, you use the separate chaining method (Good for full table), which is a linked list that chains element in the same slot, or use the probing method, which increases the position by some amount until an empty position is found (Good for sparse table)
When the # of elements to table size ratio is greater than a threshold, then the hash table needs to be resized. You need to transfer the entries from old table to new table by recomputing their positions using the new hash function
Write a smart_ptr class
template
class smartPointer {
public:
smartPointer (T * ptr) {
ptr = ptr;
rc = new RefCount();
rc++;
}
smartPointer(smartPointer rhs) {
realPtr = rhs->realPtr;
rc = rhs->rc;
rc++;
}
smartPointer & operator= (smartPointer &rhs) {
if (this == &rhs) return *this;
realPtr = rhs->realPtr;
rc = rhs->rc;
rc++;
return *this;
}
T* operator->() {
return realPtr;
}
T operator *() {
return *realPtr;
}
~smartPointer() {
rc–;
if (rc->count() == 0) {
delete rc;
delete realPtr;
rc = null;
realPtr = null;
}
}
protected:
T * realPtr;
RefCount *rc;
}
Template vs. Inheritance
Template is good if you want to use your container for multiple class
template
class List{
};
class contact{
};
list agenda;
Inheritance is good when you want to restrict your container to store only this class of object
class Agenda : public List
For a hashtable, if the number of inputs are small, what can be used instead of a hashtable?
Use a map, which also stores key/data pair, but doesn’t need to statically allocate
a huge hash table with many empty slots


