Data Structures and Data Types Primer Flashcards
How are lists stored in memory in python? When is memory reallocated?
Lists are actually variable-length arrays in Python, not linked lists. Internally they are stored as pointers to elements.
Append: When another element is appended, the list is resized to allocate additional space if needed. In order to avoid costly memory allocation operations, initially more memory is allocated.
Pop: When the last element is popped (removed), if the current list size is less than half of allocated memory, the allocated memory is shrinked.
What are the rules for Big O? (4)
- Add steps
- Don’t include constants because they’re not significant enough
- Different steps get different variables in the O equation
- Drop non-dominant terms
What’s Big O notation?
Shows how time scales with respect to some input variables
For lists, what are the operations you can do and what’s their big O notation? (3)
- Append (add): the list is resized to allocate additional space (i.e. more than is required for just the element(s) being appended) O(n)
- Pop (remove): When the last element is popped (removed), if the current list size is less than half of allocated memory, the allocated memory is shrinked. O(n)
- Accessing: Any element can be directly accessed by its index by calculating its pointer in memory using the offset from pointer to initial element, which makes accessing an O(1) operation
Describe Tuples
- Tuples are immutable, which means that they can’t be changed.
- Therefore, they are hashable and can be used as keys in dictionaries
What’s good about linked lists?
Adding new elements is less expensive than with arrays (aka lists).
How are linked lists structured?
- Each element has a pointer to the next element
- For double linked lists: Stores pointers to each the last and next elements.
Describe the operations performed on linked lists and their Big O notation
- Adding to end: Need to add to the last element in the list a link to the new element.
-
Inserting at position i:
- Traverse to element before where you want to insert.
- Update that element’s pointer to point to the inserted element.
- Update inserted element’s pointer to the element that comes after
- Delete at position i: the element at position i-1 needs to be updated to maintain the link to element i+1
- Accessing element at index i: Traverse all elements until you get the desired element.
access and search are O(n) operations and adding and removing elements (to and from the end) is just O(1) operation
What’s one interesting thing about traversing a linked list
- Simple linked lists can only be traversed in the forward direction.
- Double linked lists can be traversed in either direction
Other name for linked list
queue
Library you can use for a linked list in python
from collections import deque
What are sets?
- Unordered
- Has only unique elements
set_example = {1,2,3}
Big O for checking if element exists in a set
O(1) on average and O(n) in worst case
What are sets best for?
For operations that require checking whether some value is in some set of values
What’s the big O of dictionary operations?
getting, adding and deleting an item: O(1)