0% found this document useful (0 votes)
3 views

??? ?? ??????? ?????? ????

Dynamic arrays are resizable arrays that can expand their capacity when full, allowing for more elements to be inserted. When the array reaches its capacity, it doubles in size to accommodate new insertions, which helps maintain an amortized O(1) insertion time. Key operations include pushback, popback, and resizing, which involves allocating a new array and copying existing elements.

Uploaded by

Mehul Thuletiya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

??? ?? ??????? ?????? ????

Dynamic arrays are resizable arrays that can expand their capacity when full, allowing for more elements to be inserted. When the array reaches its capacity, it doubles in size to accommodate new insertions, which helps maintain an amortized O(1) insertion time. Key operations include pushback, popback, and resizing, which involves allocating a new array and copying existing elements.

Uploaded by

Mehul Thuletiya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

1+2+4+8+⋯+n

How do
data structures: a deep dive

dynamic
arrays
work?
1 2 3 4
1 2 3 4
Find more
interview tips ->
neetcode.io
Overview
Dynamic arrays are arrays that can be resized.

These arrays can occupy more space in memory depending on the


number of elements inserted.

Most languages have a default size for dynamic arrays. For example,
Java has a default size of 10 and C++ has a default size of 4 upon
initialization.

At a glance, dynamic arrays have the following operations:

pushback(n) - Insert n in the last position of the array


resize() - insert at next empty position
popback() - Remove the last element in the array
get() - Get value at i-th index
insert() - Insert n at i-th index

Find more
interview tips ->
neetcode.io
Insertion
With a static array, when the array becomes full, we can no longer
insert elements. With dynamic arrays, as soon as the length of the
array reaches the capacity, the size of the array doubles to
accommodate new insertions.

Length refers to the number of actual elements in the array.

Capacity refers to the number of elements that the array can hold.

length = 3
capacity = 3
New insertion

1 2 3

1 2 3 4

Find more
interview tips ->
neetcode.io
Resizing
Resizing is a key feature of dynamic arrays, making them "dynamic."

How do we resize
Allocate a new array with twice the original capacity
Copy the elements from the old array to the new one
Update the reference to point to the new array.

Initialize a new array, 2x the capacity

capacity = 6
length = 0

capacity = 3
1 2 3 length = 3

capacity = 6
1 2 3 length = 3
Copy the elements

to the new array


Find more
interview tips ->
neetcode.io
Why double the capacity?
Resizing the array every time a new element is inserted would make
the cost of insertion O(n) per operation, because
Resizing the array: O(n) (since all existing elements need to be
copied to the new array
Inserting into the newly resized array: O(1) (a simple append
operation)

To reduce each operation to amortized O(1), we can double the size


of the array when it becomes full.

1+2+4+8+⋯+n
The largest resize accounts
for more work than all
previous resizes combined.

Doubling the array size reduces the frequency of O(n) resizes,

spreading the cost across multiple insertions. This approach strikes a

balance between time and space.

Find more
interview tips ->
neetcode.io
Join a million people
preparing for coding
interviews

neetcode.io

You might also like