Citigroup Interview Question for Software Engineer / Developers






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

You can use pointer ton an integer for the reference count, in place of using an integer. When creating fresh, new an integer and initialize the reference count to 0. When you copy, you can make the pointer point to the reference count of the original object and increment it by one.

- Arvind.Sinh June 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

in the copy constructor, how to implement the reference count? I used an integer for counting the reference, incrementing by 1 when constructing a new shared pointer, decreased by 1 when destructing a shared pointer. but the problem here is how to share the reference count for all shared pointers. obvious i can't use static variable.

- Itcecsa June 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It's simple, you can create an inner object with actual stored data and ref count.

template <class T>
public class RefPtr
{

public:
   RefPtr(RefPtr& other)
   {
       if (other != this)
       {
           other.Release();
           other.m_pPtr = m_pPtr;
           m_pPtr->m_refCount++;
       }
   }

   RefPtr<T>& operator=(T& data)
   {
      Release();
      m_pPtr = new RefPtrInner();
      m_pPtr->m_pData = &data;
      m_pPtr->m_refCount = 1;
   }

private:

   void Release()
   {
        if (m_pPtr != NULL)
        {
            m_pPtr->m_refCount--;
            if (m_pPtr->m_refCount == 0)
            {
                if (m_pPtr->m_pData != NULL)
                {
                    delete m_pPtr->m_pData;
                    delete m_pPtr;
                }
                m_pPtr = NULL;
            } 
        }
   }

   typedef struct RefPtrInner
   {
      T* m_pData;
      int m_refCount;
   } RefPtrInner;

   RefPtrInner* m_pPtr;
};

- Ashish Kaila June 09, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

It's simple, you can create an inner object with actual stored data and ref count.

template <class T>
public class RefPtr
{

public:
   RefPtr(RefPtr& other)
   {
       if (other != this)
       {
           other.Release();
           other.m_pPtr = m_pPtr;
           m_pPtr->m_refCount++;
       }
   }

   RefPtr<T>& operator=(T& data)
   {
      Release();
      m_pPtr = new RefPtrInner();
      m_pPtr->m_pData = &data;
      m_pPtr->m_refCount = 1;
   }

private:

   void Release()
   {
        if (m_pPtr != NULL)
        {
            m_pPtr->m_refCount--;
            if (m_pPtr->m_refCount == 0)
            {
                if (m_pPtr->m_pData != NULL)
                {
                    delete m_pPtr->m_pData;
                    delete m_pPtr;
                }
                m_pPtr = NULL;
            } 
        }
   }

   typedef struct RefPtrInner
   {
      T* m_pData;
      int m_refCount;
   } RefPtrInner;

   RefPtrInner* m_pPtr;
};

- Ashish Kaila June 09, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I'm not very clear about this question. Does it mean that you want the reference count of an object? So, whenever a new pointer points to this object, the count is incremented? Thanks!

- abc June 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In the copy constructor increase reference count by 1
In the destructor decrease the reference count by 1 and if 0 delete the inner object

- Ashish Kaila June 09, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@Ashish Kaila
good work but do you think it would work for this case:
RefPtr<A> p(new A);
RefPtt<A> p2(p);

In this case the ref count should be 2 but in the code by calling " RefPtr(RefPtr& other)"
it would call Release() and would decrease the ref count...
plz correct me if I am wrong

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

Can we not just create a static integer variable and initialize it to 0. On every object creation increment the number by 1 in constructor and on deletion decrement the number if destructor.

- Ritesh August 19, 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