Dynamic Array Data Structure - Interview Cake
Dynamic Array Data Structure - Interview Cake
0 a
1 b size: 3
2 c
capacity: 6
3
Dynamic Array
Data Structure (/data-structures-reference)
Other names:
array list, growable array, resizable array, mutable array
Quick reference
A dynamic array is an array (/concept/array) with Average Case Worst Case
a big improvement: automatic resizing.
space O(n) O(n)
One limitation of arrays is that they're xed size,
lookup O(1) O(1)
meaning you need to specify the number of
elements your array will hold ahead of time. append O(1) O(n)
Strengths:
Fast lookups. Just like arrays, retrieving the element at a given index takes O(1) time.
Variable size. You can add as many items as you want, and the dynamic array will expand
to hold them.
Cache-friendly. Just like arrays, dynamic arrays place items right next to each other in
memory, making ef cient use of caches.
Weaknesses:
Slow worst-case appends. Usually, adding a new element at the end of the dynamic array
takes O(1) time. But if the dynamic array doesn't have any room for the new item, it'll need
to expand, which takes O(n) time.
Costly inserts and deletes. Just like arrays, elements are stored adjacent to each other. So
adding or removing an item in the middle of the array requires "scooting over" other
elements (/concept/array#inserting), which takes O(n) time.
In Java
In Java, dynamic arrays are called ArrayList s.
Java
List<Integer> gasPrices = new ArrayList<>();
gasPrices.add(346);
gasPrices.add(360);
gasPrices.add(354);
We'd say this dynamic array's size is 4 and its capacity is 10. The dynamic array stores an
endIndex to keep track of where the dynamic array ends and the extra capacity begins.
Doubling Appends
What if we try to append an item but our array's capacity is already full?
To make room, dynamic arrays automatically make a new, bigger underlying array. Usually twice
as big.
Why not just extend the existing array? Because that memory might already be taken by another program.
That's the worst case. But in the best case (and the average case), appends are just O(1) time.
These two things sort of "cancel out," and we can say each append has an average cost or
amortized cost of O(1).↴
Given this, in industry we usually wave our hands and say dynamic arrays have a time cost of O(1)
for appends, even though strictly speaking that's only true for the average case or the amortized
cost.
See also:
Array (/concept/java/array)
Linked List (/concept/java/linked-list)
No prior computer science training necessary—we'll get you up to speed quickly, skipping
all the overly academic stu .
(/question/java/product-of-other-numbers)
Highest Product of 3 »
Find the highest possible product that you can get by multiplying any 3 numbers
from an input array. keep reading »
(/question/java/highest-product-of-3)
Making Change »
Write a function that will replace your role as a cashier and make everyone rich or
something. keep reading »
(/question/java/coin)
(/question/java/ nd-in-ordered-set)
Find Rotation Point »
I wanted to learn some big words to make people think I'm smart, but I messed
up. Write a function to help untangle the mess I made. keep reading »
(/question/java/ nd-rotation-point)
Reverse Words »
Write a function to reverse the word order of a string, in place. It's to decipher a
supersecret message and head o a heist. keep reading »
(/question/java/reverse-words)
Parenthesis Matching »
Write a function that nds the corresponding closing parenthesis given the
position of an opening parenthesis in a string. keep reading »
(/question/java/matching-parens)
Bracket Validator »
Write a super-simple JavaScript parser that can nd bugs in your intern's code.
keep reading »
(/question/java/bracket-validator)
Permutation Palindrome »
Check if any permutation of an input string is a palindrome. keep reading »
(/question/java/permutation-palindrome)
(/question/java/recursive-string-permutations)
Top Scores »
E ciently sort numbers in an array, where each number is below a certain
maximum. keep reading »
(/question/java/top-scores)
In-Place Shu e»
Do an in-place shu e on an array of numbers. It's trickier than you might think!
keep reading »
(/question/java/shu e)
Cafe Order Checker »
Write a function to tell us if cafe customer orders are served in the same order
they're paid for. keep reading »
(/question/java/cafe-order-checker)
(/question/java/merge-sorted-arrays)
Check out interviewcake.com for more advice, guides, and practice questions.