What is memory ownership for a container? - Answers When a container owns a value, only that
container can modify the value. A drawback of this is it takes a long time to copy containers like this.
When a container has pointers, either the container or other objects pointing to the same thing can
modify, which can be unsafe. This can be used for shared data. When a container owns a reference to
a value, it doesn't own the value. You cannot delete by reference. The value must be initialized but
cannot be assigned to.
student answer:
Memory ownership of a container refers to how controlled the interface is to the data held within the
container. There are different degrees of ownership ranging from no control (as in the case of
references) to a lot of control (which is the default from within the container). A container class can
control how much access objects outside the class can have over the elements of the container
(hence the class has a public interface for other objects).
- A container composed of values can completely restrict or give complete access to the elements (the
former being impractical and the latter being close to what vector does) but in either situation the
container class mutates the data.
- A container composed of pointers however is a different story. Because they contain pointers, the
container only protects the location data (the pointer data) but it cannot protect or control the
interface to the data stores at the addresses. Thus, the container does not have total control over the
relevant data. Hypothetically, an object outside the container can make edits to the data.
What are some disadvantages of arrays? - Answers when many insertions are needed, we need to
move everything over EVERY time we insert a number.
One disadvantage of an array is that there are no bound checks so you increase the change of illegally
accessing memory (causing a seg fault) or you need O(1) complexity overhead like a size variable to
maintain bound checks.
Why do you need a const and non-const version of some operators?
What should non=const op[ ] return ? - Answers Read and write. In some cases, the compiler knows it
can speed some things up if it knows it never has to make any changes. Non const operator returns
the object, which can be modified.
You would need both const and non-const version of operations because you would want to be as
safe as possible. One good rule for C++ programming from the book "Effective C++" highlights that
you should make every variable you don't manipulate a const variable to protect any data you can.
Having methods return const makes your operation more resilient to potential bugs (like accidently
mutating data in a container when you didn't want to).
how many destructor calls (min, max) can be invoked by
operator delete
operator delete[ ] - Answers A destructor should have only one call to delete for each new allocation
to the heap and one call to delete[] for each new[] allocation to the heap.
Why would you use a pointer-based copying algorithm? - Answers Pointer-based algorithm works
with both random access and sequential access (ex: will work with both a linked list and a
vector).Random access would not work with a linked list, just a vector.
You would use a pointer-based copying algorithm when the objects are very large and it would be
costly to copy or when you don't necessarily know the end of a container so you can use a nullptr as
the null terminator. This is the idea behind vector.end() iterator which is an iterator that points one
past the end of a vector.
Are C++ strings null-terminated? - Answers no
Give two examples of off-by-one bugs - Answers accessing one off the end of the array when
comparing i <= SIZE. Assigning one off the end of the array with arr[i] = arr[i + 1] when i = size - 1.
How do I set up a two-dim array class? - Answers setup an array of int pointers:
const int ** arr = new int * [R];
, The array is a collection of pointers, which is currently not looking at anything
Perform an amortized complexity analysis of an automatically resizable container with a doubling
policy - Answers When a call to add an element places the element one passed the end, the container
will resize to twice it's size. In amortized complexity analysis, we find the cost per operation over a
sequence of operations. In the worst case of the container reaching it's limit, the complexity of the
nth addition would be O(n). However, the next n-1 calls to add an element will have a O(1)
complexity. So we have n + n(1) = 2n => O(n) complexity. O(n) / n = O(1) amortized complexity.
Discuss pros and cons of pointers and references when implementing container classes. - Answers
Pointers vs references in container classes.
Pointers are useful for having containers of large objects. They are also useful for sharing data with
other objects (as long as you have mutually exclusive locks on data as they are being edited just so
you don't have two objects accidently edit the object at the same time).
References are useful for function returns but aren't very useful as elements of the container.
Convert indexing between a 1D array and a 2D array - Answers 1D to 2D: column = index %
num_columns
row = index / num_columns
2D to 1D: index = row * num_columns + column
Copying complexities: worst, best and average case - Answers O(n)
write an algorithm for dynamic reallocation of array class (aka, inserting something when the array is
full) - Answers
time complexity of inserting into an array - Answers Best case: O(1). We just need to insert at the end
Worst case: O(n). We need to loop through the whole thing and insert at the beginning
Average Case: O(n). Insert in the middle, but n/2 is O(n).
stl swap<> and max<> - Answers
STL: random_shuffle - Answers Can be used with a container or array. Use to generate random
permutations, which is good for testing programs. ex:
1. random_shuffle(perm.begin(), perm.end());
with an array:
2. random_shuffle(perm, perm + N);
STL: iota - Answers fills a container instead of using a loop:
1. iota(v.begin(), v.end(), 0);
2. iota(perm, perm + N, 0);
is an array or linked list more efficient for a stack? - Answers Linked list is always constant time, and
the complexities are very similar. However, the linked list must allocate memory every time while an
array only has to when it has run out of size. Also, the memory overhead for a linked list is lower than
an array. So contiguous memory is probably a better choice
Queue implementation as a circular buffer - complexities for:
1. push
2. pop
3. front
4. size (how would we calculate this?)
5. empty (how would we calcualte this?) - Answers 1. push: amortized O(1)
2. pop: O(1)
3. front: O(1)
4. empty: O(1) (check if back index == front index)
5. size: O(1): if back index is greater than front index, return back - front. else return back - front +
array size