Time Complexity of Data Structures Flashcards
1
Q
Dynamic Arrays
What is the time complexity for:
- Adding
- Removing
- Replacing
- Accessing
- Traversing
A
- Adding
-
O(n)
beginning -
O(1)+
end
-
- Removing
-
O(n)
beginning -
O(1)+
end
-
- Replacing
O(1)
- Accessing
O(1)
- Traversing
O(n)
2
Q
Hash Maps
What is the time complexity for:
- Adding
- Removing
- Replacing
- Accessing
- Traversing
A
- Adding
-
O(1)+
anywhere
-
- Removing
-
O(1)+
anywhere
-
- Replacing
O(1)+
- Accessing
-
O(1)+
by key -
O(n)
by value
-
- Traversing
O(n)
3
Q
Singly Linked Lists
What is the Big O for:
- Insert
- Delete
- Access
A
- Inserting at the beginning = O(1)
Inserting in between = O(n)
Inserting at the end = O(1)
Replacing in between = O(n)
Replacing at the beginning or at the end = O(1) - Deleting at the beginning = O(1)
Deleting in between = O(n)
Deleting at the end = O(n) - Access at the beginning = O(1)
Access in between = O(n)
Access at the end = O(1)
4
Q
Stacks
What is the Big O for:
- Insert
- Delete
- Access
A
- Inserting = O(1)
- Deleting = O(1)
- Access in between = O(n)
Access at the top = O(1)
Assumes Doubly Linked List implementation
5
Q
Queues
What is the Big O for:
- Insert
- Delete
- Access
A
- Inserting = O(1)
- Deleting = O(1)
- Access at the beginning = O(1)
Access in between = O(n)
Assumes Doubly Linked List implementation
6
Q
Binary Search Tree
What is the Big O for:
- Insertion
- Deletion
- Access
- Traversal
- Search
A
If balanced, O(log(n)) for all operations (except traversal)
If unbalanced, O(n) for all operations
Traversal = O(n)
7
Q
What are the 6 general data structure operations?
A
- Insert
- Delete
- Access
- Traverse
- Search
- Merge
8
Q
Strings
What is the time complexity for:
1. Adding
2. Removing
3. Accessing
A
-
O(n)
beginning middle end -
O(n)
biginning middle end O(1)