📖 Scenario: You are helping a small bakery manage the queue of customers waiting to buy fresh bread. 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 an array in JavaScript. You will add customers to the queue, remove them when served, and finally display the current queue.
📋 What You'll Learn
Create an array called queue with initial customers
Create a variable called newCustomer to add to the queue
Add the newCustomer to the end of the queue
Remove the first customer from the queue to simulate serving
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 where order matters.
💼 Career
Understanding queues helps in software development roles that involve managing tasks, requests, or data streams in order.
Progress0 / 4 steps
1
Create the initial queue array
Create an array called queue with these exact customers in order: 'Alice', 'Bob', 'Charlie'
DSA Javascript
Hint
Use square brackets [] to create an array and separate names with commas.
2
Add a new customer to the queue
Create a variable called newCustomer and set it to 'David'
DSA Javascript
Hint
Use const newCustomer = 'David'; to create the variable.
3
Add the new customer to the queue and serve the first customer
Add newCustomer to the end of queue using push(). Then remove the first customer from queue using shift()
DSA Javascript
Hint
Use queue.push(newCustomer); to add and queue.shift(); to remove the first customer.
4
Print the final queue
Print the queue array to show the current customers waiting
DSA Javascript
Hint
Use console.log(queue); to print the array.
Practice
(1/5)
1. What is the main characteristic of a queue data structure implemented using an array?
easy
A. It stores elements in sorted order
B. It follows Last-In-First-Out (LIFO) order
C. It follows First-In-First-Out (FIFO) order
D. It allows random access to elements
Solution
Step 1: Understand queue behavior
A queue adds elements at the end and removes from the front, so the first element added is the first removed.
Step 2: Compare with options
FIFO means First-In-First-Out, which matches queue behavior. LIFO is for stacks, random access is for arrays, and sorted order is unrelated.
Final Answer:
It follows First-In-First-Out (FIFO) order -> Option C
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 syntax to add an element `x` to the end of a queue implemented as an array named queue in JavaScript?
easy
A. queue.push(x);
B. queue.pop(x);
C. queue.shift(x);
D. queue.unshift(x);
Solution
Step 1: Identify method to add at end
In JavaScript arrays, push() adds an element to the end of the array.
Step 2: Check other methods
pop() removes from end, shift() removes from front, unshift() adds at front.
Final Answer:
queue.push(x); -> Option A
Quick Check:
push adds at end [OK]
Hint: Use push() to add at end of array [OK]
Common Mistakes:
Using pop() which removes instead of adds
Using shift() or unshift() incorrectly
Confusing push() with unshift()
3. What will be the output of the following JavaScript code implementing a queue using an array?