Day 5 - Time Complexity, Stack, and Queue
What does an activation record hold? (2)
1. A program counter 2. Data (or references) of the actual parameters and other local variables
What is an example of a use for stack data structure? (2)
1. Activation stack with activation records 2. Undo features
What are two ways a doubly linked list differs from a singly linked list?
1. Doubly linked lists have removeLast with complexity O(1) while singly linked lists have removeLast with complexity O(n). 2. Doubly linked lists require additional memory due to the extra link.
In order for a data structure to be a queue, what methods are needed? What do they do?
1. Enqueue(item): Appends an item to the queue 2. Dequeue(): Removes and returns the item at the beginning of the queue
Why might we create a circular array queue? (2)
1. Nodes require extra memory in a linked list compared to an array (memory issue) 2. If the queue is large enough, the extra space required by a linked list may cause performance issues due to a lack of ability to store the entire thing in cache
How does an activation stack work? (3)
1. When a function is called, a new activation record is pushed onto the stack 2. When that function finishes, its record is popped and a value is returned. 3. The next statement in the previous function is continued
When is the most efficient times to use an array data structure? (2)
1. When we want to update and retrieve ith element 2. When we are appending to a list of a known maximum size
When is it most efficient to use a singly linked list? (2)
1. When we will be adding elements to the beginning or end of a list a lot 2. When we will be removing elements from the beginning of a list a lot
What are the parts (attributes) of an array based queue? (5)
1. length 2. capacity 3. front 4. rear 5. array
How many bytes are required for a memory address in a typical 64-bit computer?
8
What is a queue data structure?
A structure modeling a line queue resulting in FIFO (first in first out)
In a circular array based queue, what does rear = front mean?
Depends on the context. 1. Enqueue: Full Queue 2. Dequeue: Empty Queue
How do we decide the complexity of linked list method algorithms?
Do I need to traverse the linked list (O(n)) or not (O(1))?
What does the __slots__ method do?
Forces data to be physically stored in a structure rather than stored as a reference in the structure
Pseudocode for loop has limits that are...
Inclusive
To implement a stack data structure, what base data structure is better (in terms of efficiency) and why?
Linked list is better because removeFirst and addLast have O(1) with no caveats
Array A[i] = value time complexity:
O(1)
Array A[i] time complexity:
O(1)
Singly Linked List L.addFirst(item) time complexity:
O(1)
Singly Linked List L.addLast(item) time complexity:
O(1)
Singly Linked List L.removeFirst() time complexity:
O(1)
List pop() method time complexity
O(1)*
Array A.append() time complexity:
O(1)* (O(n) when a resize is required)
Array A.insert(i, value) time complexity:
O(n)
Array A.remove(i) time complexity:
O(n)
Array A.resize() time complexity:
O(n)
Array A.search(key) time complexity:
O(n)
Singly Linked List L.insertAt(i, item) time complexity:
O(n)
Singly Linked List L.removeFrom(i, item) time complexity:
O(n)
Singly Linked List L.removeLast() time complexity:
O(n)
What is the best data structure to implement a queue? What methods of this data structure can be used to implement queue methods?
Singly Linked List Enqueue: addLast(item) Dequeue: removeFirst()
If absolutely required, how might we implement a queue using a list when we are unsure of the maximum queue size?
Use a circular array
Queue and stack can be considered... why?
Wrapper data structures because they use Array and Linked List for implementation
for i <- m to n What is the equivalent in Python?
for i in range(m, n+1)
for i <- n downto m What is the equivalent in Python?
for i in range(n, m-1, -1)