Bird
Raised Fist0
DSA Typescriptprogramming~20 mins

Queue Concept and FIFO Principle in DSA Typescript - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Queue Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this queue operation sequence?
Consider a queue where we enqueue numbers 1, 2, 3, then dequeue once, then enqueue 4. What is the state of the queue?
DSA Typescript
class Queue {
  private items: number[] = [];
  enqueue(item: number) { this.items.push(item); }
  dequeue() { return this.items.shift(); }
  print() { return this.items.join(' -> ') + ' -> null'; }
}

const q = new Queue();
q.enqueue(1);
q.enqueue(2);
q.enqueue(3);
q.dequeue();
q.enqueue(4);
console.log(q.print());
A3 -> 4 -> null
B2 -> 3 -> 4 -> null
C1 -> 2 -> 3 -> 4 -> null
D1 -> 3 -> 4 -> null
Attempts:
2 left
💡 Hint
Remember FIFO means first in, first out. The first number enqueued is removed first.
🧠 Conceptual
intermediate
1:00remaining
Which property best describes a queue?
Choose the property that correctly describes how a queue works.
AFirst In, First Out (FIFO)
BRandom Access
CLast In, First Out (LIFO)
DSorted Order
Attempts:
2 left
💡 Hint
Think about who leaves first in a line.
🔧 Debug
advanced
1:30remaining
What error does this queue code produce?
This TypeScript queue code tries to dequeue from an empty queue. What error occurs?
DSA Typescript
class Queue {
  private items: number[] = [];
  dequeue() { return this.items.shift(); }
}

const q = new Queue();
console.log(q.dequeue().toString());
ATypeError: Cannot read property 'toString' of undefined
BSyntaxError: Unexpected token
CReferenceError: q is not defined
DNo error, prints 'undefined'
Attempts:
2 left
💡 Hint
What does shift() return if the array is empty?
🚀 Application
advanced
1:00remaining
How many items remain after these queue operations?
Start with an empty queue. Enqueue 5 items, dequeue 3 items, enqueue 2 more items. How many items are in the queue now?
A2
B5
C3
D4
Attempts:
2 left
💡 Hint
Count carefully: add 5, remove 3, add 2.
❓ Predict Output
expert
2:30remaining
What is the output after this complex queue sequence?
Given the queue operations below, what is printed?
DSA Typescript
class Queue {
  private items: number[] = [];
  enqueue(item: number) { this.items.push(item); }
  dequeue() { return this.items.shift(); }
  print() { return this.items.join(' -> ') + ' -> null'; }
}

const q = new Queue();
q.enqueue(10);
q.enqueue(20);
q.enqueue(30);
q.dequeue();
q.enqueue(40);
q.dequeue();
q.enqueue(50);
console.log(q.print());
A40 -> 50 -> null
B20 -> 30 -> 40 -> 50 -> null
C30 -> 40 -> 50 -> null
D10 -> 20 -> 30 -> 40 -> 50 -> null
Attempts:
2 left
💡 Hint
Track each enqueue and dequeue step carefully in order.

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

  1. Step 1: Understand FIFO meaning

    FIFO means the first element that enters the queue is the first to leave.
  2. Step 2: Match FIFO to queue behavior

    Queues follow FIFO, so the first added element is removed first.
  3. Final Answer:

    First In, First Out - the first element added is the first removed -> Option A
  4. 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

  1. Step 1: Identify TypeScript array syntax

    To create an empty array of numbers, use const queue: number[] = [];
  2. Step 2: Check other options

    const queue = new Queue(); uses a class not defined here, A creates an object, D is missing declaration keyword.
  3. Final Answer:

    const queue: number[] = []; -> Option B
  4. 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?
const queue: number[] = [];
queue.push(10);
queue.push(20);
queue.push(30);
const first = queue.shift();
console.log(queue);
medium
A. []
B. [10, 20, 30]
C. [30]
D. [20, 30]

Solution

  1. Step 1: Add elements to queue

    Elements 10, 20, 30 are added to the queue in order.
  2. Step 2: Remove first element using shift()

    shift() removes 10, so queue now contains [20, 30].
  3. Final Answer:

    [20, 30] -> Option D
  4. Quick Check:

    After shift(), queue = [20, 30] [OK]
Hint: shift() removes first element, push() adds last [OK]
Common Mistakes:
  • Confusing shift() with pop()
  • Expecting original array unchanged
  • Thinking push() removes elements
4. Identify the error in this TypeScript queue code:
const queue: number[] = [];
queue.push(5);
queue.push(10);
const removed = queue.pop();
console.log(queue);
medium
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

  1. Step 1: Understand queue removal method

    Queue removes elements in FIFO order, so first element should be removed.
  2. Step 2: Check method used

    pop() removes last element, which breaks FIFO principle.
  3. Final Answer:

    Using pop() removes last, not first element in queue -> Option A
  4. 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?
const queue: number[] = [];
queue.push(1);
queue.push(2);
queue.push(3);
queue.shift();
queue.shift();
console.log(queue);
hard
A. [2, 3]
B. [1, 2, 3]
C. [3]
D. []

Solution

  1. Step 1: Add elements to queue

    Elements 1, 2, 3 are added in order: queue = [1, 2, 3]
  2. Step 2: Remove two elements using shift()

    First shift() removes 1, second shift() removes 2, leaving [3]
  3. Final Answer:

    [3] -> Option C
  4. Quick Check:

    After two shifts, queue = [3] [OK]
Hint: Each shift() removes one from front [OK]
Common Mistakes:
  • Removing elements with pop() instead of shift()
  • Expecting queue to be empty after two removals
  • Confusing order of elements after removals
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard