Bird
Raised Fist0
DSA Javascriptprogramming~20 mins

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

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

class LinkedList {
  constructor() {
    this.head = null;
  }

  insertAtPosition(value, position) {
    const newNode = new Node(value);
    if (position === 1) {
      newNode.next = this.head;
      this.head = newNode;
      return;
    }
    let current = this.head;
    let count = 1;
    while (current !== null && count < position - 1) {
      current = current.next;
      count++;
    }
    if (current === null) return; // position out of bounds
    newNode.next = current.next;
    current.next = newNode;
  }

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

const list = new LinkedList();
list.insertAtPosition(10, 1);
list.insertAtPosition(20, 2);
list.insertAtPosition(30, 3);
list.insertAtPosition(40, 4);
list.insertAtPosition(99, 3);
list.printList();
A10 -> 20 -> 30 -> 99 -> 40 -> null
B10 -> 20 -> 99 -> 30 -> 40 -> null
C10 -> 99 -> 20 -> 30 -> 40 -> null
D99 -> 10 -> 20 -> 30 -> 40 -> null
Attempts:
2 left
💡 Hint
Remember that position 3 means the new node should be the third node in the list.
❓ Predict Output
intermediate
2:00remaining
Result of inserting at position beyond list length
What is the printed state of the linked list after trying to insert a node with value 50 at position 10 in a list of 4 nodes?
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}

class LinkedList {
  constructor() {
    this.head = null;
  }

  insertAtPosition(value, position) {
    const newNode = new Node(value);
    if (position === 1) {
      newNode.next = this.head;
      this.head = newNode;
      return;
    }
    let current = this.head;
    let count = 1;
    while (current !== null && count < position - 1) {
      current = current.next;
      count++;
    }
    if (current === null) return; // position out of bounds
    newNode.next = current.next;
    current.next = newNode;
  }

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

const list = new LinkedList();
list.insertAtPosition(1, 1);
list.insertAtPosition(2, 2);
list.insertAtPosition(3, 3);
list.insertAtPosition(4, 4);
list.insertAtPosition(50, 10);
list.printList();
A1 -> 2 -> 3 -> 4 -> null
B1 -> 2 -> 3 -> 4 -> 50 -> null
C50 -> 1 -> 2 -> 3 -> 4 -> null
Dnull
Attempts:
2 left
💡 Hint
Inserting at a position beyond the list length does not change the list.
🔧 Debug
advanced
2:00remaining
Identify the error in insertAtPosition method
What error will this code produce when inserting at position 1 in an empty list?
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}

class LinkedList {
  constructor() {
    this.head = null;
  }

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

const list = new LinkedList();
list.insertAtPosition(5, 1);
ANo error, insertion works correctly
BSyntaxError: Unexpected token '.'
CReferenceError: newNode is not defined
DTypeError: Cannot read property 'next' of null
Attempts:
2 left
💡 Hint
Check what happens when this.head is null and you try to access this.head.next.
❓ Predict Output
advanced
2:00remaining
Output after multiple insertions at middle positions
What is the printed state of the linked list after these insertions?
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}

class LinkedList {
  constructor() {
    this.head = null;
  }

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

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

const list = new LinkedList();
list.insertAtPosition(1, 1);
list.insertAtPosition(3, 2);
list.insertAtPosition(2, 2);
list.insertAtPosition(5, 4);
list.insertAtPosition(4, 4);
list.printList();
A1 -> 2 -> 3 -> 4 -> 5 -> null
B1 -> 3 -> 2 -> 4 -> 5 -> null
C1 -> 2 -> 3 -> 5 -> 4 -> null
D1 -> 3 -> 2 -> 5 -> 4 -> null
Attempts:
2 left
💡 Hint
Insertions at position 2 and 4 shift existing nodes to the right.
🧠 Conceptual
expert
2:00remaining
Time complexity of inserting at a specific 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(1) because insertion is always constant time
BO(n^2) because each insertion requires traversing the entire list multiple times
CO(n) because you must traverse the list to the position before insertion
DO(log n) because of binary search to find the position
Attempts:
2 left
💡 Hint
Think about how you find the node before the insertion point.

Practice

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

Solution

  1. Step 1: Understand insertion position

    Inserting at a specific middle position means placing the new node exactly at that index, shifting others if needed.
  2. Step 2: Differentiate from other operations

    Insertion at start or end is different; here we insert inside the list at the given position.
  3. Final Answer:

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

    Insert at position = Add node there [OK]
Hint: Insert means add new node at exact position [OK]
Common Mistakes:
  • Confusing insertion with deletion
  • Thinking insertion only happens at start or end
  • Ignoring position counting from start
2. Which of the following is the correct syntax to insert a node at position 3 in a singly linked list in JavaScript?
easy
A. list.insert(newNode, 3);
B. list.addNode(3, newNode);
C. list.pushAt(3, newNode);
D. list.insertAt(3, newNode);

Solution

  1. Step 1: Identify common method naming

    Common method to insert at a position is named insertAt(position, node).
  2. Step 2: Match parameters order

    Position comes first, then the node to insert, matching list.insertAt(3, newNode).
  3. Final Answer:

    list.insertAt(3, newNode); -> Option D
  4. Quick Check:

    insertAt(position, node) syntax [OK]
Hint: insertAt(position, node) is standard [OK]
Common Mistakes:
  • Swapping parameter order
  • Using wrong method names
  • Confusing insertAt with push or addNode
3. What will be the linked list after inserting value 99 at position 2 in the list: 10 -> 20 -> 30 -> null?
function insertAtPosition(head, value, position) {
  let newNode = { value: value, next: null };
  if (position === 1) {
    newNode.next = head;
    return newNode;
  }
  let current = head;
  let count = 1;
  while (count < position - 1 && current != null) {
    current = current.next;
    count++;
  }
  if (current == null) return head;
  newNode.next = current.next;
  current.next = newNode;
  return head;
}

let head = { value: 10, next: { value: 20, next: { value: 30, next: null } } };
head = insertAtPosition(head, 99, 2);

// Print list
let result = [];
let temp = head;
while(temp) {
  result.push(temp.value);
  temp = temp.next;
}
console.log(result);
medium
A. [99, 10, 20, 30]
B. [10, 99, 20, 30]
C. [10, 20, 99, 30]
D. [10, 20, 30, 99]

Solution

  1. Step 1: Locate position 2 in the list

    Position 2 means after first node (10), before second node (20).
  2. Step 2: Insert new node with value 99 at position 2

    New node points to 20, first node points to new node, resulting list: 10 -> 99 -> 20 -> 30.
  3. Final Answer:

    [10, 99, 20, 30] -> Option B
  4. Quick Check:

    Insert 99 at pos 2 shifts 20 right [OK]
Hint: Insert shifts nodes right at position [OK]
Common Mistakes:
  • Inserting at wrong position
  • Not updating next pointers correctly
  • Confusing position counting starting at 0
4. The following code tries to insert a node at position 4 in a list with 3 nodes. What is the error?
function insertAtPosition(head, value, position) {
  let newNode = { value: value, next: null };
  if (position === 1) {
    newNode.next = head;
    return newNode;
  }
  let current = head;
  let count = 1;
  while (count < position - 1) {
    current = current.next;
    count++;
  }
  newNode.next = current.next;
  current.next = newNode;
  return head;
}
medium
A. The while loop can cause an error if position is greater than list length + 1
B. The newNode is not created properly
C. The function does not return the head
D. The position check for 1 is incorrect

Solution

  1. Step 1: Analyze while loop condition

    The loop runs without checking if current is null, so if position > list length + 1, current becomes null and next access fails.
  2. Step 2: Identify missing boundary check

    There is no check to stop loop if current is null, causing runtime error when accessing current.next.
  3. Final Answer:

    The while loop can cause an error if position is greater than list length + 1 -> Option A
  4. Quick Check:

    Loop needs null check to avoid error [OK]
Hint: Always check current != null in loops [OK]
Common Mistakes:
  • Ignoring null checks in loops
  • Assuming position is always valid
  • Not returning head after insertion
5. You have a singly linked list representing a queue: 5 -> 10 -> 15 -> 20 -> null. You want to insert a new value 12 exactly in the middle position (between 10 and 15). Which code snippet correctly inserts 12 at position 3?
function insertAtPosition(head, value, position) {
  let newNode = { value: value, next: null };
  if (position === 1) {
    newNode.next = head;
    return newNode;
  }
  let current = head;
  let count = 1;
  while (count < position - 1 && current != null) {
    current = current.next;
    count++;
  }
  if (current == null) return head;
  newNode.next = current.next;
  current.next = newNode;
  return head;
}

let head = { value: 5, next: { value: 10, next: { value: 15, next: { value: 20, next: null } } } };
head = insertAtPosition(head, 12, 3);

// Print list
let result = [];
let temp = head;
while(temp) {
  result.push(temp.value);
  temp = temp.next;
}
console.log(result);
What is the output?
hard
A. [5, 10, 12, 15, 20]
B. [5, 10, 15, 12, 20]
C. [12, 5, 10, 15, 20]
D. [5, 12, 10, 15, 20]

Solution

  1. Step 1: Identify position 3 in the list

    Position 3 means after nodes 5 (pos 1) and 10 (pos 2), so between 10 and 15.
  2. Step 2: Insert new node with value 12 at position 3

    New node points to 15, node 10 points to new node, resulting list: 5 -> 10 -> 12 -> 15 -> 20.
  3. Final Answer:

    [5, 10, 12, 15, 20] -> Option A
  4. Quick Check:

    Insert 12 at pos 3 shifts 15 right [OK]
Hint: Insert at position shifts next nodes right [OK]
Common Mistakes:
  • Inserting at wrong position
  • Not updating next pointers correctly
  • Confusing position indexing starting at 0
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