Queue Concept and FIFO Principle in DSA Typescript - Time & Space Complexity
We want to understand how the time to add or remove items from a queue changes as the queue grows.
How does the queue handle operations when more items are involved?
Analyze the time complexity of the following code snippet.
class Queue {
private items: number[] = [];
enqueue(item: number) {
this.items.push(item); // add to end
}
dequeue() {
return this.items.shift(); // remove from front
}
}
This code adds items to the end and removes items from the front of the queue, following FIFO.
Look at the main repeated actions when using the queue.
- Primary operation: Adding with
pushand removing withshift. - How many times: Each operation happens once per item added or removed.
Adding items is quick no matter how many items are in the queue.
Removing items takes longer as the queue grows because all remaining items shift forward.
| Input Size (n) | Approx. Operations for enqueue | Approx. Operations for dequeue |
|---|---|---|
| 10 | 10 (simple add) | Up to 10 (shifting items) |
| 100 | 100 (simple add) | Up to 100 (shifting items) |
| 1000 | 1000 (simple add) | Up to 1000 (shifting items) |
Pattern observation: Adding stays fast, removing slows down as queue grows.
Time Complexity: O(1) for enqueue, O(n) for dequeue
Adding is always quick, but removing takes longer when the queue is bigger.
[X] Wrong: "Both adding and removing items from a queue always take the same quick time."
[OK] Correct: Removing from the front requires moving all other items forward, which takes more time as the queue grows.
Understanding how queue operations scale helps you explain data structure choices clearly and shows you know what happens behind the scenes.
"What if we used a linked list instead of an array for the queue? How would the time complexity change?"
Practice
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 AQuick Check:
FIFO = First In, First Out [OK]
- Confusing FIFO with LIFO
- Thinking last added is removed first
- Mixing queue with stack behavior
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 BQuick Check:
Array declaration with type and empty brackets [OK]
- Using object {} instead of array []
- Missing type annotation in TypeScript
- Omitting declaration keyword (const/let)
const queue: number[] = []; queue.push(10); queue.push(20); queue.push(30); const first = queue.shift(); console.log(queue);
Solution
Step 1: Add elements to queue
Elements 10, 20, 30 are added to the queue in order.Step 2: Remove first element using shift()
shift() removes 10, so queue now contains [20, 30].Final Answer:
[20, 30] -> Option DQuick Check:
After shift(), queue = [20, 30] [OK]
- Confusing shift() with pop()
- Expecting original array unchanged
- Thinking push() removes elements
const queue: number[] = []; queue.push(5); queue.push(10); const removed = queue.pop(); console.log(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 AQuick Check:
Queue removal must use shift(), not pop() [OK]
- Using pop() instead of shift() for queue
- Thinking push() removes elements
- Confusing array with object declaration
const queue: number[] = []; queue.push(1); queue.push(2); queue.push(3); queue.shift(); queue.shift(); console.log(queue);
Solution
Step 1: Add elements to queue
Elements 1, 2, 3 are added in order: queue = [1, 2, 3]Step 2: Remove two elements using shift()
First shift() removes 1, second shift() removes 2, leaving [3]Final Answer:
[3] -> Option CQuick Check:
After two shifts, queue = [3] [OK]
- Removing elements with pop() instead of shift()
- Expecting queue to be empty after two removals
- Confusing order of elements after removals
Ready to prove it?
Pass the test and this pattern moves unknown
