KatInterview

Build a Scoped Owner and a Resizable Container from Scratch

Data Structure DesignModel answer
MediumModel answerData Structure Designc++memory-managementraii

Last updated

Implement a move-only smart pointer and a generic dynamic array in C++, exercising RAII, the Rule of Five, placement new, and exception-safe resizing.

Background

Two of the most fundamental building blocks in modern C++ are a scoped ownership wrapper and a growable contiguous container. The standard library ships std::unique_ptr and std::vector, but writing them yourself is the clearest way to demonstrate that you actually understand what those abstractions are doing under the hood.

Part 1 — Scoped Ownership Wrapper

Write a class template ScopedPtr<T> that owns exactly one heap-allocated object and frees it when the wrapper goes out of scope.

Requirements:

  • A constructor that accepts a raw pointer.
  • A destructor that deletes the owned object.
  • Dereference (*) and member-access (->) operators.
  • A release() method that relinquishes ownership, returning the raw pointer and leaving the wrapper empty.
  • Copying must be forbidden. Moving must transfer ownership and leave the source empty.

Part 2 — Generic Growable Array

Write a class template DynArray<T> that stores elements of type T in a heap-allocated contiguous buffer and grows automatically.

Requirements:

  • push_back(T value) — appends an element.
  • size() — returns the current element count.
  • operator[] — provides indexed access.
  • When the buffer is full, allocate a new one with twice the capacity, transfer all existing elements, then release the old buffer.
  • Satisfy the Rule of Five: destructor, copy constructor, move constructor, copy assignment, and move assignment must all be handled explicitly.
  • The container must accept move-only element types such as ScopedPtr<T>.

Constraints

  • Use new and delete (or ::operator new / ::operator delete) for all memory management — no std::allocator or smart-pointer wrappers around the buffer itself.
  • After completing the implementation, write a short prose explanation (a few sentences is enough) of how your resize logic avoids leaving the container in a broken state when an exception is thrown during the transfer.

Usage Example

ScopedPtr<int> p(new int(10));
DynArray<ScopedPtr<int>> arr;
arr.push_back(std::move(p));
// *arr[0] == 10, p is now empty

Hints

Hint 1

For ScopedPtr, the move constructor and move assignment operator both need to call release() on the source — that single operation atomically hands over the pointer and nulls out the original. In reset(), assign the new pointer to the member variable before deleting the old value, so the object stays consistent even if the destructor of T does something unusual.

Hint 2

DynArray must separate raw memory allocation from object construction. Allocating with new T[cap] default-constructs objects you don't want and fails outright for types with no default constructor. Allocate a raw byte buffer with ::operator new(n * sizeof(T)), construct elements in it with placement new, and tear them down with explicit destructor calls (p[i].~T()) before releasing the buffer.

Hint 3

The resize step should follow a commit-or-rollback pattern: allocate the new buffer and transfer elements entirely before touching the old buffer's state. If a constructor throws partway through the transfer, destroy the partially built elements in the new buffer and free it, then rethrow — leaving the original buffer completely untouched. Consider using std::move_if_noexcept instead of a plain st…