📖 Scenario: You are managing a line of customers waiting to buy tickets at a counter. Each new customer joins the end of the line. This line is called a queue.
🎯 Goal: Build a simple queue using an array and add new customers to the end using the enqueue operation.
📋 What You'll Learn
Create an array called queue with initial customers
Create a variable called newCustomer to hold the name of the customer to add
Write code to add newCustomer to the end of queue using the enqueue operation
Print the final state of queue after adding the new customer
💡 Why This Matters
🌍 Real World
Queues are used in real life to manage lines, like customers waiting for service or tasks waiting to be processed.
💼 Career
Understanding queues and enqueue operations is important for software developers working on scheduling, task management, and real-time systems.
Progress0 / 4 steps
1
Create the initial queue
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 name
Create a variable called newCustomer and set it to the string 'David'
DSA Javascript
Hint
Use const newCustomer = 'David'; to create the variable.
3
Enqueue the new customer
Use the push method on queue to add newCustomer to the end of the array
DSA Javascript
Hint
Use queue.push(newCustomer); to add the new customer at the end.
4
Print the final queue
Print the queue array to show the final order of customers after adding newCustomer
DSA Javascript
Hint
Use console.log(queue); to print the array.
Practice
(1/5)
1. What does the enqueue operation do in a queue data structure?
easy
A. Adds an element to the end of the queue
B. Removes an element from the front of the queue
C. Checks if the queue is empty
D. Returns the size of the queue
Solution
Step 1: Understand the queue behavior
A queue works like a line where new people join at the end.
Step 2: Define enqueue operation
Enqueue means adding a new element at the end of the queue.
Final Answer:
Adds an element to the end of the queue -> Option A
Quick Check:
Enqueue = Add at end [OK]
Hint: Enqueue always adds at the back of the queue [OK]
Common Mistakes:
Confusing enqueue with dequeue
Thinking enqueue removes elements
Mixing enqueue with checking size
2. Which of the following is the correct syntax to add an element 5 to a queue implemented as an array named queue in JavaScript?
easy
A. queue.enqueue(5);
B. queue.add(5);
C. queue.push(5);
D. queue.insert(5);
Solution
Step 1: Identify JavaScript array method for adding at end
JavaScript arrays use push() to add elements at the end.
Step 2: Match method to enqueue operation
Enqueue adds to the end, so queue.push(5); is correct syntax.
Final Answer:
queue.push(5); -> Option C
Quick Check:
Array push adds at end [OK]
Hint: Use push() to enqueue in array-based queue [OK]
Common Mistakes:
Using non-existent methods like add() or insert()
Confusing enqueue with dequeue methods
Trying to use enqueue() directly on array
3. What will be the output of the following JavaScript code?
B. Using unshift adds element at the front, not the end
C. Array should be initialized as null, not []
D. Missing return statement in enqueue method
Solution
Step 1: Understand unshift behavior
unshift adds element at the start (front) of the array, not the end.
Step 2: Compare with enqueue operation
Enqueue must add element at the end, so unshift is incorrect here.
Final Answer:
Using unshift adds element at the front, not the end -> Option B
Quick Check:
Enqueue adds at end, unshift adds at front [OK]
Hint: Use push() for enqueue, not unshift() [OK]
Common Mistakes:
Confusing unshift with push
Thinking unshift adds at end
Ignoring method behavior differences
5. You have a queue implemented with an array. You want to enqueue multiple elements [7, 8, 9] at once while preserving their order. Which code snippet correctly enqueues all elements in order?
hard
A. queue.unshift(7, 8, 9);
B. queue.concat([7, 8, 9]);
C. queue.push([7, 8, 9]);
D. [7, 8, 9].forEach(e => queue.push(e));
Solution
Step 1: Analyze each option for enqueue behavior
queue.push([7, 8, 9]); adds the entire array as one element, not individual elements. queue.concat([7, 8, 9]); creates a new array but does not modify queue. [7, 8, 9].forEach(e => queue.push(e)); pushes each element individually, preserving order. queue.unshift(7, 8, 9); adds elements at front, reversing order.
Step 2: Choose the method that enqueues elements individually at the end
Using forEach with push adds each element in order at the end of queue.
Final Answer:
[7, 8, 9].forEach(e => queue.push(e)); -> Option D
Quick Check:
forEach + push enqueues elements in order [OK]
Hint: Use forEach with push to enqueue multiple elements in order [OK]
Common Mistakes:
Using push with array adds nested array
Using unshift reverses order
Forgetting to assign concat result back to queue
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.