Bird
Raised Fist0
DSA C#programming~20 mins

Insert at Middle Specific Position in DSA C# - 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
What is the output after inserting at position 3?
Given the linked list 10 -> 20 -> 30 -> 40 -> null, what is the list after inserting 25 at position 3?
DSA C#
class Node {
    public int data;
    public Node next;
    public Node(int d) { data = d; next = null; }
}

class LinkedList {
    public Node head;

    public void InsertAtPosition(int data, int position) {
        Node newNode = new Node(data);
        if (position == 1) {
            newNode.next = head;
            head = newNode;
            return;
        }
        Node current = head;
        for (int i = 1; i < position - 1 && current != null; i++) {
            current = current.next;
        }
        if (current == null) return;
        newNode.next = current.next;
        current.next = newNode;
    }

    public override string ToString() {
        Node temp = head;
        string result = "";
        while (temp != null) {
            result += temp.data + " -> ";
            temp = temp.next;
        }
        return result + "null";
    }
}

LinkedList list = new LinkedList();
list.head = new Node(10);
list.head.next = new Node(20);
list.head.next.next = new Node(30);
list.head.next.next.next = new Node(40);
list.InsertAtPosition(25, 3);
System.Console.WriteLine(list.ToString());
A10 -> 20 -> 25 -> 30 -> 40 -> null
B10 -> 25 -> 20 -> 30 -> 40 -> null
C10 -> 20 -> 30 -> 25 -> 40 -> null
D25 -> 10 -> 20 -> 30 -> 40 -> null
Attempts:
2 left
💡 Hint
Remember that position 3 means after the second node.
❓ Predict Output
intermediate
2:00remaining
What happens if you insert at position 1?
Given the linked list 5 -> 15 -> 25 -> null, what is the list after inserting 1 at position 1?
DSA C#
class Node {
    public int data;
    public Node next;
    public Node(int d) { data = d; next = null; }
}

class LinkedList {
    public Node head;

    public void InsertAtPosition(int data, int position) {
        Node newNode = new Node(data);
        if (position == 1) {
            newNode.next = head;
            head = newNode;
            return;
        }
        Node current = head;
        for (int i = 1; i < position - 1 && current != null; i++) {
            current = current.next;
        }
        if (current == null) return;
        newNode.next = current.next;
        current.next = newNode;
    }

    public override string ToString() {
        Node temp = head;
        string result = "";
        while (temp != null) {
            result += temp.data + " -> ";
            temp = temp.next;
        }
        return result + "null";
    }
}

LinkedList list = new LinkedList();
list.head = new Node(5);
list.head.next = new Node(15);
list.head.next.next = new Node(25);
list.InsertAtPosition(1, 1);
System.Console.WriteLine(list.ToString());
A5 -> 15 -> 25 -> 1 -> null
B5 -> 1 -> 15 -> 25 -> null
C5 -> 15 -> 1 -> 25 -> null
D1 -> 5 -> 15 -> 25 -> null
Attempts:
2 left
💡 Hint
Inserting at position 1 means adding at the start.
🔧 Debug
advanced
2:00remaining
Why does this insertion code fail to insert at position 4?
Consider this code snippet to insert at position 4 in a linked list. Why does it fail to insert the new node?
DSA C#
public void InsertAtPosition(int data, int position) {
    Node newNode = new Node(data);
    Node current = head;
    for (int i = 1; i < position - 1; i++) {
        current = current.next;
    }
    newNode.next = current.next;
    current.next = newNode;
}
AIt fails because it does not check if current is null before accessing current.next.
BIt fails because newNode.next is assigned after current.next is changed.
CIt fails because the loop should run until i <= position - 1.
DIt fails because newNode is not linked to head.
Attempts:
2 left
💡 Hint
Think about what happens if position is beyond the list length.
🧠 Conceptual
advanced
1:00remaining
What is the time complexity of inserting at a specific middle position in a singly linked list?
If you insert a node at position k in a singly linked list of length n, what is the time complexity?
AO(n-k) because you traverse from the end backwards.
BO(1) because insertion is always at the head.
CO(k) because you must traverse to the (k-1)th node.
DO(n^2) because each insertion requires traversing the whole list.
Attempts:
2 left
💡 Hint
Think about how many nodes you must visit before inserting.
❓ Predict Output
expert
3:00remaining
What is the final list after multiple insertions at various positions?
Starting with an empty list, perform these insertions in order: 1) Insert 100 at position 1 2) Insert 200 at position 2 3) Insert 150 at position 2 4) Insert 50 at position 1 What is the final linked list?
DSA C#
class Node {
    public int data;
    public Node next;
    public Node(int d) { data = d; next = null; }
}

class LinkedList {
    public Node head;

    public void InsertAtPosition(int data, int position) {
        Node newNode = new Node(data);
        if (position == 1) {
            newNode.next = head;
            head = newNode;
            return;
        }
        Node current = head;
        for (int i = 1; i < position - 1 && current != null; i++) {
            current = current.next;
        }
        if (current == null) return;
        newNode.next = current.next;
        current.next = newNode;
    }

    public override string ToString() {
        Node temp = head;
        string result = "";
        while (temp != null) {
            result += temp.data + " -> ";
            temp = temp.next;
        }
        return result + "null";
    }
}

LinkedList list = new LinkedList();
list.InsertAtPosition(100, 1);
list.InsertAtPosition(200, 2);
list.InsertAtPosition(150, 2);
list.InsertAtPosition(50, 1);
System.Console.WriteLine(list.ToString());
A100 -> 150 -> 200 -> 50 -> null
B50 -> 100 -> 150 -> 200 -> null
C50 -> 150 -> 100 -> 200 -> null
D100 -> 50 -> 150 -> 200 -> null
Attempts:
2 left
💡 Hint
Track each insertion step carefully from the head.

Practice

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

Solution

  1. Step 1: Understand linked list insertion

    Inserting at a specific position means placing a new node between existing nodes, not just at start or end.
  2. Step 2: Define middle position insertion

    Middle insertion targets a position inside the list, not at boundaries.
  3. Final Answer:

    Adding a new element exactly between two existing nodes at a given position -> Option B
  4. Quick Check:

    Middle insertion = between nodes [OK]
Hint: Middle insertion means placing node between two nodes [OK]
Common Mistakes:
  • Confusing middle insertion with adding at start
  • Thinking middle insertion removes nodes
  • Assuming insertion only happens at end
2. Which of the following C# code snippets correctly inserts a new node at position 3 in a singly linked list?
easy
A. Node current = head; for(int i=1; i<2; i++) current = current.next; newNode.next = current.next; current.next = newNode;
B. Node current = head; for(int i=1; i<3; i++) current = current.next; newNode.next = current.next; current.next = newNode;
C. Node current = head; for(int i=0; i<3; i++) current = current.next; newNode.next = current.next; current.next = newNode;
D. Node current = head; for(int i=1; i<=3; i++) current = current.next; newNode.next = current.next; current.next = newNode;

Solution

  1. Step 1: Understand zero-based vs one-based indexing

    Position 3 means after 2 nodes, so loop runs i from 1 to 2 to reach node before position 3.
  2. Step 2: Check loop and insertion logic

    Node current = head; for(int i=1; i<2; i++) current = current.next; newNode.next = current.next; current.next = newNode; loops correctly to node 2, then inserts newNode after it.
  3. Final Answer:

    Node current = head; for(int i=1; i<2; i++) current = current.next; newNode.next = current.next; current.next = newNode; -> Option A
  4. Quick Check:

    Loop to node before position = i < position-1 [OK]
Hint: Loop to node before position: i < position - 1 [OK]
Common Mistakes:
  • Looping too many times and skipping target node
  • Using wrong loop bounds causing null reference
  • Not updating next pointers correctly
3. Given the linked list: 10 -> 20 -> 30 -> 40 -> null, what is the list after inserting 25 at position 3 using this code?
Node newNode = new Node(25);
Node current = head;
for(int i = 1; i < 2; i++) current = current.next;
newNode.next = current.next;
current.next = newNode;
medium
A. 10 -> 25 -> 20 -> 30 -> 40 -> null
B. 25 -> 10 -> 20 -> 30 -> 40 -> null
C. 10 -> 20 -> 30 -> 25 -> 40 -> null
D. 10 -> 20 -> 25 -> 30 -> 40 -> null

Solution

  1. Step 1: Trace loop to position before insertion

    Loop runs i=1; i<2, so current moves from 10 to 20 (node at position 2).
  2. Step 2: Insert newNode after current

    newNode.next points to current.next (30), current.next points to newNode (25).
  3. Final Answer:

    10 -> 20 -> 25 -> 30 -> 40 -> null -> Option D
  4. Quick Check:

    Inserted 25 at position 3 correctly [OK]
Hint: Insert after node at position-1 to place at position [OK]
Common Mistakes:
  • Inserting at wrong position due to loop bounds
  • Not updating newNode.next correctly
  • Confusing node values and positions
4. What is the error in this code snippet that tries to insert a node at position 4 in a linked list?
Node current = head;
for(int i = 1; i <= 4; i++)
  current = current.next;
newNode.next = current.next;
current.next = newNode;
medium
A. Loop runs too many times causing current to be null before insertion
B. newNode.next is not assigned before insertion
C. The loop should start from i = 0 instead of 1
D. The newNode is not created before insertion

Solution

  1. Step 1: Analyze loop bounds

    Loop runs i=1 to 4 inclusive, moving current 4 times ahead, which may make current null if list length is less than 5.
  2. Step 2: Identify null reference risk

    Accessing current.next after current is null causes runtime error.
  3. Final Answer:

    Loop runs too many times causing current to be null before insertion -> Option A
  4. Quick Check:

    Loop bounds must stop at position-1 [OK]
Hint: Loop to position-1, not position, to avoid null [OK]
Common Mistakes:
  • Using <= instead of < in loop condition
  • Not checking for null before accessing next
  • Incorrect loop start index
5. You have a singly linked list: 5 -> 15 -> 25 -> 35 -> 45 -> null. Write the steps to insert a new node with value 30 at position 4, and what will be the final list?
hard
A. Traverse to node at position 2, link new node's next to current's next, then set current's next to new node. Result: 5 -> 15 -> 30 -> 25 -> 35 -> 45 -> null
B. Traverse to node at position 4, link new node's next to current's next, then set current's next to new node. Result: 5 -> 15 -> 25 -> 35 -> 30 -> 45 -> null
C. Traverse to node at position 3, link new node's next to current's next, then set current's next to new node. Result: 5 -> 15 -> 25 -> 30 -> 35 -> 45 -> null
D. Insert new node at head and shift all nodes. Result: 30 -> 5 -> 15 -> 25 -> 35 -> 45 -> null

Solution

  1. Step 1: Traverse to node before position 4

    Move current pointer to node at position 3 (value 25).
  2. Step 2: Insert new node after current

    Set newNode.next to current.next (35), then current.next to newNode (30).
  3. Final Answer:

    5 -> 15 -> 25 -> 30 -> 35 -> 45 -> null -> Option C
  4. Quick Check:

    Insert at position 4 means after node 3 [OK]
Hint: Insert after node at position-1 for correct placement [OK]
Common Mistakes:
  • Inserting at wrong position by off-by-one error
  • Not updating next pointers correctly
  • Confusing position with node value
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