Arrays Flashcards
time complexity of accessing an index from an array
O(1)
how is array stored in memory?
statically allocated contiguous blocks
what’s soft removal
leave the removed data in the data structure unless it’s absolutely necessary to get rid of it.
e.g. in arraylists, the end of the array is controlled by size variable, so to remove the last item we can just decrement size by 1 without explicitly removing the data at that slot.
what’s hard removal
ensure the data you removed is completely removed from the backing structure.
what is arraylist
an abstraction built upon arrays that allows dynamic allocation of space without user knowledge.
what’s the size of an arraylist
number of data (non-null) being stored in it.
what’s the capacity of an arraylist
number of data that can be stored without a resize
what’s the amortized time complexity of adding to the back of an arraylist and why
O(1)*,
because normal case is O(1)
worst case is when a resizing is needed but it happens rarely
time complexity of adding elsewhere in the arraylist
O(n),
because we must shift data around to keep contiguity of data
time complexity of removing from the back of an arraylist
O(1)
time complexity of removing anywhere but the back of an arraylist
O(n) due to shifting