Data Structures Flashcards
Explain the difference between Stacks and Queues
Stacks VS Queues:
Stacks:
- Can be built with Arrays and Linked Lists
- Arrays allow cache locality, which makes it technically faster since their items are right next to each other in memory
- Linked lists items are scattered all over memory and have extra memory onto them
Queues:
- Never build with an array ONLY LINKED LISTS
- Because we would have to shift the indexes everytime
What are Queues and what they can be used for?
Queues (FIFO):
- lookup O(n)
- enqueue O(n) – aka push aka add to the queue
- dequeue O(1) – aka pop aka remove the first person in line
- peek O(1) – whats the first item that’ll come up?
- First one in, first out (ROLLERCOASTER LINE)
- Used for:
—- if you had any waitlist to buy tix for a concert, reservations, uber, lyft
—- a printer
Should you use an array to build a Queue?
Never use an ARRAY to build a QUEUE
- its inefficient
- removing the first item, so if you use an array you will have to shift the indexes
What is a Stack and what can it be used for?
STACK (LIFO):
- lookup O(n)
- pop O(1)
- push O(1)
- peek O(1) – view the top plate
- Last one in, first one out
- Useful for:
- — Need to know the very last value that was added
What are the pros and cons for Linked Lists?
LINKED LISTS
GOOD:
- Fast Insertion
- Fast Deletion
- Ordered
- Flexible Size
BAD:
- Slow lookup
- More memory
Difference between a Singly vs Doubly Linked list?
Singly vs Doubly (When to use)
- Singly
— simple implementation
— less memory
– Cannot traverse from back to front
Good option when:
- Less memory or memory is expensive
- Want to do fast insertion and deletion
Doubly
- Can traverse the list
- If you need to delete a previous node, you don’t need to traverse from the head node and find what the previous node is
- Requires more memory in storage
What is good about a doubly linked list?
Doubly Linked list
- Allows us to traverse our list forwards/backwards
What is a pointer?
Pointer is just a reference to another variable.
const obj1 = { a: true }
const obj2 = obj1;
both point to the same thing in memory
What is a linked list and some of its qualities?
Definition of Linked List:
Linked lists are data structures that represent a series of nodes where each node contains two pieces of information: the value of the node and a pointer/reference to the next node in the list. The beginning of the list is called the head and the node at the end of the list is referred to as the tail, which points to the empty value; null.
notes
- nodes are scattered
- iterating through it is slower
- INSERTing into it is a lot better (no shifting of the arrays)
- you can sort data
- prepend O(1)
- append O(1)
- lookup O(n) aka Traversal - we have to start from Head
- insert O(n) - Have to go one by one to find the index and insert there
- delete O(n) - Have to find the items
What is a tradeoff for Hash tables?
Tradeoffs of Hash tables
- Fast (better time complexity) but adds more memory aka Space complexity
What are some differences between Hash tables and Arrays?
Hash tables vs Arrays
Hash tables
- when u want quick access to a certain item [search O(1)]
- no concept of order
- flexible keys
- fast inserts
- good collision resolution needed***
Bad: unordered and slow key iteration
Arrays
- not so quick for search O(n)
- each item is placed on a shelf next to each other in memory
Difference between a Map vs. Object?
Map vs. Object
- Map allows you to store any type of data as a key
- Object only allows strings as keys
What is the Big O of
‘asdfasfasfasfdasdfa’.length?
Depends on the language you are using. JavaScript has .length built in to each string.. so its a property of an object. so in JS it’ll be O(1) constant time
What adds to Space Complexity?
For space complexity, we don’t care about the input size.
What takes up space?
- Variables
- Data Structures ( like creating a new array and adding items to it)
- function calls
- allocations
What to consider when thinking of Space Complexity?
Space Complexity:
- Heap - store variables
- Stack - keep track of functions calls