📖 Scenario: Imagine a line of people waiting to buy tickets at a movie theater. The first person who arrives is the first person to get the ticket and leave the line. This is called FIFO - First In, First Out.We will create a queue to represent this line and see how people join and leave the queue.
🎯 Goal: Build a simple queue using an array in TypeScript that follows the FIFO principle. You will add people to the queue and remove them in the order they arrived.
📋 What You'll Learn
Create an array called queue with initial people names
Create a variable called newPerson to add to the queue
Add newPerson to the end of the queue
Remove the first person from the queue
Print the final state of the queue
💡 Why This Matters
🌍 Real World
Queues are used in real life for lines at stores, printers, and call centers where the first person to arrive is served first.
💼 Career
Understanding queues is important for software jobs that deal with task scheduling, resource management, and handling requests in order.
Progress0 / 4 steps
1
Create the initial queue
Create an array called queue with these exact names in order: 'Alice', 'Bob', 'Charlie'
DSA Typescript
Hint
Use square brackets to create an array and include the names as strings separated by commas.
2
Add a new person to the queue
Create a variable called newPerson and set it to 'David'
DSA Typescript
Hint
Use const newPerson: string = 'David'; to create the variable.
3
Add newPerson to the end of the queue
Use queue.push(newPerson) to add newPerson to the end of the queue
DSA Typescript
Hint
Use the push method to add an item to the end of an array.
4
Remove the first person from the queue and print the queue
Use queue.shift() to remove the first person from the queue and then use console.log(queue) to print the final state of the queue
DSA Typescript
Hint
The shift() method removes the first item from an array. Use console.log(queue) to see the queue after removal.
Practice
(1/5)
1. What does the FIFO principle mean in the context of a queue?
easy
A. First In, First Out - the first element added is the first removed
B. First In, First Out - the last element added is the first removed
C. Last In, First Out - the first element added is the last removed
D. Last In, First Out - the last element added is the last removed
Solution
Step 1: Understand FIFO meaning
FIFO means the first element that enters the queue is the first to leave.
Step 2: Match FIFO to queue behavior
Queues follow FIFO, so the first added element is removed first.
Final Answer:
First In, First Out - the first element added is the first removed -> Option A
Quick Check:
FIFO = First In, First Out [OK]
Hint: Remember queue = line; first come, first served [OK]
Common Mistakes:
Confusing FIFO with LIFO
Thinking last added is removed first
Mixing queue with stack behavior
2. Which of the following is the correct TypeScript syntax to create an empty queue using an array?
easy
A. let queue = {};
B. const queue: number[] = [];
C. const queue = new Queue();
D. queue = [];
Solution
Step 1: Identify TypeScript array syntax
To create an empty array of numbers, use const queue: number[] = [];
Step 2: Check other options
const queue = new Queue(); uses a class not defined here, A creates an object, D is missing declaration keyword.
Final Answer:
const queue: number[] = []; -> Option B
Quick Check:
Array declaration with type and empty brackets [OK]
Hint: Use const with type and empty brackets for empty array [OK]
Common Mistakes:
Using object {} instead of array []
Missing type annotation in TypeScript
Omitting declaration keyword (const/let)
3. What will be the output of the following TypeScript code?
A. Using pop() removes last, not first element in queue
B. push() is not valid for arrays
C. queue should be declared as object, not array
D. Missing type annotation for queue
Solution
Step 1: Understand queue removal method
Queue removes elements in FIFO order, so first element should be removed.
Step 2: Check method used
pop() removes last element, which breaks FIFO principle.
Final Answer:
Using pop() removes last, not first element in queue -> Option A
Quick Check:
Queue removal must use shift(), not pop() [OK]
Hint: Use shift() to remove first, pop() removes last [OK]
Common Mistakes:
Using pop() instead of shift() for queue
Thinking push() removes elements
Confusing array with object declaration
5. You have a queue implemented as an array in TypeScript. You add elements 1, 2, 3, then remove two elements. What will be the state of the queue after these operations?