Stacks and Queues Flashcards

1
Q

What’s the order of inserts/deletes for stacks?

A

Last in, first out

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

What’s the order of inserts/deletes for queues?

A

First in, first out

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

Time complexity of push in to stack?

A

O(1)

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

Time complexity of pop from stack?

A

O(1)

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

How do you return the top of the stack without popping it?

A

Peek

stack[-1]

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

What is a common application for using a stack?

A

For parsing

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

What built-in can you use to implement a stack?

A

List

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

How do you push an element in to a stack built using a list?

A

stack.append(e)

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

How do you retrieve/peek at the top element of a stack?

A

stack[-1]

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

How do you remove and return the top element of a stack?

A

stack.pop()

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

How do you test if a stack is empty?

A

len(stack) == 0

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

What operations does a queue support?

A

enqueue and dequeue

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

What data structure can be used to implement a queue?

A

Linked list

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

Time complexity for enqueue?

A

O(1) if we have a pointer to the tail

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

Time complexity for dequeue?

A

O(1) if we have a pointer to the head

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

How is a deque (double-ended queue) implemented?

A

Doubly linked list

17
Q

Where can you insert or delete from a deque?

A

At the head or the tail of the linkedlist

18
Q

What Python built-in can you use to create a queue?

A

collections.deque

from collections impor deque

q = deque()

19
Q

How do you append to a queue?

A

q.append(e)

20
Q

How do you retrieve, but not remove, the element at the front of the queue?

21
Q

How do you remove and return the element at the front of the queue?

A

q.popleft()

22
Q

What should you always check before q.popleft() (dequeueing)?

A

that q is not empty

if q

23
Q

When should you consider a queue?

A

When order matters (ex: “buildings with an ocean view”)

24
Q

How do you implement stack with a linkedlist?

A

Have a pointer to the head/top

Add and remove items from the same side (where the pointer points to)

25
How do you add items to a stack implemented as a linkedlist?
new\_node.next = top # "shifting" the old top further down stack top = new\_node # setting the new node as the top of stack
26
How do you pop from a stack implemented as a linked list
item = top.data top = top.next # nothing is pointing to the 'old' top and will be garbage collected return item
27
What data structure can be used to implement a recursive algorithm iteratively?
Stack
28
What's the difference between pointers used in linkedlists for stacks and queues?
Stack only needs pointer to the head/top Queue needs pointer to the head and tail
29
What type of data structure to use for BFS?
Queue
30
What type of data structure to use for DFS?
Stack