Bird
Raised Fist0
DSA Typescriptprogramming~20 mins

Enqueue Operation 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 after enqueue operations?
Consider a queue implemented using an array. After performing the following enqueue operations, what is the printed state of the queue?
DSA Typescript
class Queue {
  private items: number[] = [];

  enqueue(element: number) {
    this.items.push(element);
  }

  print() {
    console.log(this.items.join(' -> ') + ' -> null');
  }
}

const queue = new Queue();
queue.enqueue(5);
queue.enqueue(10);
queue.enqueue(15);
queue.print();
A15 -> 10 -> 5 -> null
B5 -> 10 -> 15 -> null
Cnull -> 5 -> 10 -> 15
D5 -> 10 -> null
Attempts:
2 left
💡 Hint
Remember that enqueue adds elements to the end of the queue.
❓ Predict Output
intermediate
2:00remaining
What is the queue state after these enqueue calls?
Given this queue implementation, what will be the output after these enqueue operations?
DSA Typescript
class Queue {
  private items: number[] = [];

  enqueue(element: number) {
    this.items.unshift(element);
  }

  print() {
    console.log(this.items.join(' -> ') + ' -> null');
  }
}

const queue = new Queue();
queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);
queue.print();
Anull -> 1 -> 2 -> 3
B1 -> 2 -> 3 -> null
C1 -> 3 -> 2 -> null
D3 -> 2 -> 1 -> null
Attempts:
2 left
💡 Hint
unshift adds elements to the front of the array.
🔧 Debug
advanced
2:00remaining
Why does this enqueue method leave the queue empty?
This enqueue method is intended to add elements to the queue. Why does the queue remain empty after these operations?
DSA Typescript
class Queue {
  private items: number[] = [];

  enqueue(element: number) {
    this.items[this.items.length] = element;
    this.items.length = this.items.length - 1;
  }
}

const queue = new Queue();
queue.enqueue(7);
queue.enqueue(8);
ABecause reducing length after adding removes the last element, so queue is empty
BBecause assigning to items[length] is invalid and causes an error
CBecause enqueue does not add elements at all due to missing push
DBecause the queue array is not initialized properly
Attempts:
2 left
💡 Hint
Check what happens when you reduce the array length after adding an element.
🧠 Conceptual
advanced
2:00remaining
What is the time complexity of enqueue in a linked list queue?
If a queue is implemented using a linked list with pointers to head and tail, what is the time complexity of the enqueue operation?
AO(log n) because of pointer updates
BO(n) because we must traverse the list to find the tail
CO(1) because we add at the tail directly
DO(n^2) because of repeated traversals
Attempts:
2 left
💡 Hint
Think about how having a tail pointer helps.
🚀 Application
expert
3:00remaining
After these enqueue and dequeue operations, what is the queue state?
Given this queue implementation, what is the printed queue state after the operations?
DSA Typescript
class Queue {
  private items: number[] = [];

  enqueue(element: number) {
    this.items.push(element);
  }

  dequeue() {
    return this.items.shift();
  }

  print() {
    console.log(this.items.join(' -> ') + ' -> null');
  }
}

const queue = new Queue();
queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);
queue.dequeue();
queue.enqueue(4);
queue.print();
A2 -> 3 -> 4 -> null
B1 -> 2 -> 3 -> 4 -> null
C3 -> 4 -> null
D4 -> null
Attempts:
2 left
💡 Hint
Dequeue removes from the front, enqueue adds to the end.

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

  1. Step 1: Understand the queue structure

    A queue works like a line where elements are added at the back and removed from the front.
  2. Step 2: Define the enqueue operation

    Enqueue means adding a new element to the end of the queue, so it waits its turn.
  3. Final Answer:

    Adds an element to the end of the queue -> Option A
  4. Quick Check:

    Enqueue = Add at end [OK]
Hint: Enqueue means add at the back of the queue [OK]
Common Mistakes:
  • Confusing enqueue with dequeue (removal)
  • Thinking enqueue checks queue size
  • Assuming enqueue removes elements
2. Which of the following is the correct TypeScript syntax to add an element 5 to a queue represented by an array queue?
easy
A. queue.pop(5);
B. queue.push(5);
C. queue.shift(5);
D. queue.unshift(5);

Solution

  1. Step 1: Identify array methods for queue operations

    In TypeScript, push adds an element at the end, which matches enqueue.
  2. Step 2: Match enqueue with correct method

    pop removes from end, shift removes from front, unshift adds at front. So only push adds at the end.
  3. Final Answer:

    queue.push(5); -> Option B
  4. Quick Check:

    Enqueue uses push() to add at end [OK]
Hint: Use push() to add at end of array for enqueue [OK]
Common Mistakes:
  • Using pop() which removes instead of adds
  • Using shift() or unshift() which affect front of queue
  • Confusing push() with unshift()
3. What will be the state of the queue after executing the following TypeScript code?
let queue: number[] = [10, 20, 30];
queue.push(40);
queue.push(50);
medium
A. [10, 20, 30, 40, 50]
B. [50, 40, 10, 20, 30]
C. [10, 20, 30]
D. [40, 50]

Solution

  1. Step 1: Start with initial queue

    The queue starts as [10, 20, 30].
  2. Step 2: Apply enqueue operations

    First, push(40) adds 40 at the end: [10, 20, 30, 40]. Then push(50) adds 50 at the end: [10, 20, 30, 40, 50].
  3. Final Answer:

    [10, 20, 30, 40, 50] -> Option A
  4. Quick Check:

    push adds at end, final queue = [10,20,30,40,50] [OK]
Hint: push() adds elements at the end in order [OK]
Common Mistakes:
  • Thinking push adds at front
  • Ignoring second push operation
  • Confusing push with pop or shift
4. Identify the error in the following TypeScript code that tries to enqueue an element 25 to a queue represented by an array queue:
let queue: number[] = [5, 10, 15];
queue.pop(25);
medium
A. The element 25 should be added using shift
B. pop adds element at the front, so wrong method
C. The queue array is not initialized properly
D. pop does not take arguments and removes from the end

Solution

  1. Step 1: Understand pop() method behavior

    pop() removes the last element from the array and does not accept any arguments.
  2. Step 2: Identify misuse of pop()

    Calling pop(25) is incorrect because pop() cannot add elements; it only removes.
  3. Final Answer:

    pop does not take arguments and removes from the end -> Option D
  4. Quick Check:

    pop() removes, push() adds [OK]
Hint: pop() removes last element; push() adds at end [OK]
Common Mistakes:
  • Using pop() to add elements
  • Confusing shift() and pop()
  • Passing arguments to pop()
5. You have a queue implemented as an array in TypeScript. You want to enqueue multiple elements [100, 200, 300] at once while preserving order. Which code snippet correctly enqueues all elements in order?
hard
A. queue.unshift(100, 200, 300);
B. queue.push([100, 200, 300]);
C. queue.push(100, 200, 300);
D. queue.pop(100); queue.pop(200); queue.pop(300);

Solution

  1. Step 1: Understand push with multiple arguments

    push() can take multiple arguments and adds them in order at the end of the array.
  2. Step 2: Analyze each option

    queue.push(100, 200, 300); adds 100, 200, 300 in order at the end. queue.push([100, 200, 300]); adds a single array element, nesting the array. queue.unshift(100, 200, 300); adds elements at the front, reversing order. queue.pop(100); queue.pop(200); queue.pop(300); uses pop which removes elements, not adds.
  3. Final Answer:

    queue.push(100, 200, 300); -> Option C
  4. Quick Check:

    push(...items) adds all at end in order [OK]
Hint: Use push with multiple args to enqueue many elements [OK]
Common Mistakes:
  • Using push with array argument nests array
  • Using unshift adds at front, reversing order
  • Using pop to add elements
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