Vectors, Stacks & Queues Flashcards

1
Q

What is a Vector in ADE?

A

An Abstract Data Type corresponding to generalising the notion of the ‘array’ CDT
Based off of the array CDT, and stores a sequence of arbitrary objects

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a common usage of Vector? How does this work briefly?

A

Using it as a stack
Add and remove elements at the end i.e. maximum rank values
push(o) = insert after all the existing ones
pop() = remove the element that is located after all the existing ones

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How does an array-based Vector work?

A

Use an array V of size N as the CDT
A variable n keeps track of the size of the vector (number of elements currently stored)
Operation elementAtRank(r) is O(1) time by returning V[r]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the time complexity of an insertion into an array-based Vector and how does it work?

A

Worst time complexity - O(n)
Shifts all the elements after the specified point forward another space, so as to make room for the new element

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the time complexity of deletion in an array-based Vector and how does it work?

A

Worst case - O(n)
Shifts all the elements back a space that are after the specified point so as to ‘fill the hole’, thereby removing the redundant element

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the time complexities for the space used by the data structure, the methods size(), isEmpty(), insertAtRank and removeAtRank, as well as push and pop?

A

Data structure - O(n)
size, isEmpty - O(1)
insertAtRank, removeAtRank - O(n)
push and pop - O(1) (unless needing to resize the array with push, in which case, this will change to O(n))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How would you go about increasing the size of an array-based Vector?

A

Incremental strategy - increase size by a constant c
Doubling strategy - double the size

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the difference between amortised analysis and average case analysis?

A

Amortised - real sequence of dependent operations
Average - set of possibly independent operations

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the amortised time of a push operation within an incremental strategy?

A

Amortised time of a push operation - O(n)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How does the incremental strategy work?

A

Replace the array k = n/c times (c being the length added to the array, and n being the number of elements
T(n) is O(n + k^2) i.e. O(n^2)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How does the doubling strategy work?

A

When the array needs to be extended, takes the current length of the array and then doubles it. Afterwards, transfers over all the old elements.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the amortised cost of the doubling strategy?

A

Gives an average of O(1) per push operation, as the overall amount of pushes performed at O(1) vastly outweigh those of O(n)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the stack ADT and how does it work?

A

Stores arbitrary references to objects
Insertions and deletions follow last-in-first-out (LIFO)
Main stack operations:
push(object) - inserts an element
pop(object) - removes and returns the last inserted element
Other elements are not accessible

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a common implementation of a stack CDT?

A

Using it within an array
Add elements from left to right, and a variable (t) keeps track of the index of the top element
If trying to push to an already full array, will throw an exception

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the performance of an array-based Stack and the limitations?

A

The space used is O(n)
Each operation runs in O(1)
Limitations:
Maximum size of the array-based Stack must be defined in advance and cannot be changed dynamically
Trying to push a new element into a full stack causes an implementation-specific exception

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the Queue ADT and how does it work?

A

Stores arbitrary objects
Insertions and deletions follow the first-in-first-out (FIFO) scheme
Insertions are at the rear (tail) of the queue, and removals are at the front (head) of the queue

17
Q

How does a queue work when using an array?

A

Use an array of size N in a circular ‘fashion’
Two variables keep track of the front and rear:
f - index of the front element
r - index immediately past the rear element
Array location r is kept empty

18
Q

What are the differences between ‘narrow’ and ‘wide’ ADTs?

A

Narrow:
Stack ADT is one of these
Less flexible to use, but more flexible to implement, hence maybe more efficient
Wide:
Java Stack
More flexible to use
Possibly more difficult to implement efficiently

19
Q

How does a stack with a singly linked list work?

A

Top element is stored at the first node of the list
The space used is O(n) and each operation of the stack ADT takes O(1) time

20
Q

How does a queue work as a singly linked list?

A

Needs to keep track of both the head and the tail
New arrivals for the queue are placed at the tail and deletions are made at the head
Complexities of operations are:
Inserting at the tail - O(1)
Removing at the head - O(n)

21
Q

What are the pros and cons for using linked list CDTs vs array-based CDTs?

A

Array:
Con - fixed size, might need a lot of unused space
Pro - contiguous in memory
Linked list:
pro - size grows and shrinks automatically
Cons - can be scattered around memory, and give poor usage of the cache, as well as storage of the ‘next’ reference doubles the space usage