📖 Scenario: Imagine you are managing a line at a coffee shop and a stack of plates in the kitchen. You want to understand how to organize these two situations using programming concepts called Queue and Stack.
🎯 Goal: You will create two data structures: a queue to represent customers waiting in line (first come, first served), and a stack to represent plates stacked on top of each other (last in, first out). You will then simulate adding and removing items from both to see how they behave differently.
📋 What You'll Learn
Create a queue array with exact customers: 'Alice', 'Bob', 'Charlie'
Create a stack array with exact plates: 'Plate1', 'Plate2', 'Plate3'
Add a new customer 'Diana' to the queue
Add a new plate 'Plate4' to the stack
Remove one customer from the queue (the one who came first)
Remove one plate from the stack (the one on top)
Print the final state of both queue and stack
💡 Why This Matters
🌍 Real World
Queues are used in places like lines at stores or print jobs waiting to print. Stacks are used in undo features or keeping track of tasks in reverse order.
💼 Career
Understanding queues and stacks helps in software development, especially in managing tasks, processes, and data flow efficiently.
Progress0 / 4 steps
1
Create initial Queue and Stack
Create an array called queue with these exact customers: 'Alice', 'Bob', 'Charlie'. Also create an array called stack with these exact plates: 'Plate1', 'Plate2', 'Plate3'.
DSA Javascript
Hint
Use const queue = [...] and const stack = [...] with the exact values inside square brackets.
2
Add new customer and plate
Add a new customer 'Diana' to the end of the queue using push. Add a new plate 'Plate4' to the top of the stack using push.
DSA Javascript
Hint
Use queue.push('Diana') and stack.push('Plate4') to add items at the end/top.
3
Remove one customer and one plate
Remove one customer from the front of the queue using shift(). Remove one plate from the top of the stack using pop().
DSA Javascript
Hint
Use queue.shift() to remove the first customer and stack.pop() to remove the top plate.
4
Print final Queue and Stack
Print the final state of queue and stack using console.log. Print queue first, then stack.
DSA Javascript
Hint
Use console.log(queue) and console.log(stack) to show the final arrays.
Practice
(1/5)
1. Which data structure should you use when you need to process tasks in the exact order they arrive, like customers waiting in line at a store?
easy
A. Graph
B. Stack
C. Tree
D. Queue
Solution
Step 1: Understand the order of processing
Tasks must be processed in the order they arrive, which means first-in, first-out (FIFO).
Step 2: Match the data structure to FIFO
A queue follows FIFO, so it fits the requirement perfectly.
Final Answer:
Queue -> Option D
Quick Check:
FIFO = Queue [OK]
Hint: Use queue for first-in, first-out order [OK]
Common Mistakes:
Choosing stack which is last-in, first-out
Confusing queue with tree or graph
Thinking stack can process in arrival order
2. Which of the following is the correct way to add an element to a stack in JavaScript using an array named stack?
easy
A. stack.unshift(element);
B. stack.shift(element);
C. stack.push(element);
D. stack.pop(element);
Solution
Step 1: Identify stack insertion method
Stacks add elements on top, so we use push() to add at the end of the array.
Step 2: Check JavaScript array methods
push() adds element at the end, shift() removes from front, unshift() adds at front, pop() removes from end.
Final Answer:
stack.push(element); -> Option C
Quick Check:
Stack add = push() [OK]
Hint: Use push() to add on top of stack [OK]
Common Mistakes:
Using shift() which removes from front
Using unshift() which adds at front (queue behavior)
Using pop() which removes element
3. What will be the output of the following JavaScript code?