0% found this document useful (0 votes)
231 views9 pages

Dynamic Array Data Structure - Interview Cake

A dynamic array is a data structure that automatically resizes itself as elements are added or removed. It provides fast lookups like a regular array but can grow in size dynamically. While appending an element takes on average O(1) time, in the worst case of needing to resize it takes O(n) time due to needing to copy all existing elements to a new larger array. Dynamic arrays are commonly used in programming languages like Java's ArrayList class.

Uploaded by

RonaldMartinez
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
231 views9 pages

Dynamic Array Data Structure - Interview Cake

A dynamic array is a data structure that automatically resizes itself as elements are added or removed. It provides fast lookups like a regular array but can grow in size dynamically. While appending an element takes on average O(1) time, in the worst case of needing to resize it takes O(n) time due to needing to copy all existing elements to a new larger array. Dynamic arrays are commonly used in programming languages like Java's ArrayList class.

Uploaded by

RonaldMartinez
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 9

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)

insert O(n) O(n)


A dynamic array expands as you add more
elements. So you don't need to determine the size delete O(n) O(n)
ahead of time.

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.

Here's what they look like:

Java
List<Integer> gasPrices = new ArrayList<>();

gasPrices.add(346);

gasPrices.add(360);
gasPrices.add(354);

Size vs. Capacity


When you allocate a dynamic array, your dynamic array implementation makes an underlying
xed-size array. The starting size depends on the implementation—let's say our implementation
uses 10 indices. Now say we append 4 items to our dynamic array. At this point, our dynamic
array has a length of 4. But the underlying array has a length of 10.

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.

Each item has to be individually copied into the new array.


Copying each item over costs O(n) time! So whenever appending an item to our dynamic array
forces us to make a new double-size underlying array, that append takes O(n) time.

That's the worst case. But in the best case (and the average case), appends are just O(1) time.

Amortized cost of appending


The time cost of each special O(n) "doubling append" doubles each time.
At the same time, the number of O(1) appends you get until the next doubling append also
doubles.

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)

Interview coming up?


Get the free 7-day email crash course. You'll learn how to think algorithmically, so you can
break down tricky coding interview questions.

No prior computer science training necessary—we'll get you up to speed quickly, skipping
all the overly academic stu .

No spam. One-click unsubscribe whenever.

me@gmail.com Get the rst day now!

Dynamic Array Coding Interview Questions


Apple Stocks »
Figure out the optimal buy and sell time for a given stock, given its prices
yesterday. keep reading »
(/question/java/stock-price)

Product of All Other Numbers »


For each number in an array, nd the product of all the other numbers. You can
do it faster than you'd think! keep reading »

(/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)

Find in Ordered Set »


Given an array of numbers in sorted order, how quickly could we check if a given
number is present in the array? keep reading »

(/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 String in Place »


Write a function to reverse a string in place. keep reading »
(/question/java/reverse-string-in-place)

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)

Recursive String Permutations »


Write a recursive function of generating all permutations of an input string.
keep reading »

(/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)

Which Appears Twice »


Find the repeat number in an array of numbers. Optimize for runtime.
keep reading »
(/question/java/which-appears-twice)

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)

Merge Sorted Arrays »


Write a function for consolidating cookie orders and taking over the world.
keep reading »

(/question/java/merge-sorted-arrays)

All Questions  (/all-questions)

Want more coding interview help?

Check out interviewcake.com for more advice, guides, and practice questions.

You might also like