Bird
Raised Fist0
DSA Typescriptprogramming~20 mins

Insert at End Tail Insert in DSA Typescript - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Tail Insert Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output after inserting nodes at the tail
What is the printed linked list after inserting 10, 20, and 30 at the tail in order?
DSA Typescript
class Node {
  value: number;
  next: Node | null = null;
  constructor(value: number) {
    this.value = value;
  }
}

class LinkedList {
  head: Node | null = null;
  tail: Node | null = null;

  insertAtTail(value: number): void {
    const newNode = new Node(value);
    if (!this.head) {
      this.head = newNode;
      this.tail = newNode;
    } else {
      this.tail!.next = newNode;
      this.tail = newNode;
    }
  }

  printList(): string {
    let current = this.head;
    let result = '';
    while (current) {
      result += current.value + ' -> ';
      current = current.next;
    }
    return result + 'null';
  }
}

const list = new LinkedList();
list.insertAtTail(10);
list.insertAtTail(20);
list.insertAtTail(30);
console.log(list.printList());
Anull
B10 -> 20 -> 30 -> null
C10 -> 20 -> null
D30 -> 20 -> 10 -> null
Attempts:
2 left
💡 Hint
Think about how nodes are added at the end and how the list is printed from head to tail.
❓ Predict Output
intermediate
2:00remaining
Linked list state after multiple tail insertions
What is the linked list printed after inserting 5, 15, 25, and 35 at the tail?
DSA Typescript
class Node {
  value: number;
  next: Node | null = null;
  constructor(value: number) {
    this.value = value;
  }
}

class LinkedList {
  head: Node | null = null;
  tail: Node | null = null;

  insertAtTail(value: number): void {
    const newNode = new Node(value);
    if (!this.head) {
      this.head = newNode;
      this.tail = newNode;
    } else {
      this.tail!.next = newNode;
      this.tail = newNode;
    }
  }

  printList(): string {
    let current = this.head;
    let result = '';
    while (current) {
      result += current.value + ' -> ';
      current = current.next;
    }
    return result + 'null';
  }
}

const list = new LinkedList();
list.insertAtTail(5);
list.insertAtTail(15);
list.insertAtTail(25);
list.insertAtTail(35);
console.log(list.printList());
A5 -> 15 -> 25 -> null
B35 -> 25 -> 15 -> 5 -> null
C5 -> 15 -> 25 -> 35 -> null
Dnull
Attempts:
2 left
💡 Hint
Remember that tail insertion adds nodes at the end, preserving order.
🔧 Debug
advanced
2:00remaining
Identify the error in tail insertion
What error occurs when running this code that tries to insert nodes at the tail?
DSA Typescript
class Node {
  value: number;
  next: Node | null = null;
  constructor(value: number) {
    this.value = value;
  }
}

class LinkedList {
  head: Node | null = null;
  tail: Node | null = null;

  insertAtTail(value: number): void {
    const newNode = new Node(value);
    if (!this.head) {
      this.head = newNode;
      this.tail = newNode;
    } else {
      this.tail!.next = newNode;
    }
    // Missing tail update here
  }

  printList(): string {
    let current = this.head;
    let result = '';
    while (current) {
      result += current.value + ' -> ';
      current = current.next;
    }
    return result + 'null';
  }
}

const list = new LinkedList();
list.insertAtTail(1);
list.insertAtTail(2);
list.insertAtTail(3);
console.log(list.printList());
AOutput: 1 -> 2 -> 3 -> null
BOutput: 1 -> 2 -> null
CRuntime Error: Cannot read property 'next' of null
DOutput: 1 -> null
Attempts:
2 left
💡 Hint
Check if the tail pointer is updated after insertion.
❓ Predict Output
advanced
2:00remaining
Linked list after tail insertions and one head insertion
What is the printed linked list after inserting 100 at head, then 200 and 300 at tail?
DSA Typescript
class Node {
  value: number;
  next: Node | null = null;
  constructor(value: number) {
    this.value = value;
  }
}

class LinkedList {
  head: Node | null = null;
  tail: Node | null = null;

  insertAtHead(value: number): void {
    const newNode = new Node(value);
    if (!this.head) {
      this.head = newNode;
      this.tail = newNode;
    } else {
      newNode.next = this.head;
      this.head = newNode;
    }
  }

  insertAtTail(value: number): void {
    const newNode = new Node(value);
    if (!this.head) {
      this.head = newNode;
      this.tail = newNode;
    } else {
      this.tail!.next = newNode;
      this.tail = newNode;
    }
  }

  printList(): string {
    let current = this.head;
    let result = '';
    while (current) {
      result += current.value + ' -> ';
      current = current.next;
    }
    return result + 'null';
  }
}

const list = new LinkedList();
list.insertAtHead(100);
list.insertAtTail(200);
list.insertAtTail(300);
console.log(list.printList());
A100 -> 200 -> 300 -> null
B200 -> 300 -> 100 -> null
C300 -> 200 -> 100 -> null
D100 -> null
Attempts:
2 left
💡 Hint
Head insertion adds at front, tail insertion adds at end.
🧠 Conceptual
expert
2:00remaining
Why is tail pointer important in tail insertion?
Which statement best explains why maintaining a tail pointer is important when inserting nodes at the end of a singly linked list?
AWithout a tail pointer, inserting at the end requires traversing the entire list, making insertion slower.
BTail pointer is only needed for doubly linked lists, not singly linked lists.
CTail pointer helps to delete nodes from the middle of the list efficiently.
DTail pointer is used to reverse the linked list in constant time.
Attempts:
2 left
💡 Hint
Think about how you find the end of the list without a tail pointer.

Practice

(1/5)
1. What does the "Insert at End (Tail Insert)" operation do in a singly linked list?
easy
A. Adds a new node at the beginning of the list
B. Adds a new node at the end of the list
C. Deletes the last node of the list
D. Deletes the first node of the list

Solution

  1. Step 1: Understand the operation

    Insert at End means adding a new node after the last node in the list.
  2. Step 2: Compare with other operations

    Adding at the beginning or deleting nodes are different operations.
  3. Final Answer:

    Adds a new node at the end of the list -> Option B
  4. Quick Check:

    Insert at End = Add node at tail [OK]
Hint: Tail insert always adds node after last element [OK]
Common Mistakes:
  • Confusing tail insert with head insert
  • Thinking it deletes nodes
  • Assuming it inserts in the middle
2. Which of the following is the correct TypeScript syntax to create a new node for tail insertion in a singly linked list?
easy
A. const newNode = new Node(value);
B. const newNode = Node(value);
C. let newNode = Node.create(value);
D. newNode = new Node();

Solution

  1. Step 1: Recall TypeScript class instantiation

    To create an instance of a class, use the 'new' keyword with constructor.
  2. Step 2: Check each option

    const newNode = new Node(value); uses 'new Node(value);' which is correct syntax.
  3. Final Answer:

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

    Use 'new' keyword to create class instance [OK]
Hint: Use 'new' keyword to create node instances [OK]
Common Mistakes:
  • Omitting 'new' keyword
  • Calling class like a function
  • Using undefined methods like create()
3. Consider the following TypeScript code for inserting at the end of a singly linked list:
class Node {
  value: number;
  next: Node | null = null;
  constructor(value: number) { this.value = value; }
}

class LinkedList {
  head: Node | null = null;

  insertAtEnd(value: number) {
    const newNode = new Node(value);
    if (!this.head) {
      this.head = newNode;
      return;
    }
    let current = this.head;
    while (current.next) {
      current = current.next;
    }
    current.next = newNode;
  }
}

const list = new LinkedList();
list.insertAtEnd(5);
list.insertAtEnd(10);
list.insertAtEnd(15);

// What is the linked list after these insertions?
medium
A. 10 -> 5 -> 15 -> null
B. 15 -> 10 -> 5 -> null
C. 5 -> 15 -> 10 -> null
D. 5 -> 10 -> 15 -> null

Solution

  1. Step 1: Insert 5 into empty list

    Since head is null, head becomes node with value 5.
  2. Step 2: Insert 10 at end

    Traverse from head (5), next is null, so set next to new node 10.
  3. Step 3: Insert 15 at end

    Traverse from head (5) to 10, next is null, set next to new node 15.
  4. Final Answer:

    5 -> 10 -> 15 -> null -> Option D
  5. Quick Check:

    Nodes added in order at tail [OK]
Hint: Tail insert keeps order of insertion [OK]
Common Mistakes:
  • Assuming insertion at head
  • Mixing node order
  • Not updating next pointers correctly
4. The following TypeScript code attempts to insert a node at the end of a singly linked list but causes an error:
insertAtEnd(value: number) {
  const newNode = new Node(value);
  if (!this.head) {
    this.head = newNode;
  }
  let current = this.head;
  while (current.next) {
    current = current.next;
  }
  current.next = newNode;
}
What is the error in this code?
medium
A. It inserts the new node twice when the list is empty
B. It never updates the head pointer
C. It causes an infinite loop
D. It does not create a new node properly

Solution

  1. Step 1: Analyze empty list case

    If head is null, head is set to newNode, but code continues to traverse and insert again.
  2. Step 2: Identify double insertion

    After setting head, it still traverses and sets current.next = newNode, adding newNode twice.
  3. Final Answer:

    It inserts the new node twice when the list is empty -> Option A
  4. Quick Check:

    Missing return after head assignment causes double insert [OK]
Hint: Return immediately after setting head in empty list [OK]
Common Mistakes:
  • Not returning after head assignment
  • Assuming single insertion always
  • Ignoring traversal logic
5. You want to optimize the tail insertion in a singly linked list to avoid traversing the entire list every time. Which modification will achieve this?
hard
A. Insert new nodes at the head instead of the tail
B. Use a doubly linked list instead of singly linked list
C. Keep a tail pointer that always points to the last node
D. Store the list length and use it to find the last node

Solution

  1. Step 1: Understand traversal cost

    Without a tail pointer, inserting at end requires traversing all nodes, which is slow for long lists.
  2. Step 2: Use tail pointer

    Maintaining a tail pointer lets you directly access the last node and insert in constant time.
  3. Step 3: Evaluate other options

    Inserting at head changes order, doubly linked list adds complexity, length doesn't help find last node efficiently.
  4. Final Answer:

    Keep a tail pointer that always points to the last node -> Option C
  5. Quick Check:

    Tail pointer = O(1) insert at end [OK]
Hint: Tail pointer avoids full list traversal [OK]
Common Mistakes:
  • Thinking length helps find last node
  • Confusing head insertion with tail insertion
  • Ignoring complexity of doubly linked list
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