2 - Core Data Structures
arrays - runtime for access
O(1) random O(1) sequential
what is the time complexity for searching an element in a sorted array?
O(log n)
what is the time complexity for searching an element in a sorted singly linked list?
O(n)
what is the time complexity for searching an element in unsorted array?
O(n)
what is the time complexity for searching an element in unsorted singly-linked list?
O(n)
what runtime is needed to remove an element at the bottom of a stack with n elements and return the stack with all the other elements in their original order
O(n)
arrays - insertion/append
O(n) insertion O(1) amortized for vector resizing implementation
linked lists - insertion/append
O(n) insertion O(n) append O(1) with a tail pointer
linked lists - runtime for access
O(n) random O(1) sequential
arrays - bookkeeping
ptr to the beginning size or ptr to end of space used maxSize to end of allocated space
what is the additional space complexity of removing an element at the back of a queue with n elements and returning to the queue with all other elements in their original order?
O(1)
linked lists - memory
allocates memory as needed, but large memory overhead for pointers (wasteful for smaller items like primitive types)
what are the core data structures?
arrays and matrices, linked lists, stacks, queues, priority queues, binary (search) trees, heaps, hash tables, sets, graphs, union-find
array/ll: search on a sorted container
arrays can do binary search, while linked lists can't
array/ll: given an index, update an arbitrary element
arrays have random access in O(1) time using pointe arithmetic, but linked lists have O(n) search. array is the clear winner
which data structure has on average O(1) look up for key-value pairs?
hash tables
which data structure gives you O(1) lookup for extreme elements, such as minimum or maximum?
heaps/priority queues
which data structure is preferable for storing large data types that often must be inserted at random positions?
linked lists
array/ll: given a pointer to an element, insert a new element right after?
linked lists - no potential shifting of elements after insertion point
array/ll: remove multiple elements from a container?
linked lists has no potential shifting of elements after deletion point
which data structure has FIFO (first-in, first-out) property?
queue
which data structure is useful when order matters?
queue
which data structure should you select if you need to perform a breadth-first search (BFS)
queue
which data structure should you use for average O(1) search for if an element exists?
set
linked lists - bookkeeping
size (optional) head ptr to first node tail ptr to last node (optional) in each node, at least a ptr to next node with an optional prev pointer to previous node
which data structure has LIFO (last-in, first-out) property?
stack
which data structure should you select if you need to perform a depth-first search (DFS)?
stack
arrays - memory
wastes memory if size is too large, requires allocation if too small (use reserve or resize for C++ vectors)