Bird
Raised Fist0
DSA Typescriptprogramming~20 mins

Enqueue Using Linked List in DSA Typescript - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Linked List Queue Master
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 linked list queue initially empty. We enqueue 10, then 20, then 30. What is the printed state of the queue after these operations?
DSA Typescript
class Node {
  value: number;
  next: Node | null = null;
  constructor(value: number) {
    this.value = value;
  }
}

class Queue {
  front: Node | null = null;
  rear: Node | null = null;

  enqueue(value: number) {
    const newNode = new Node(value);
    if (!this.rear) {
      this.front = this.rear = newNode;
      return;
    }
    this.rear.next = newNode;
    this.rear = newNode;
  }

  printQueue() {
    let current = this.front;
    let result = '';
    while (current) {
      result += current.value + ' -> ';
      current = current.next;
    }
    result += 'null';
    console.log(result);
  }
}

const q = new Queue();
q.enqueue(10);
q.enqueue(20);
q.enqueue(30);
q.printQueue();
A30 -> 20 -> 10 -> null
B20 -> 30 -> null
C10 -> 20 -> null
D10 -> 20 -> 30 -> null
Attempts:
2 left
💡 Hint
Remember, enqueue adds to the rear of the queue.
❓ Predict Output
intermediate
2:00remaining
What happens if we enqueue to an empty queue?
Given the enqueue method below, what will be the state of the queue after enqueueing 5 into an empty queue?
DSA Typescript
class Node {
  value: number;
  next: Node | null = null;
  constructor(value: number) {
    this.value = value;
  }
}

class Queue {
  front: Node | null = null;
  rear: Node | null = null;

  enqueue(value: number) {
    const newNode = new Node(value);
    if (!this.rear) {
      this.front = this.rear = newNode;
      return;
    }
    this.rear.next = newNode;
    this.rear = newNode;
  }

  printQueue() {
    let current = this.front;
    let result = '';
    while (current) {
      result += current.value + ' -> ';
      current = current.next;
    }
    result += 'null';
    console.log(result);
  }
}

const q = new Queue();
q.enqueue(5);
q.printQueue();
A5 -> 5 -> null
Bnull
C5 -> null
DError: Cannot read property 'next' of null
Attempts:
2 left
💡 Hint
Check how the code handles the empty queue case.
🔧 Debug
advanced
2:00remaining
Identify the error in this enqueue implementation
What error will this enqueue method cause when adding the first element to an empty queue?
DSA Typescript
enqueue(value: number) {
  const newNode = new Node(value);
  if (this.front === null) {
    this.front.next = newNode;
    this.front = newNode;
  } else {
    this.rear.next = newNode;
    this.rear = newNode;
  }
}
ATypeError: Cannot read property 'next' of null
BNo error, works correctly
CReferenceError: rear is not defined
DSyntaxError: Unexpected token
Attempts:
2 left
💡 Hint
Check what happens when front is null and you try to access front.next.
❓ Predict Output
advanced
2:00remaining
What is the queue state after multiple enqueues and one dequeue?
Starting from an empty queue, enqueue 1, enqueue 2, enqueue 3, then dequeue once. What is the printed queue state?
DSA Typescript
class Node {
  value: number;
  next: Node | null = null;
  constructor(value: number) {
    this.value = value;
  }
}

class Queue {
  front: Node | null = null;
  rear: Node | null = null;

  enqueue(value: number) {
    const newNode = new Node(value);
    if (!this.rear) {
      this.front = this.rear = newNode;
      return;
    }
    this.rear.next = newNode;
    this.rear = newNode;
  }

  dequeue() {
    if (!this.front) return null;
    const value = this.front.value;
    this.front = this.front.next;
    if (!this.front) this.rear = null;
    return value;
  }

  printQueue() {
    let current = this.front;
    let result = '';
    while (current) {
      result += current.value + ' -> ';
      current = current.next;
    }
    result += 'null';
    console.log(result);
  }
}

const q = new Queue();
q.enqueue(1);
q.enqueue(2);
q.enqueue(3);
q.dequeue();
q.printQueue();
A1 -> 2 -> 3 -> null
B2 -> 3 -> null
C3 -> null
Dnull
Attempts:
2 left
💡 Hint
Dequeue removes from the front of the queue.
🧠 Conceptual
expert
2:00remaining
Why is it important to update rear when enqueueing in a linked list queue?
In a linked list implementation of a queue, what is the main reason to update the rear pointer when enqueueing a new element?
ATo ensure new elements are added at the end and future enqueues can link correctly
BTo keep track of the front element for dequeue operations
CTo avoid memory leaks by freeing old nodes
DTo reverse the queue order after each enqueue
Attempts:
2 left
💡 Hint
Think about where new elements are added in a queue.

Practice

(1/5)
1. What does the enqueue operation do in a linked list implementation of a queue?
easy
A. Removes the first element from the linked list
B. Adds a new element at the end (tail) of the linked list
C. Adds a new element at the beginning (head) of the linked list
D. Deletes the last element from the linked list

Solution

  1. Step 1: Understand enqueue operation

    Enqueue means adding an element to the queue.
  2. Step 2: Identify linked list position for enqueue

    In a linked list queue, new elements are added at the tail (end).
  3. Final Answer:

    Adds a new element at the end (tail) of the linked list -> Option B
  4. Quick Check:

    Enqueue = Add at tail [OK]
Hint: Enqueue always adds at the tail in linked list queue [OK]
Common Mistakes:
  • Confusing enqueue with dequeue operation
  • Adding new element at head instead of tail
  • Thinking enqueue removes elements
  • Mixing linked list positions
2. Which of the following is the correct TypeScript syntax to create a new node for enqueue in a linked list?
easy
A. newNode = new Node();
B. let newNode = Node(value);
C. const newNode = new Node(value);
D. const newNode = Node.new(value);

Solution

  1. Step 1: Recall TypeScript class instantiation

    To create an object, use new ClassName(params).
  2. Step 2: Check options for correct syntax

    Only const newNode = new Node(value); correctly creates a new Node instance.
  3. Final Answer:

    const newNode = new Node(value); -> Option C
  4. Quick Check:

    Use 'new' keyword to instantiate class [OK]
Hint: Use 'new' keyword to create class instances in TypeScript [OK]
Common Mistakes:
  • Omitting 'new' keyword when creating objects
  • Using function call syntax without 'new'
  • Incorrect property access like Node.new
  • Not declaring variable with let or const
3. What will be the state of the queue after executing the following enqueue operations on an empty linked list queue?
enqueue(10); enqueue(20); enqueue(30);
medium
A. 10 -> 20 -> 30 -> null
B. 30 -> 20 -> 10 -> null
C. null -> 10 -> 20 -> 30
D. 20 -> 10 -> 30 -> null

Solution

  1. Step 1: Understand enqueue order

    Enqueue adds elements at the tail, so order is preserved as inserted.
  2. Step 2: Trace each enqueue

    After enqueue(10): 10 -> null
    After enqueue(20): 10 -> 20 -> null
    After enqueue(30): 10 -> 20 -> 30 -> null
  3. Final Answer:

    10 -> 20 -> 30 -> null -> Option A
  4. Quick Check:

    Enqueue order = insertion order [OK]
Hint: Enqueue adds at tail, so order stays same as insertion [OK]
Common Mistakes:
  • Reversing order of elements after enqueue
  • Confusing enqueue with push at head
  • Misreading linked list direction
  • Ignoring null at the end
4. Consider this enqueue function snippet for a linked list queue:
enqueue(value: number) {
  const newNode = new Node(value);
  if (!this.tail) {
    this.head = newNode;
    this.tail = newNode;
    return;
  }
  this.tail.next = newNode;
  this.tail = newNode;
}

What is the bug in this code?
medium
A. It does not update the previous tail's next pointer to newNode
B. It incorrectly sets head to null when queue is empty
C. It creates newNode without value
D. It sets tail to null after enqueue

Solution

  1. Step 1: Analyze enqueue logic

    When queue is empty, head and tail should point to newNode.
  2. Step 2: Check linking of nodes

    Code sets tail = newNode but does not link old tail's next to newNode, breaking chain.
  3. Final Answer:

    It does not update the previous tail's next pointer to newNode -> Option A
  4. Quick Check:

    Link old tail's next to newNode [OK]
Hint: Always link old tail's next to new node when enqueueing [OK]
Common Mistakes:
  • Forgetting to update old tail's next pointer
  • Not handling empty queue case properly
  • Setting tail to null accidentally
  • Creating node without value parameter
5. You have a linked list queue with elements: 5 -> 15 -> 25 -> null.
After enqueue(35), what will be the correct linked list representation and tail node value?
hard
A. 5 -> 15 -> 25 -> 35 -> null, tail value = 25
B. 35 -> 5 -> 15 -> 25 -> null, tail value = 25
C. 5 -> 15 -> 25 -> null, tail value = 25
D. 5 -> 15 -> 25 -> 35 -> null, tail value = 35

Solution

  1. Step 1: Understand enqueue effect on queue

    Enqueue adds new element at tail, so 35 goes after 25.
  2. Step 2: Update tail pointer

    Tail must point to the new last node, which holds 35.
  3. Final Answer:

    5 -> 15 -> 25 -> 35 -> null, tail value = 35 -> Option D
  4. Quick Check:

    Enqueue adds at tail, tail updates to new node [OK]
Hint: Tail always points to last enqueued node [OK]
Common Mistakes:
  • Adding new node at head instead of tail
  • Not updating tail pointer after enqueue
  • Confusing tail value with head value
  • Leaving tail pointing to old last node
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