CodeBlanks Logo Icon CodeBlanks

Summary - Queue

Enqueue and dequeue items and print the queue in a mini task.

Summary - Queue

In this group you learned what a queue is, enqueue, dequeue, front and back. Queue follows the FIFO principle.

Queue is used in BFS, schedulers and queue systems. Enqueue is O(1), dequeue depends on implementation.

Practice Queue size

Write queue_size(queue). Return the number of elements. Print 4 with print(queue_size([1, 2, 3, 4])).

Starter code

solution.py
def queue_size(queue):
    # Size

Sample solution

solution.py
def queue_size(queue):
    # Size
    return len(queue)
print(queue_size([1, 2, 3, 4]))

Expected output

4

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Summary - Queue: match the terms

Term Meaning
enqueue Add to back
dequeue Remove from front
FIFO First in first out
BFS Graph

2 Queue principle

Question
Queue follows the ____ principle.
Answer
Queue follows the FIFO principle.

Explanation: FIFO is right.

3 Which algorithm uses a queue?

  • DFS
  • BFS Correct answer
  • Selection sort
  • Binary search

Explanation: BFS uses a queue.

All lessons in Beginner