PayPal Interview Question for Developer Program Engineers


Country: -
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
2
of 2 vote

Many of the earlier solutions are wrong because they don't properly update the count. The count variable has to be a single value that is shared among all copies of the same pointer. It should therefore be an int* allocated on the heap when the pointer is first created (via a T* object and not via copy constructor). Otherwise, old copies of the smart pointer won't know about new copies that may be floating around and may prematurely de-allocate the object.

Here's some code:

template <class T>
class shared_ptr
{
public:
shared_ptr(T* p):
ptr(p), 
count(new int(1)) {}

//copy constructor must copy the fields AND increment the reference count
shared_ptr(const shared_ptr<T>& other):
ptr(other.ptr),
count(other.count)
{
    incrementCount();
}

//destructor must decrement reference count
~shared_ptr()
{
    decrementCount();
}

//assignment operator must decrement the reference count of the 
//object the pointer no longer points to and increment the count
//of the object it now does point to
shared_ptr<T>& operator=(const shared_ptr<T>& obj)
{
    if(this->ptr != obj.ptr)
    {
        decrementCount();
        ptr = obj.ptr;
        count = obj.count;
        incrementCount();
    }
    return *this;
}

//not a bad idea to have these, as someone else suggested
T* operator->(){ return ptr; }
T& operator*() { return *ptr;}
T* get() { return ptr;}

private:
inline void incrementCount()
{
    (*count)++;
}

//when decrementing, we must free the underlying
//pointer if there are no more copies of it
inline void decrementCount()
{
    (*count)--;
    if (*count <= 0)
    {
        delete ptr;
        delete count;
    }
}

T* ptr;
int* count;
}

- eugene.yarovoi October 02, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Idea is something like this:
struct
{
char * ptr;
u32 count;
}smart_ptr

retain()
{
count++;
}

release()
{
count--;
if(count == 0)free(ptr);
}

- Anonymous September 26, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

In my view examiner might be more interested in knowing what will be the lay out of the class. Smart pointer class shall contain the overloaded operators for * and -> a destructor and constructor

- Anonymous September 26, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes I suppose. This is a hard problem. Even CTCI 5th edition has it wrong.

Of course, from the the point of view at what can be done in an interview...

- Anonymous September 27, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

this is smart pointer implementation...
correct me if any thing is worng.....
template <class T>
class sPointer
{
public:
sPointer(T *p = 0):ptr(p){}
~sPointer(){delete ptr;}
T* operator->(){ return ptr; }
T& operator*() { return *ptr;}
T* get() { return ptr;}

sPointer(T &obj)
{
cout<<"auto copy constructor"<<endl;
this->ptr = obj.ptr;
obj.ptr = NULL;
}

T& operator=(const T & obj)
{
if(this->ptr != obj.ptr)
{
ptr = obj.ptr;
obj.ptr = NULL;
}
return *this;
}
private:
T *ptr;
};

- Anonymous September 27, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is example of shared obj....
if any thing is wrong plz correct me.

//reference count class to count the no of obj’s created
class refCount
{
int count;
public:
void incrCount(){count++;}
int decrCount(){ return --count;}
};

template <class T>
class sPointer
{
private:
T *ptr;
refCount *rCount;
public:
sPointer(T *p = 0):ptr(p),rCount(0)
{
rCount = new refCount();
rCount->incrCount();
}
~sPointer()
{
if(rCount->decrCount() == 0)
{
delete ptr;
delete rCount;
}
}
// copy constructor
sPointer(const T & obj) : ptr(obj.ptr)
{
rCount = obj.rCount;
rCount->incrCount();
}

//overload assignment operator
T& operator=(const T & obj)
{
if(this->ptr != obj.ptr)
{
ptr = obj.ptr;
rCount->incrCount();
}
return *this;
}

T* operator->(){ return ptr; }
T& operator*() { return *ptr;}
};
//this is example class
class B
{
private:
int a;
public:
B(int aa = 0):a(aa){cout<<"B - constructor"<<endl;}
~B(){ cout<<"B dest"<<endl;}
void disp() { cout<<"show - "<<a<<endl; }
};

// main prog
int main()
{
B *obj = new B(100);
obj->disp();

//creating the shared pointer
sPointer<B> obj1(obj);
obj1->disp();

sPointer<B> obj2;

//overload assignment operator called
obj2 = obj1;

obj1->disp();
obj2->disp();

// when objs count is zero then only obj will be deleted

return 0;
}

- Anonymous September 27, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

eugene.yarovoi Your answer complete. Thanks

- Pardeep October 04, 2011 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More