Stacks and Queues Flashcards

1
Q

does a stack operate by lifo or fifo

A

lifo

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

does a queue operate by lifo or fifo

A

fifo

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

sample of API for stacks

A

public class Stack()

Stack() = constructor to make an empty stack
void push(int number)
int pop()
boolean isEmpty()
int size()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

where are the two locations that can the hold the top of stack

A

head

tail

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

if the top of the stack is at the head of the list, what is the running time for the push operation

A

big theta (1)

just change the next, prev and head pointers (all constant time)

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

if the top of the stack is at the head, what is the running time for push

A

big theta (1)

just move top of stack refernce

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

if the top of stack is at the bottom of the list, what is the run time of the push function

A

big theta (1)

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

if the top of the stack is at the bottom of the list, what is the run time for pop

A

big theta (N)

the temp pointer has to move from the beginning to the end to find the top of the stack

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

for maximum efficiency, where should the top of the stack be

A

the first element in the list

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

how much memory does a stack with N nodes take up

A

40N bytes

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

what does the memory for each element in a stack include

A

reference to node
reference to data in node
object overhead

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

what is loitering

A

holding a reference to an object that is no longer needed

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

when there is overflow in an array, it should be

A

resized

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

when there is underflow in an array

A

an exception must be thrown

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

why is creating a new array each time the size gets altered expenisve

A

array must be made

all elements must be copied over

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

how to avoid having to change the size of the array for every alteration

A

allow the array to have extra empty spaces

16
Q

example of resizing thresholds

A

doubles when full
halves when half full

Array is always between 25% and 100% full

17
Q

what is a priority queue

A

queue that removes the largest or smallest item with the remove function

18
Q

what is a requirement for priority queues

A

keys must be comparable

eg ints, alphabetical

19
Q

example of a use of a priority queue

A

isolating larger transactions to flag suspicious activity

eg find the M largest transactions in a list of N transactios

20
Q

Find the M largest numbers in a list of N numbers

How would this be implemented using a elementary priority queue and unsorted array

What will the run time be

A

inserting elements onto the end of the array has a constant time of 1

deleting the smallest has a run time of M

a loop containing these two will be iterated N times

Total runtime theta(NM)

21
Q

Find the M largest numbers in a list of N numbers

How would this be implemented using a elementary priority queue and sorted array

What will the run time be

A

cost of inserting into the ordered array is M
cost of removing minium element is 1
this loop will be iterated Ntime

Total is theta (NM)

22
Q

Find the M largest numbers in a list of N numbers

How would this be implemented using an array and sorting it
What will the run time be

A

load all the N numbers (big theta N)
sort all the N numbers (big theta N logN)
print the top M numbers (big theta M)

total = big theta (NlogN + M)

23
Q

use of stack example

A

runtime stack

keeps track and retraces steps to find out where error occurs

24
Q

use of queues example

A

printer

prints out what was sent to it first