Design a growing list (dynamic array) with indexed insert, set, remove and capacity doubling

Quick Overview

Design a dynamic array with append, insert, get, set, remove, size and capacity on top of a fixed buffer that doubles when full. It tests index validation for each operation, shift loops that keep every value intact, and the amortized cost of resizing.

Design a growing list (dynamic array) with indexed insert, set, remove and capacity doubling

Company: Cisco

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

Design a growing list (a dynamic array similar to Java's `ArrayList` or C++'s `std::vector`) backed by a fixed-size internal buffer that automatically resizes when it runs out of capacity. The list must support the standard CRUD operations: appending, inserting, reading, updating and removing elements by index. When the internal buffer becomes full on an `add` operation, the capacity must double so that the amortized cost of `add` stays O(1). All index-based operations must validate the index and raise an `IndexError` when it is out of bounds. Implement the following: - `GrowingList(initial_capacity: int)`: create an empty list whose buffer has `initial_capacity` slots. - `add(value: int)`: append `value` to the end of the list, doubling the internal capacity if it is full. - `insert(index: int, value: int)`: insert `value` at position `index`, shifting later elements one position to the right. The valid range is `0 <= index <= size`. A full buffer grows the same way it does for `add`. - `get(index: int) -> int`: return the element at position `index`. Raises `IndexError` if out of bounds. - `set(index: int, value: int)`: replace the element at position `index` with `value`. Raises `IndexError` if out of bounds. - `remove(index: int) -> int`: remove and return the element at position `index`, shifting later elements one position to the left. Raises `IndexError` if out of bounds. - `size() -> int`: return the number of elements currently stored in the list. - `capacity() -> int`: return the current capacity of the internal buffer. In the first example the buffer starts with two slots and doubles once: ```text Input: ["GrowingList", "add", "add", "add", "capacity", "add", "capacity", "get", "set", "get", "size"] [[2], [10], [20], [30], [], [40], [], [2], [1, 99], [1], []] Output: [null, null, null, null, 4, null, 4, 30, null, 99, 4] ``` ```python lst = GrowingList(2) # capacity = 2, size = 0 lst.add(10) # buffer: [10, _], size = 1 lst.add(20) # buffer: [10, 20], size = 2 (full) lst.add(30) # resize to 4, buffer: [10, 20, 30, _], size = 3 lst.capacity() # returns 4 (doubled from 2) lst.add(40) # buffer: [10, 20, 30, 40], size = 4 (full) lst.capacity() # returns 4 (no resize yet) lst.get(2) # returns 30 lst.set(1, 99) # buffer: [10, 99, 30, 40] lst.get(1) # returns 99 lst.size() # returns 4 ``` In the second example an insert shifts elements right and a remove shifts them back: ```text Input: ["GrowingList", "add", "add", "add", "insert", "get", "remove", "get", "size"] [[4], [1], [2], [3], [1, 99], [1], [2], [1], []] Output: [null, null, null, null, null, 99, 2, 99, 3] ``` ```python lst = GrowingList(4) # capacity = 4, size = 0 lst.add(1) # buffer: [1, _, _, _], size = 1 lst.add(2) # buffer: [1, 2, _, _], size = 2 lst.add(3) # buffer: [1, 2, 3, _], size = 3 lst.insert(1, 99) # buffer: [1, 99, 2, 3], size = 4 lst.get(1) # returns 99 lst.remove(2) # returns 2, buffer: [1, 99, 3, _], size = 3 lst.get(1) # returns 99 lst.size() # returns 3 ``` ```hint Order of checks in insert Decide what `insert` should do when the index is invalid and the buffer is also full. Then trace an insert at position `size` on a full buffer. ``` ```hint Shift direction Opening a gap and closing a gap move elements in opposite directions. For each, work out which end the loop should start from so that no value is overwritten before it has been copied. ``` ### Constraints & Assumptions - `1 <= initial_capacity <= 10^4` - For `get`, `set` and `remove`, a valid index satisfies `0 <= index < size` at the time of the call. For `insert`, it satisfies `0 <= index <= size`. Any other index raises `IndexError`. - At most 100,000 operations are performed. - `-10^9 <= value <= 10^9` ### Clarifying Questions to Ask - Should the buffer ever shrink after removals, and if so, what should `capacity()` report afterwards? - What should the constructor do with a capacity below 1, or with no capacity at all? - Should a negative index count from the end, as it does for Python lists, or is it out of bounds? - Will more than one thread use the same list? ### What a Strong Answer Covers - Tracking size and capacity separately, and validating each operation against the right bound, including the extra position `insert` allows. - When `add` and `insert` trigger a resize, how elements move to the new buffer, and why doubling keeps appends cheap on average. - Shift loops in `insert` and `remove` that run in the correct direction over the correct range. - The time and space cost of every method, including memory after removals or with a large initial capacity. - Edge cases such as an insert at the end of a full buffer, removing the last element and operations on an empty list. ### Follow-up Questions - How would you shrink the buffer after many removals without resizing on every alternating add and remove? - How would you support iteration, and what should happen if the list is modified while someone iterates over it? - How would you make `insert(0, value)` and `remove(0)` O(1) as well, the way a double-ended queue does? - What would you change to make the list safe for concurrent use?

Overview: Design a dynamic array with append, insert, get, set, remove, size and capacity on top of a fixed buffer that doubles when full. It tests index validation for each operation, shift loops that keep every value intact, and the amortized cost of resizing.

Cisco logo
Cisco
Nov 25, 2026
mediumSoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design a growing list (a dynamic array similar to Java's ArrayList or C++'s std::vector) backed by a fixed-size internal buffer that automatically resizes when it runs out of capacity.

The list must support the standard CRUD operations: appending, inserting, reading, updating and removing elements by index. When the internal buffer becomes full on an add operation, the capacity must double so that the amortized cost of add stays O(1).

All index-based operations must validate the index and raise an IndexError when it is out of bounds.

Implement the following:

  • GrowingList(initial_capacity: int) : create an empty list whose buffer has initial_capacity slots.
  • add(value: int) : append value to the end of the list, doubling the internal capacity if it is full.
  • insert(index: int, value: int) : insert value at position index , shifting later elements one position to the right. The valid range is 0 <= index <= size . A full buffer grows the same way it does for add .
  • get(index: int) -> int : return the element at position index . Raises IndexError if out of bounds.
  • set(index: int, value: int) : replace the element at position index with value . Raises IndexError if out of bounds.
  • remove(index: int) -> int : remove and return the element at position index , shifting later elements one position to the left. Raises IndexError if out of bounds.
  • size() -> int : return the number of elements currently stored in the list.
  • capacity() -> int : return the current capacity of the internal buffer.

In the first example the buffer starts with two slots and doubles once:

Input:
["GrowingList", "add", "add", "add", "capacity", "add", "capacity", "get", "set",   "get", "size"]
[[2],           [10],  [20],  [30],  [],         [40],  [],         [2],   [1, 99], [1],   []]

Output: [null, null, null, null, 4, null, 4, 30, null, 99, 4]
lst = GrowingList(2)   # capacity = 2, size = 0
lst.add(10)            # buffer: [10, _], size = 1
lst.add(20)            # buffer: [10, 20], size = 2 (full)
lst.add(30)            # resize to 4, buffer: [10, 20, 30, _], size = 3
lst.capacity()         # returns 4 (doubled from 2)
lst.add(40)            # buffer: [10, 20, 30, 40], size = 4 (full)
lst.capacity()         # returns 4 (no resize yet)
lst.get(2)             # returns 30
lst.set(1, 99)         # buffer: [10, 99, 30, 40]
lst.get(1)             # returns 99
lst.size()             # returns 4

In the second example an insert shifts elements right and a remove shifts them back:

Input:
["GrowingList", "add", "add", "add", "insert", "get", "remove", "get", "size"]
[[4],           [1],   [2],   [3],   [1, 99],  [1],   [2],      [1],   []]

Output: [null, null, null, null, null, 99, 2, 99, 3]
lst = GrowingList(4)   # capacity = 4, size = 0
lst.add(1)             # buffer: [1, _, _, _], size = 1
lst.add(2)             # buffer: [1, 2, _, _], size = 2
lst.add(3)             # buffer: [1, 2, 3, _], size = 3
lst.insert(1, 99)      # buffer: [1, 99, 2, 3], size = 4
lst.get(1)             # returns 99
lst.remove(2)          # returns 2, buffer: [1, 99, 3, _], size = 3
lst.get(1)             # returns 99
lst.size()             # returns 3

Constraints & Assumptions

  • 1 <= initial_capacity <= 10^4
  • For get , set and remove , a valid index satisfies 0 <= index < size at the time of the call. For insert , it satisfies 0 <= index <= size . Any other index raises IndexError .
  • At most 100,000 operations are performed.
  • -10^9 <= value <= 10^9

Clarifying Questions to Ask Guidance

  • Should the buffer ever shrink after removals, and if so, what should capacity() report afterwards?
  • What should the constructor do with a capacity below 1, or with no capacity at all?
  • Should a negative index count from the end, as it does for Python lists, or is it out of bounds?
  • Will more than one thread use the same list?

What a Strong Answer Covers Guidance

  • Tracking size and capacity separately, and validating each operation against the right bound, including the extra position insert allows.
  • When add and insert trigger a resize, how elements move to the new buffer, and why doubling keeps appends cheap on average.
  • Shift loops in insert and remove that run in the correct direction over the correct range.
  • The time and space cost of every method, including memory after removals or with a large initial capacity.
  • Edge cases such as an insert at the end of a full buffer, removing the last element and operations on an empty list.

Follow-up Questions Guidance

  • How would you shrink the buffer after many removals without resizing on every alternating add and remove?
  • How would you support iteration, and what should happen if the list is modified while someone iterates over it?
  • How would you make insert(0, value) and remove(0) O(1) as well, the way a double-ended queue does?
  • What would you change to make the list safe for concurrent use?