Quick and Dirty Notes / Mini-Quiz on Arrays
By: Janu Sung
Tags: Algorithms, Datastrucutre, Arrays

A quick cheat sheet / mini-quiz on Arrays to brush up on the fundamentals.

This blog post will be formatted with a question and then a corresponding answer. I find that having a question posed can help solidify my ability to recall information as well as force me to have a better understanding of the topic. See if it does the same for you.

As you go through the blog try to answer the questions in your own words before looking at the provided answer.

Describe an Array in as many ways as possible.

A collection of items of the same data type(typically).

Can be multiple dimensions.

Can be Static or Dynamic.

Organizes items sequentially & Stores data contiguously (in order) in memory. Therefore has the smallest footprint of any data structure.

Has an index identifying the location of each piece of data.

What are common operations for an array?

Lookup

Push

Insert

Delete

What is the bigO for the above array operations? Can you list some more than what's above?

lookup →O(1) → constant time

push → O(1) → constant time → unless increasing size of the array → then O(n) due to copying/relocation of data

insert → O(n) → linear time

delete → O(n) → linear time

search → O(n) → linear time

*pop →O(1) — -> constant time — -> can you explain why?

Why is insert and delete Linear time for arrays?

Because whenever an element is inserted or deleted, the array needs to reassign the index for every value following it. Therefore the array needs to find the index for insertion or deletion, then reassign the index for the following values which requires touching the remaining fraction of the array → remaining fraction 1/x of the array → O(n * 1/x) → which evaluates to O(n).

e.g.

let arr = [1, 2, 3, 4, 5, 6]

lets say we insert 10 at index 3 to make → arr = [1, 2, 3, 10, 4, 5, 6]

When we insert at index 3, set the value 10 to that index. Therefore all the following values need to be reassigned to their index+1. So in this case, we still need to touch 3 elements within the array → 3 = 1/2 of the original array’s length → which means our bigO is O(n*1/2) which evaluates to O(n).

What’s the bigO for Array.pop()?

*pop → O(1) — -> constant time — -> because it goes directly to the last index and does not need to reassign the indices of the elements preceding it or after it (because it's the last index).

Where is the data within an array stored?

Random Access Memory (RAM) → Contiguous memory.

What are the strengths/weaknesses of an array?

Fast lookup. Fast .push(). Fast .pop(). → O(1) → Constant Time

Slower insert (.splice()). Slower delete (also splice). Slower unshift(). Slower search. → O(n) → Linear Time

What's the difference between Static Arrays vs. Dynamic Arrays?

Static array sizes are decided in advance.

Dynamic arrays grow/shrink accordingly.

How are Dynamic Arrays resized?

Dynamic arrays allow you to copy and rebuild the array at a new location if more size is required. They will copy, then move to a space in memory with typically twice the amount of the initial space.

*This copying requires a loop, meaning the increase of size will create an instance where .push() will be of O(n) instead of O(1)

*In Javascript and Python arrays are treated as dynamic arrays. Higher-level languages extrapolate this. While lower-level languages that are more concerned with memory allow you to set the size (static array).

How would you build an array?

Depending on the language you’re most familiar with the answer may vary. But take some time to walk through a solution. Consider what operations are inherent to arrays, and try writing out a class definition for an array in pseudo-code (or actual code) and see how well you understand it.

That's it for this mini-quiz/cheatsheet. Hope it tickled your brain and you had some fun!