Implement a dynamic array that doubles its capacity when full

Quick Overview

Implement a dynamic array of integers that starts at a given capacity and doubles its buffer when full, with append, get, pop, size and capacity methods. It tests buffer management, bounds checks on indices and empty arrays, and the amortized analysis behind constant-time appends.

Implement a dynamic array that doubles its capacity when full

Company: Point72

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: easy

Interview Round: Other

Design and implement a generic dynamic array that grows automatically as elements are added, similar to a resizable list or vector found in standard libraries. Internally, the structure should start with a small initial capacity and double its capacity whenever it runs out of space, ensuring amortized `O(1)` appends. Accessing an element by an out-of-range index should return `-1`, and removing from an empty array should be a no-op that returns `-1`. Implement the following methods: - `DynamicArray(initialCapacity: int)`: create an empty array whose underlying buffer holds `initialCapacity` elements. - `append(value: int)`: append `value` to the end of the array, doubling internal capacity if full. - `get(index: int) -> int`: return the element at `index`, or `-1` if the index is out of bounds. - `pop() -> int`: remove and return the last element of the array, or `-1` if the array is empty. - `size() -> int`: return the number of elements currently stored in the array. - `capacity() -> int`: return the current capacity of the underlying buffer. Example 1: ```text Input: ["DynamicArray", "append", "append", "append", "get", "size", "pop", "size", "get"] [[2], [10], [20], [30], [1], [], [], [], [5]] Output: [null, null, null, null, 20, 3, 30, 2, -1] Explanation: arr = DynamicArray(2) # initial capacity = 2 arr.append(10) # buffer: [10], size=1, cap=2 arr.append(20) # buffer: [10, 20], size=2, cap=2 arr.append(30) # full -> doubles cap to 4, buffer: [10, 20, 30], size=3 arr.get(1) # -> 20 (element at index 1) arr.size() # -> 3 arr.pop() # -> 30 (removes last element) arr.size() # -> 2 arr.get(5) # -> -1 (index out of bounds) ``` Example 2: ```text Input: ["DynamicArray", "pop", "size", "append", "get", "capacity"] [[1], [], [], [42], [0], []] Output: [null, -1, 0, null, 42, 1] Explanation: arr = DynamicArray(1) # initial capacity = 1 arr.pop() # -> -1 (array is empty) arr.size() # -> 0 arr.append(42) # buffer: [42], size=1, cap=1 arr.get(0) # -> 42 arr.capacity() # -> 1 (no resize needed yet) ``` ```hint Two different counts Track how many slots are in use separately from how many slots exist, and decide which of the two every bounds check in get and pop should compare against. ``` ### Constraints & Assumptions - `1 <= initialCapacity <= 1000` - `0 <= value <= 10^9` - At most `10^4` calls will be made across all operations. - Capacity must double when the array runs out of space. - `get(index)` returns `-1` if `index` is out of bounds. - `pop()` returns `-1` if the array is empty. ### Clarifying Questions to Ask - May I use the language's built-in growable list as the buffer, or should I manage a fixed-size block and do the copying myself? - Should `pop` ever shrink the buffer, or does capacity only grow? - Are negative indices out of bounds, or should they count from the end as Python lists do? ### What a Strong Answer Covers - Separate tracking of the element count and the buffer capacity, with the buffer allocated at full capacity. - The resize step: when it triggers, how the live elements are copied, and why capacity exactly doubles. - An argument that n appends cost O(n) in total, so each append is amortized O(1), along with the worst case of a single append. - Every boundary case: negative and too-large indices, `pop` on an empty array, and the append that triggers a resize. ### Follow-up Questions - If `pop` had to give memory back, when would you shrink the buffer, and why does shrinking at half full perform badly? - How would a growth factor of 1.5 change memory use and the amortized cost of appends? - How would you make the array truly generic, and what would you return instead of `-1`? - How would you add `insert(index, value)` and `remove(index)`, and what would each cost?

Overview: Implement a dynamic array of integers that starts at a given capacity and doubles its buffer when full, with append, get, pop, size and capacity methods. It tests buffer management, bounds checks on indices and empty arrays, and the amortized analysis behind constant-time appends.

Point72 logo
Point72
Nov 25, 2026
easySoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design and implement a generic dynamic array that grows automatically as elements are added, similar to a resizable list or vector found in standard libraries.

Internally, the structure should start with a small initial capacity and double its capacity whenever it runs out of space, ensuring amortized O(1) appends.

Accessing an element by an out-of-range index should return -1, and removing from an empty array should be a no-op that returns -1.

Implement the following methods:

  • DynamicArray(initialCapacity: int) : create an empty array whose underlying buffer holds initialCapacity elements.
  • append(value: int) : append value to the end of the array, doubling internal capacity if full.
  • get(index: int) -> int : return the element at index , or -1 if the index is out of bounds.
  • pop() -> int : remove and return the last element of the array, or -1 if the array is empty.
  • size() -> int : return the number of elements currently stored in the array.
  • capacity() -> int : return the current capacity of the underlying buffer.

Example 1:

Input:
["DynamicArray", "append", "append", "append", "get", "size", "pop", "size", "get"]
[[2],            [10],     [20],     [30],     [1],   [],     [],    [],     [5]]

Output: [null, null, null, null, 20, 3, 30, 2, -1]

Explanation:
arr = DynamicArray(2)            # initial capacity = 2
arr.append(10)                   # buffer: [10], size=1, cap=2
arr.append(20)                   # buffer: [10, 20], size=2, cap=2
arr.append(30)                   # full -> doubles cap to 4, buffer: [10, 20, 30], size=3
arr.get(1)                       # -> 20   (element at index 1)
arr.size()                       # -> 3
arr.pop()                        # -> 30   (removes last element)
arr.size()                       # -> 2
arr.get(5)                       # -> -1   (index out of bounds)

Example 2:

Input:
["DynamicArray", "pop", "size", "append", "get", "capacity"]
[[1],            [],    [],     [42],     [0],   []]

Output: [null, -1, 0, null, 42, 1]

Explanation:
arr = DynamicArray(1)            # initial capacity = 1
arr.pop()                        # -> -1   (array is empty)
arr.size()                       # -> 0
arr.append(42)                   # buffer: [42], size=1, cap=1
arr.get(0)                       # -> 42
arr.capacity()                   # -> 1    (no resize needed yet)

Constraints & Assumptions

  • 1 <= initialCapacity <= 1000
  • 0 <= value <= 10^9
  • At most 10^4 calls will be made across all operations.
  • Capacity must double when the array runs out of space.
  • get(index) returns -1 if index is out of bounds.
  • pop() returns -1 if the array is empty.

Clarifying Questions to Ask Guidance

  • May I use the language's built-in growable list as the buffer, or should I manage a fixed-size block and do the copying myself?
  • Should pop ever shrink the buffer, or does capacity only grow?
  • Are negative indices out of bounds, or should they count from the end as Python lists do?

What a Strong Answer Covers Guidance

  • Separate tracking of the element count and the buffer capacity, with the buffer allocated at full capacity.
  • The resize step: when it triggers, how the live elements are copied, and why capacity exactly doubles.
  • An argument that n appends cost O(n) in total, so each append is amortized O(1), along with the worst case of a single append.
  • Every boundary case: negative and too-large indices, pop on an empty array, and the append that triggers a resize.

Follow-up Questions Guidance

  • If pop had to give memory back, when would you shrink the buffer, and why does shrinking at half full perform badly?
  • How would a growth factor of 1.5 change memory use and the amortized cost of appends?
  • How would you make the array truly generic, and what would you return instead of -1 ?
  • How would you add insert(index, value) and remove(index) , and what would each cost?