Bird
Raised Fist0
DSA Typescriptprogramming~20 mins

Insert at Middle Specific Position in DSA Typescript - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Master of Insert at Middle Specific Position
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output after inserting at middle position in linked list
What is the printed state of the linked list after inserting value 99 at position 3?
DSA Typescript
class Node {
  value: number;
  next: Node | null;
  constructor(value: number) {
    this.value = value;
    this.next = null;
  }
}

class LinkedList {
  head: Node | null = null;

  insertAtPosition(value: number, position: number): void {
    const newNode = new Node(value);
    if (position === 1) {
      newNode.next = this.head;
      this.head = newNode;
      return;
    }
    let current = this.head;
    let prev: Node | null = null;
    let count = 1;
    while (current !== null && count < position) {
      prev = current;
      current = current.next;
      count++;
    }
    if (prev !== null) {
      prev.next = newNode;
      newNode.next = current;
    }
  }

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

const list = new LinkedList();
list.insertAtPosition(10, 1);
list.insertAtPosition(20, 2);
list.insertAtPosition(30, 3);
list.insertAtPosition(99, 3);
console.log(list.printList());
A10 -> 20 -> 99 -> 30 -> null
B10 -> 99 -> 20 -> 30 -> null
C99 -> 10 -> 20 -> 30 -> null
D10 -> 20 -> 30 -> 99 -> null
Attempts:
2 left
💡 Hint
Remember that position 3 means the new node should be placed after the second node.
❓ Predict Output
intermediate
2:00remaining
Result of inserting at position beyond list length
What is the printed state of the linked list after inserting value 50 at position 10 in the list 5 -> 15 -> 25 -> null?
DSA Typescript
class Node {
  value: number;
  next: Node | null;
  constructor(value: number) {
    this.value = value;
    this.next = null;
  }
}

class LinkedList {
  head: Node | null = null;

  insertAtPosition(value: number, position: number): void {
    const newNode = new Node(value);
    if (position === 1) {
      newNode.next = this.head;
      this.head = newNode;
      return;
    }
    let current = this.head;
    let prev: Node | null = null;
    let count = 1;
    while (current !== null && count < position) {
      prev = current;
      current = current.next;
      count++;
    }
    if (prev !== null) {
      prev.next = newNode;
      newNode.next = current;
    }
  }

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

const list = new LinkedList();
list.insertAtPosition(5, 1);
list.insertAtPosition(15, 2);
list.insertAtPosition(25, 3);
list.insertAtPosition(50, 10);
console.log(list.printList());
A50 -> 5 -> 15 -> 25 -> null
B5 -> 15 -> 50 -> 25 -> null
C5 -> 15 -> 25 -> 50 -> null
D5 -> 15 -> 25 -> null
Attempts:
2 left
💡 Hint
If the position is beyond the list length, the new node is appended at the end.
🔧 Debug
advanced
2:00remaining
Identify the error in insertAtPosition method
What error will this code produce when inserting at position 1 in an empty linked list?
DSA Typescript
class Node {
  value: number;
  next: Node | null;
  constructor(value: number) {
    this.value = value;
    this.next = null;
  }
}

class LinkedList {
  head: Node | null = null;

  insertAtPosition(value: number, position: number): void {
    const newNode = new Node(value);
    if (position === 1) {
      newNode.next = this.head;
      this.head = newNode;
      return;
    }
    let current = this.head;
    let prev: Node | null = null;
    let count = 1;
    while (current !== null && count < position) {
      prev = current;
      current = current.next;
      count++;
    }
    prev.next = newNode;
    newNode.next = current;
  }
}

const list = new LinkedList();
list.insertAtPosition(100, 1);
console.log(list.head?.value);
ATypeError: Cannot read property 'next' of null
BReferenceError: prev is not defined
CSyntaxError: Unexpected token
DNo error, prints 100
Attempts:
2 left
💡 Hint
Check the condition for position 1 and how head is updated.
❓ Predict Output
advanced
2:00remaining
Output after multiple insertions at various positions
What is the printed linked list after these insertions? Insert 1 at position 1 Insert 3 at position 2 Insert 2 at position 2 Insert 4 at position 5
DSA Typescript
class Node {
  value: number;
  next: Node | null;
  constructor(value: number) {
    this.value = value;
    this.next = null;
  }
}

class LinkedList {
  head: Node | null = null;

  insertAtPosition(value: number, position: number): void {
    const newNode = new Node(value);
    if (position === 1) {
      newNode.next = this.head;
      this.head = newNode;
      return;
    }
    let current = this.head;
    let prev: Node | null = null;
    let count = 1;
    while (current !== null && count < position) {
      prev = current;
      current = current.next;
      count++;
    }
    if (prev !== null) {
      prev.next = newNode;
      newNode.next = current;
    }
  }

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

const list = new LinkedList();
list.insertAtPosition(1, 1);
list.insertAtPosition(3, 2);
list.insertAtPosition(2, 2);
list.insertAtPosition(4, 5);
console.log(list.printList());
A1 -> 2 -> 3 -> 4 -> null
B1 -> 3 -> 2 -> 4 -> null
C1 -> 2 -> 3 -> null
D1 -> 2 -> 3 -> 4 -> 5 -> null
Attempts:
2 left
💡 Hint
Insertions at position 2 shift existing nodes to the right. Position 5 appends at the end.
🧠 Conceptual
expert
2:00remaining
Time complexity of inserting at middle position in singly linked list
What is the time complexity of inserting a node at a specific middle position in a singly linked list of length n?
AO(n^2) because each insertion requires traversing the entire list multiple times
BO(n) because you must traverse to the position before insertion
CO(log n) because of binary search to find the position
DO(1) because insertion is always constant time
Attempts:
2 left
💡 Hint
Think about how you find the position before inserting in a singly linked list.

Practice

(1/5)
1. What does inserting at a specific middle position in a linked list mean?
easy
A. Adding a new node only at the beginning of the list
B. Removing a node from the middle of the list
C. Adding a new node only at the end of the list
D. Adding a new node exactly at the given position counting from the start

Solution

  1. Step 1: Understand insertion position

    Inserting at a specific middle position means placing the new node exactly at that position counting from the start of the list.
  2. Step 2: Differentiate from other insertions

    Insertion at beginning or end is different; here we insert inside the list at a given index.
  3. Final Answer:

    Adding a new node exactly at the given position counting from the start -> Option D
  4. Quick Check:

    Insert at position = Add node at that index [OK]
Hint: Insert means add node exactly at given index [OK]
Common Mistakes:
  • Confusing insertion with deletion
  • Thinking insertion only happens at start or end
  • Ignoring the position index
2. Which of the following TypeScript code snippets correctly inserts a node with value 10 at position 2 in a singly linked list?
easy
A. function insertAtPosition(head, val, pos) { let newNode = {value: val, next: null}; let current = head; for(let i=1; i
B. function insertAtPosition(head, val, pos) { let newNode = {value: val, next: null}; let current = head; for(let i=0; i
C. function insertAtPosition(head, val, pos) { let newNode = {value: val, next: null}; newNode.next = head; head = newNode; return head; }
D. function insertAtPosition(head, val, pos) { let newNode = {value: val, next: null}; head.next = newNode; return head; }

Solution

  1. Step 1: Analyze loop for position

    The loop runs from 1 to pos-2 to reach node before insertion point (pos-1).
  2. Step 2: Insert new node correctly

    New node's next points to current.next, then current.next updated to new node.
  3. Final Answer:

    Code in option A correctly inserts at position 2 -> Option A
  4. Quick Check:

    Loop to pos-1, link new node [OK]
Hint: Loop to node before position, then insert [OK]
Common Mistakes:
  • Looping too far or too short
  • Not updating newNode.next correctly
  • Inserting at wrong position index
3. What will be the linked list after inserting value 99 at position 3 in the list 1 -> 2 -> 3 -> 4 -> null using the following TypeScript code?
function insertAtPosition(head, val, pos) {
  let newNode = {value: val, next: null};
  if (pos === 1) {
    newNode.next = head;
    return newNode;
  }
  let current = head;
  for (let i = 1; i < pos - 1; i++) {
    current = current.next;
  }
  newNode.next = current.next;
  current.next = newNode;
  return head;
}

// Initial list: 1 -> 2 -> 3 -> 4 -> null
// Insert 99 at position 3
let head = {value:1, next:{value:2, next:{value:3, next:{value:4, next:null}}}};
head = insertAtPosition(head, 99, 3);
medium
A. 1 -> 2 -> 3 -> 4 -> 99 -> null
B. 1 -> 2 -> 99 -> 3 -> 4 -> null
C. 99 -> 1 -> 2 -> 3 -> 4 -> null
D. 1 -> 99 -> 2 -> 3 -> 4 -> null

Solution

  1. Step 1: Traverse to node before position 3

    Loop runs i=1 to pos-2=1, so current moves to node with value 2.
  2. Step 2: Insert new node after current

    New node 99 points to current.next (node 3), current.next updated to new node.
  3. Final Answer:

    1 -> 2 -> 99 -> 3 -> 4 -> null -> Option B
  4. Quick Check:

    Insert at pos 3 places 99 after 2 [OK]
Hint: Insert after node at pos-1 [OK]
Common Mistakes:
  • Inserting at wrong position
  • Not updating links correctly
  • Confusing position indexing
4. Identify the error in the following TypeScript function that tries to insert a node at position 2 in a linked list.
function insertAtPosition(head, val, pos) {
  let newNode = {value: val, next: null};
  let current = head;
  for(let i = 0; i < pos - 1; i++) {
    current = current.next;
  }
  newNode.next = current.next;
  current.next = newNode;
  return head;
}
medium
A. The loop runs one step too many, causing current to be null before insertion
B. The newNode.next is not assigned correctly
C. The function does not handle insertion at position 1
D. The function returns newNode instead of head

Solution

  1. Step 1: Analyze loop range

    The loop runs from i=0 to pos-2, moving current pos-1 times, which moves current too far.
  2. Step 2: Consequence of loop

    Current becomes the node at position pos, so current.next is undefined, causing error on insertion.
  3. Final Answer:

    The loop runs one step too many, causing current to be null before insertion -> Option A
  4. Quick Check:

    Loop overshoot breaks insertion [OK]
Hint: Loop to node before position, not at position [OK]
Common Mistakes:
  • Looping to position instead of position-1
  • Not checking for position 1 insertion
  • Incorrectly updating links
5. Given a singly linked list and a position that may be greater than the list length, how should you modify the insertAtPosition function to insert the new node at the end if the position is beyond the list length? Choose the correct approach.
hard
A. Insert the new node at the head regardless of position
B. Throw an error if position is greater than list length
C. Traverse until current.next is null or position reached, then insert new node at current.next
D. Ignore the insertion if position is invalid

Solution

  1. Step 1: Traverse safely

    Move current node forward until either position-1 steps or current.next is null (end of list).
  2. Step 2: Insert at end if position too large

    If position is beyond length, current.next will be null, so insert new node at end by setting current.next to new node.
  3. Final Answer:

    Traverse until current.next is null or position reached, then insert new node at current.next -> Option C
  4. Quick Check:

    Insert at end if position too large [OK]
Hint: Stop at end or position, then insert [OK]
Common Mistakes:
  • Not handling position beyond length
  • Throwing error unnecessarily
  • Inserting at wrong place when position invalid
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