queue and stack Flashcards

it includes bfs and dfs

1
Q

what is FIFO data structure

A

the first element added to the queue will be processed first

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

explain enqueue

A

new element is always added at the end of the queue

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

explain dequeue

A

only allowed to remove first element

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

drawback to naive implementation of queue using array and one pointer

A

with the movement of start pointer more and more space is wasted

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

explain circular queue

A

use a fixed sized array and two pointers to indicate starting and ending position.

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

when is queue a good choice

A

When you want to process the elements in order

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

python lib for queue

A

from queue import Queue

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

python lib for queue and stack both

A

from collections import deque

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

application of breadth first search

A

to find the shortest path from root node to target node

OR

do traversal

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

processing order of nodes in bfs

A

in the first round, we process the root node. in the second round, we process the nodes next to the root node. In the third round, we process the nodes which are two steps from the root node and so on.

Similar to tree’s level order traversal, the nodes closer to the root node will be traversed earlier.

If a node X is added to the queue in the kth round, the length of the shortest path between the root node and X is exactly k.

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

what is enqueue and dequeue order of the queue

A

we first enqueue the root node. then in each round, we process the nodes which are already in the queue one by one and add all their neighbours to the queue.

the processing order is same order as they were added to the queue, FIFO

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

Template I for bfs

returns the length of the shortest path between root and target node

A

from collections import deque

def bfs(root, target):
queue = deque()
step = 0
queue.append(root)
while queue:
size = len(queue)
for _ in range(size):
cur = queue[0]
if cur == target:
return step
for next_node in cur.neighbours:
queue.append(next_node)
queue.popleft()
step += 1
return -1

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

template II for bfs

when you don’t want to visit a node twice (think graphs with cycle)

A

from collections import deque

def bfs(root, target):
queue = deque()
visited = set()
step = 0

queue.append(root)
visited.add(root)

while queue:
    size = len(queue)
    for _ in range(size):
        cur = queue[0]
        if cur == target:
            return step
        for next_node in cur.neighbours:
            if next_node not in visited:
                queue.append(next_node)
                visited.add(next_node)
        queue.popleft()
    step += 1
return -1
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly