Data Structures and Abstract Data Types Flashcards
What is the time complexity for hash table lookup?
O(1)
Name 2 collision resolution methods
closed addressing (linked list at location), open addressing (new location)
What is the time complexity for linked list insertion and deletion? (Assuming the index for insertion/deletion is already known)
O(1)
What is the time complexity for linked list search?
O(n)
State three features of a good hashing algorithm
- The hash function uses all the input data
- The hash function uniformly distributes the data across the entire set of possible hash values
- The hash function generates very different hash values for similar (but non-identical) strings
Name 2 Abstract Data Types.
Stack and Queue
How does a stack work?
Elements are pushed onto the stack, or popped from the stack.
The first element pushed onto the stack is the last element popped from the stack (FILO)
How does a queue work?
Elements are enqueued into the queue, or dequeued from the queue.
The first element enqueued is also the first element dequeued (FIFO)
What methods are required for a Queue ADT?
Enqueue: add an element to the queue
Dequeue: get the next element from the queue
What methods are required for a Stack ADT?
Push: add an element to the stack
Pop: get the next element from the stack
What are the key features of a Circular Queue?
Circular queues have a fixed capacity.
The last location in the array wraps around to the first location.
What does a hash function do in a hash table?
Hash function takes an input key and returns a location/index of fixed size. Each unique key hashes to the same location/index.
static vs dynamic memory allocation
Static allocation pre allocates fixed amount of memory upfront … / has constant access time …
1. predetermined data format (element size is calculated)
2. constant access
3. fixed size
while dynamic allocation requests memory on an ad-hoc basis / only when needed / requires traversal to reach further nodes
1. flexible data format
2. no constant time access
3. dynamic size
BST vs Hash table (5 criteria)
- sorting: In order traversal of BST allows us to display items in incremental order
- searching for closest value: BST allows us to search for the key that has the closest value to target
- memory efficient: BST requires the exact amount of memory for its nodes
- time complexity of searching : hash table O(1), BST: O(logn)
- worse time complexity of searching:
- hash table (collision O(n)) -> choose appropriate size table
- BST (unbalanced O(n)) -> self balancing BST every time there is a change made
state the differences between circular queue and linear queue
- space utilisation
circular queue efficiently utilises available space by reusing positions that become free whereas a linear queue may leave unused spaces at the beginning as elements are dequeued
(linear= less efficient use of memory) - wrapping
in a circular queue, the last position is connected back to the first position, forming a loop
in a linear queue elements only move in a straight line from front to back