Bird
Raised Fist0
DSA Typescriptprogramming~20 mins

Insert at Specific Position in Doubly Linked List in DSA Typescript - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Doubly Linked List Insertion 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 inserting at position 2?
Consider a doubly linked list with nodes 10 <-> 20 <-> 30. We insert a new node with value 15 at position 2 (1-based index). What is the printed list after insertion?
DSA Typescript
class Node {
  value: number;
  next: Node | null = null;
  prev: Node | null = null;
  constructor(value: number) {
    this.value = value;
  }
}

class DoublyLinkedList {
  head: Node | null = null;

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

  printList() {
    let current = this.head;
    const values = [];
    while (current) {
      values.push(current.value);
      current = current.next;
    }
    console.log(values.join(' <-> ') + ' <-> null');
  }
}

const dll = new DoublyLinkedList();
dll.head = new Node(10);
dll.head.next = new Node(20);
dll.head.next.prev = dll.head;
dll.head.next.next = new Node(30);
dll.head.next.next.prev = dll.head.next;

dll.insertAtPosition(15, 2);
dll.printList();
A10 <-> 15 <-> 20 <-> 30 <-> null
B15 <-> 10 <-> 20 <-> 30 <-> null
C10 <-> 20 <-> 15 <-> 30 <-> null
D10 <-> 20 <-> 30 <-> 15 <-> null
Attempts:
2 left
💡 Hint
Remember that position 2 means the new node goes after the first node.
🧠 Conceptual
intermediate
1:30remaining
What happens if we insert at position 1 in a non-empty doubly linked list?
In a doubly linked list, inserting a new node at position 1 means adding it at the head. What changes happen to the list pointers?
AThe new node is inserted after the head; pointers remain unchanged for the head.
BThe new node becomes the tail; its prev points to old tail; old tail's next points to new node.
CThe new node replaces the old head without linking to it.
DThe new node becomes the head; its next points to old head; old head's prev points to new node.
Attempts:
2 left
💡 Hint
Think about how the head pointer and the previous head node's pointers change.
🔧 Debug
advanced
2:30remaining
Why does this insertion code cause a runtime error?
Given the code below, why does calling insertAtPosition(25, 5) cause an error when the list has only 3 nodes?
DSA Typescript
insertAtPosition(value: number, position: number) {
  const newNode = new Node(value);
  if (position === 1) {
    newNode.next = this.head;
    if (this.head) this.head.prev = newNode;
    this.head = newNode;
    return;
  }
  let current = this.head;
  let count = 1;
  while (count < position - 1) {
    current = current.next!;
    count++;
  }
  newNode.next = current.next;
  if (current.next) current.next.prev = newNode;
  current.next = newNode;
  newNode.prev = current;
}
AThe prev pointers are not updated causing a broken list but no runtime error.
BThe new node is inserted at the wrong position causing a logic error but no runtime error.
Ccurrent becomes null before reaching position - 1, so current.next causes a runtime error.
DThe head pointer is overwritten causing loss of the list.
Attempts:
2 left
💡 Hint
Check what happens when position is greater than list length + 1.
❓ Predict Output
advanced
2:00remaining
What is the list after inserting at the tail position?
Starting with list 5 <-> 10 <-> 15, insert 20 at position 4 (tail). What is the printed list?
DSA Typescript
class Node {
  value: number;
  next: Node | null = null;
  prev: Node | null = null;
  constructor(value: number) {
    this.value = value;
  }
}

class DoublyLinkedList {
  head: Node | null = null;

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

  printList() {
    let current = this.head;
    const values = [];
    while (current) {
      values.push(current.value);
      current = current.next;
    }
    console.log(values.join(' <-> ') + ' <-> null');
  }
}

const dll = new DoublyLinkedList();
dll.head = new Node(5);
dll.head.next = new Node(10);
dll.head.next.prev = dll.head;
dll.head.next.next = new Node(15);
dll.head.next.next.prev = dll.head.next;

dll.insertAtPosition(20, 4);
dll.printList();
A20 <-> 5 <-> 10 <-> 15 <-> null
B5 <-> 10 <-> 15 <-> 20 <-> null
C5 <-> 10 <-> 20 <-> 15 <-> null
D5 <-> 10 <-> 15 <-> null
Attempts:
2 left
💡 Hint
Position 4 means after the third node, at the tail.
🧠 Conceptual
expert
1:30remaining
What is the time complexity of inserting at a specific position in a doubly linked list?
Given a doubly linked list of length n, what is the time complexity to insert a node at position k (1 <= k <= n+1)?
AO(k) because we traverse from head to position k-1 to insert.
BO(1) because doubly linked lists allow direct access to any position.
CO(n^2) because each insertion requires traversing the entire list multiple times.
DO(log n) because we can use binary search to find the position.
Attempts:
2 left
💡 Hint
Think about how we find the position before inserting.

Practice

(1/5)
1. What does inserting a node at a specific position in a doubly linked list involve?
easy
A. Adjusting the previous and next pointers of neighboring nodes to include the new node
B. Only changing the head pointer of the list
C. Replacing the node at that position without changing pointers
D. Adding the node at the end regardless of position

Solution

  1. Step 1: Understand doubly linked list structure

    Each node has pointers to both previous and next nodes, so insertion requires updating these pointers.
  2. Step 2: Insert node at specific position

    We must update the previous node's next pointer and the next node's previous pointer to point to the new node.
  3. Final Answer:

    Adjusting the previous and next pointers of neighboring nodes to include the new node -> Option A
  4. Quick Check:

    Pointer updates = Adjust neighbors [OK]
Hint: Remember to update both previous and next pointers [OK]
Common Mistakes:
  • Only updating one pointer leads to broken links
  • Ignoring the previous pointer in insertion
  • Replacing nodes instead of inserting
2. Which of the following TypeScript code snippets correctly declares a node class for a doubly linked list?
easy
A. class Node { value: number; next: Node | null; constructor(value: number) { this.value = value; this.next = null; } }
B. class Node { value: number; next: Node; prev: Node; constructor(value: number) { this.value = value; } }
C. class Node { val: number; next: Node | null; constructor(val: number) { this.val = val; this.next = null; } }
D. class Node { value: number; next: Node | null; prev: Node | null; constructor(value: number) { this.value = value; this.next = null; this.prev = null; } }

Solution

  1. Step 1: Check for previous and next pointers

    A doubly linked list node must have both next and prev pointers, which can be null.
  2. Step 2: Verify constructor initializes all properties

    class Node { value: number; next: Node | null; prev: Node | null; constructor(value: number) { this.value = value; this.next = null; this.prev = null; } } initializes value, next, and prev correctly with proper types.
  3. Final Answer:

    class Node { value: number; next: Node | null; prev: Node | null; constructor(value: number) { this.value = value; this.next = null; this.prev = null; } } -> Option D
  4. Quick Check:

    Both pointers initialized = class Node { value: number; next: Node | null; prev: Node | null; constructor(value: number) { this.value = value; this.next = null; this.prev = null; } } [OK]
Hint: Doubly linked nodes need both next and prev pointers [OK]
Common Mistakes:
  • Forgetting to initialize prev pointer
  • Using wrong property names
  • Not allowing null for next or prev
3. Given the doubly linked list 1 <-> 2 <-> 4 and the code below inserting 3 at position 3, what is the list after insertion?
insertAtPosition(head, 3, 3);
Positions start at 1.
medium
A. 1 <-> 2 <-> 3 <-> 4
B. 1 <-> 3 <-> 2 <-> 4
C. 3 <-> 1 <-> 2 <-> 4
D. 1 <-> 2 <-> 4 <-> 3

Solution

  1. Step 1: Identify position 3 in the list

    Positions: 1=1, 2=2, 3=4. We want to insert before node with value 4.
  2. Step 2: Insert node with value 3 at position 3

    Update pointers so 2 points next to 3, 3 points prev to 2 and next to 4, 4 points prev to 3.
  3. Final Answer:

    1 <-> 2 <-> 3 <-> 4 -> Option A
  4. Quick Check:

    Insert at position 3 = 1-2-3-4 [OK]
Hint: Count nodes, insert before node at that position [OK]
Common Mistakes:
  • Inserting after instead of before position
  • Mixing up prev and next pointers
  • Off-by-one errors in position counting
4. What is wrong with this TypeScript code snippet for inserting a node at position 2 in a doubly linked list?
function insertAtPosition(head: Node | null, value: number, position: number): Node | null {
  let newNode = new Node(value);
  if (position === 1) {
    newNode.next = head;
    if (head) head.prev = newNode;
    return newNode;
  }
  let current = head;
  for (let i = 1; i < position - 1; i++) {
    if (!current) return head;
    current = current.next;
  }
  newNode.next = current.next;
  current.next = newNode;
  newNode.prev = current;
  return head;
}
medium
A. Not handling insertion at head
B. Incorrect loop range in for loop
C. Missing update of newNode.next.prev to newNode
D. Not creating new node correctly

Solution

  1. Step 1: Analyze pointer updates after insertion

    After inserting newNode, newNode.next.prev should point back to newNode to maintain doubly links.
  2. Step 2: Identify missing pointer update

    The code misses updating newNode.next.prev = newNode, which breaks backward link from next node.
  3. Final Answer:

    Missing update of newNode.next.prev to newNode -> Option C
  4. Quick Check:

    Update both next and prev pointers [OK]
Hint: Always update newNode.next.prev after insertion [OK]
Common Mistakes:
  • Forgetting to update newNode.next.prev
  • Loop off-by-one errors
  • Not handling empty list insertion
5. You have a doubly linked list representing train cars: Engine <-> Car1 <-> Car2 <-> Caboose. You want to insert a new CarX at position 2. After insertion, what is the correct order of cars?
hard
A. CarX <-> Engine <-> Car1 <-> Car2 <-> Caboose
B. Engine <-> CarX <-> Car1 <-> Car2 <-> Caboose
C. Engine <-> Car1 <-> CarX <-> Car2 <-> Caboose
D. Engine <-> Car1 <-> Car2 <-> Caboose <-> CarX

Solution

  1. Step 1: Identify position 2 in the list

    Positions: 1=Engine, 2=Car1. Insert CarX before Car1 to become new position 2.
  2. Step 2: Insert CarX at position 2

    Update pointers so Engine next points to CarX, CarX prev to Engine, CarX next to Car1, Car1 prev to CarX.
  3. Final Answer:

    Engine <-> CarX <-> Car1 <-> Car2 <-> Caboose -> Option B
  4. Quick Check:

    Insert at position 2 = Engine-CarX-Car1... [OK]
Hint: Insert before node at position to shift others right [OK]
Common Mistakes:
  • Inserting after instead of before position
  • Not updating prev 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