Implement a dynamic array that doubles its capacity when full
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: easy
Interview Round: Other
Implement a generic dynamic array from scratch that behaves like a resizable list, supporting `add`, `get`, `remove` and `size` operations.
The structure starts with a default initial capacity of 10 and must automatically double its internal capacity whenever it is full and a new element is added.
When `get` or `remove` is called with an index outside the range `[0, size)`, the operation must raise an index-out-of-bounds error (`IndexError` in Python). Removing an element shifts all later elements one position to the left.
Implement the following methods:
- `add(element: int)`: append `element` to the end of the array, doubling the internal capacity if it is full.
- `get(index: int) -> int`: return the element at position `index`. Raises `IndexError` if `index` is out of bounds.
- `remove(index: int) -> int`: remove and return the element at position `index`, shifting later elements left. Raises `IndexError` if `index` is out of bounds.
- `size() -> int`: return the number of elements currently stored in the dynamic array.
For example, given one list of calls and one list of arguments:
```text
Input:
["DynamicArray", "add", "add", "add", "get", "size", "remove", "get", "size"]
[[], [10], [20], [30], [1], [], [0], [0], []]
Output: [null, null, null, null, 20, 3, 10, 20, 2]
```
The same sequence written as code:
```python
arr = DynamicArray() # capacity = 10, size = 0
arr.add(10) # data: [10], size = 1
arr.add(20) # data: [10, 20], size = 2
arr.add(30) # data: [10, 20, 30], size = 3
arr.get(1) # returns 20
arr.size() # returns 3
arr.remove(0) # returns 10, data shifts left to [20, 30]
arr.get(0) # returns 20
arr.size() # returns 2
```
```hint Two different counts
The backing array has a length (its capacity) and the structure has a number of stored elements. Decide which of the two each method should compare against.
```
```hint Shifting on removal
Walk through `remove` on a completely full array. Check that the shift loop never reads past the last stored element, and think about what should be left in the slot that becomes free.
```
### Constraints & Assumptions
- The default initial capacity is 10.
- Capacity doubles whenever the internal array is full and a new element is added.
- For `get(index)` and `remove(index)`, the index must satisfy `0 <= index < size`; otherwise raise an `IndexError`.
- At most 10,000 operations are performed across all methods.
- Stored elements may be any value; the test cases use integers.
### Clarifying Questions to Ask
- Should a negative index count from the end, as it does for Python lists, or is it out of bounds?
- Should the internal storage ever shrink after removals, or does it only grow?
- May the internal storage be a built-in growable list, or should it be treated as a fixed-size block that is replaced on each resize?
- Will more than one thread use the same instance?
### What a Strong Answer Covers
- Keeping the logical size separate from the physical capacity, and checking bounds against the right one.
- When the resize happens, how existing elements move to the new storage, and why doubling keeps `add` cheap on average.
- The shift in `remove`: the loop range, the freed slot and the returned value.
- The time and space cost of each operation, including memory use after many removals.
- Edge cases such as an empty array, a negative index and the add that triggers the first resize.
### Follow-up Questions
- How would you shrink the internal storage after removals, and what threshold stops alternating adds and removes from resizing on every call?
- How would you add `insert(index, element)`, and what does it cost?
- What changes if the array grows by 1.5x instead of 2x, or by a fixed 10 slots each time?
- How would you make the structure safe to use from several threads?
Overview: Build a resizable array from scratch with add, get, remove and size, starting at capacity 10 and doubling when full. It tests keeping the logical size apart from the capacity, bounds checks, shifting elements on removal, and the amortized cost of resizing.