📖 Scenario: You are helping a small bakery manage the queue of customers waiting to place their orders. The bakery wants to keep track of customers in the order they arrive and serve them one by one.
🎯 Goal: Build a simple queue using a Python list (array) to add customers to the end and remove them from the front, simulating the real-life queue at the bakery.
📋 What You'll Learn
Create an empty list called queue to hold customer names.
Create a variable called max_size to limit the queue size to 5.
Write code to add a customer named 'Alice' to the queue if there is space.
Write code to remove the first customer from the queue to simulate serving them.
Print the final state of the queue.
💡 Why This Matters
🌍 Real World
Queues are used in many places like customer service lines, printer job management, and task scheduling.
💼 Career
Understanding queues helps in software development roles that involve managing tasks, requests, or data streams in order.
Progress0 / 4 steps
1
Create an empty queue
Create an empty list called queue to hold customer names.
DSA Python
Hint
Use [] to create an empty list and assign it to queue.
2
Set the maximum queue size
Create a variable called max_size and set it to 5 to limit the queue size.
DSA Python
Hint
Just assign the number 5 to the variable max_size.
3
Add and remove customers in the queue
Add the customer 'Alice' to the end of the queue only if the length of queue is less than max_size. Then remove the first customer from the queue to simulate serving them.
DSA Python
Hint
Use len(queue) to check size, append() to add, and pop(0) to remove the first item.
4
Print the final queue
Print the final state of the queue.
DSA Python
Hint
Use print(queue) to show the queue contents.
Practice
(1/5)
1. What is the main characteristic of a queue implemented using an array?
easy
A. It follows First-In-First-Out (FIFO) order
B. It follows Last-In-First-Out (LIFO) order
C. It allows random access to elements
D. It automatically sorts elements
Solution
Step 1: Understand queue behavior
A queue adds elements at the rear and removes from the front, following FIFO.
Step 2: Compare options
LIFO is for stacks, random access and sorting are not queue properties.
Final Answer:
It follows First-In-First-Out (FIFO) order -> Option A
Quick Check:
Queue = FIFO [OK]
Hint: Queue always removes the oldest element first [OK]
Common Mistakes:
Confusing queue with stack (LIFO)
Thinking queue allows random access
Assuming queue sorts elements automatically
2. Which of the following is the correct way to initialize an empty queue using an array in Python?
easy
A. queue = ()
B. queue = {}
C. queue = set()
D. queue = []
Solution
Step 1: Identify array type in Python
Python lists are used as arrays and initialized with [] for empty list.
Step 2: Check other options
{} is dict, () is tuple, set() is a set, none are arrays.
Final Answer:
queue = [] -> Option D
Quick Check:
Empty array = [] [OK]
Hint: Use [] to create empty list (array) in Python [OK]
pop() without index removes last element, not front as queue requires.
Step 2: Check queue correctness
Queue must remove front element, so pop(0) should be used instead of pop().
Final Answer:
pop() removes last element, not front -> Option A
Quick Check:
Queue removal = pop(0) front [OK]
Hint: Use pop(0) to dequeue from front [OK]
Common Mistakes:
Using pop() removes last, not front
Confusing append usage
Thinking queue is a dictionary
5. Given a fixed-size array of length 3, what happens if you try to enqueue 4 elements in a queue implemented using this array without resizing or circular logic?
hard
A. The queue automatically resizes to fit 4 elements
B. The 4th element replaces the first element
C. The queue overflows and cannot add the 4th element
D. The queue removes the last element to add the 4th
Solution
Step 1: Understand fixed-size array limits
An array of length 3 can hold only 3 elements; no extra space for 4th.
Step 2: Check enqueue behavior without resizing
Without resizing or circular logic, adding 4th element causes overflow error or failure.
Final Answer:
The queue overflows and cannot add the 4th element -> Option C