Bird
Raised Fist0
DSA Goprogramming~10 mins

Insert at Middle Specific Position in DSA Go - Interactive Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to insert a new node at the given position in the linked list.

DSA Go
func InsertAtPosition(head *Node, data int, pos int) *Node {
    newNode := &Node{data: data}
    if pos == 1 {
        newNode.next = head
        return newNode
    }
    current := head
    for i := 1; i < [1]-1 && current != nil; i++ {
        current = current.next
    }
    if current == nil {
        return head
    }
    newNode.next = current.next
    current.next = newNode
    return head
}
Drag options to blanks, or click blank then click option'
Apos
Bdata
CnewNode
Dhead
Attempts:
3 left
💡 Hint
Common Mistakes
Using data or newNode instead of pos in the loop condition.
Looping until pos instead of pos-1 causing off-by-one error.
2fill in blank
medium

Complete the code to check if the position is valid before insertion.

DSA Go
func InsertAtPosition(head *Node, data int, pos int) *Node {
    if pos <= 0 {
        return head
    }
    length := 0
    current := head
    for current != nil {
        length++
        current = current.next
    }
    if pos > [1]+1 {
        return head
    }
    // insertion code continues...
    return head
}
Drag options to blanks, or click blank then click option'
Apos
Bdata
Clength
Dhead
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing position with pos or data instead of length.
Allowing position greater than length+1 causing invalid insertion.
3fill in blank
hard

Fix the error in the loop that traverses to the node before insertion.

DSA Go
func InsertAtPosition(head *Node, data int, pos int) *Node {
    newNode := &Node{data: data}
    if pos == 1 {
        newNode.next = head
        return newNode
    }
    current := head
    for i := 1; i < [1]-1 && current != nil; i++ {
        current = current.next
    }
    if current == nil {
        return head
    }
    newNode.next = current.next
    current.next = newNode
    return head
}
Drag options to blanks, or click blank then click option'
Adata
Bpos
Chead
DnewNode
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= instead of < causing the loop to move one node too far.
Using data or newNode instead of pos in the loop condition.
4fill in blank
hard

Fill both blanks to create a map of word lengths for words longer than 3 characters.

DSA Go
lengths := map[string]int{}
for _, word := range words {
    if len(word) [1] 3 {
        lengths[word] = [2]
    }
}
Drag options to blanks, or click blank then click option'
A>
Blen(word)
C<
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of > causing wrong filtering.
Assigning word instead of len(word) as value.
5fill in blank
hard

Fill all three blanks to create a filtered map with values greater than 0.

DSA Go
result := map[string]int{}
for k, v := range data {
    if v [3] 0 {
        result[[1]] = [2]
    }
}
Drag options to blanks, or click blank then click option'
Ak.upper()
Bv
C>
Dk
Attempts:
3 left
💡 Hint
Common Mistakes
Using k.upper() which is not valid Go syntax.
Using < instead of > causing wrong filtering.

Practice

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

Solution

  1. Step 1: Understand linked list positions

    Positions in a linked list start from 1 at the head and increase by 1 for each node.
  2. Step 2: Define middle insertion

    Inserting at a middle position means placing a new node between two existing nodes at that position.
  3. Final Answer:

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

    Middle insertion = Insert between nodes [OK]
Hint: Middle insertion means placing node between two nodes [OK]
Common Mistakes:
  • Confusing insertion with deletion
  • Thinking insertion only happens at start or end
  • Ignoring position indexing starting at 1
2. Which Go code snippet correctly inserts a new node at position 3 in a singly linked list?
easy
A. temp := head for i := 1; i < 2; i++ { temp = temp.next } newNode.next = temp.next temp.next = newNode
B. temp := head for i := 0; i < 3; i++ { temp = temp.next } newNode.next = temp.next temp.next = newNode
C. temp := head for i := 1; i <= 3; i++ { temp = temp.next } newNode.next = temp.next temp.next = newNode
D. temp := head for i := 1; i < 3; i++ { temp = temp.next } newNode.next = temp.next temp.next = newNode

Solution

  1. Step 1: Loop to node before position 3

    To insert at position 3, loop until position 2 (i < 2) to get node before insertion point.
  2. Step 2: Adjust pointers correctly

    Set newNode.next to temp.next, then temp.next to newNode to insert newNode at position 3.
  3. Final Answer:

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

    Loop to position-1, then insert [OK]
Hint: Loop to position-1 node before inserting [OK]
Common Mistakes:
  • Looping too many times and skipping nodes
  • Setting pointers in wrong order causing lost nodes
  • Using wrong loop conditions (<= instead of <)
3. What will be the linked list after inserting value 99 at position 2 in the list 10 -> 20 -> 30 -> null using this Go code?
type Node struct {
  val  int
  next *Node
}

head := &Node{10, &Node{20, &Node{30, nil}}}
newNode := &Node{99, nil}
temp := head
for i := 1; i < 1; i++ {
  temp = temp.next
}
newNode.next = temp.next
temp.next = newNode
medium
A. 10 -> 99 -> 20 -> 30 -> null
B. 99 -> 10 -> 20 -> 30 -> null
C. 10 -> 20 -> 99 -> 30 -> null
D. 10 -> 20 -> 30 -> 99 -> null

Solution

  1. Step 1: Loop runs zero times to position 1 node

    The loop condition i < 1 means no iteration; temp stays at head (value 10).
  2. Step 2: Insert newNode after temp

    newNode.next points to temp.next (node with 20), then temp.next points to newNode, inserting 99 after 10.
  3. Final Answer:

    10 -> 99 -> 20 -> 30 -> null -> Option A
  4. Quick Check:

    Insert after position 1 = position 2 [OK]
Hint: Loop to node before position, then insert new node [OK]
Common Mistakes:
  • Misunderstanding loop range causing wrong insertion
  • Confusing head node position as 0 instead of 1
  • Incorrect pointer assignments losing nodes
4. Identify the error in this Go code that tries to insert a node at position 4 in a linked list of length 3:
temp := head
for i := 1; i <= 4; i++ {
  temp = temp.next
}
newNode.next = temp.next
temp.next = newNode
medium
A. New node is not created before insertion
B. Pointers are assigned in wrong order causing lost nodes
C. Position 4 is invalid for a list of length 3
D. Loop runs too many times causing temp to be nil and panic

Solution

  1. Step 1: Analyze loop iterations

    The loop runs from i=1 to i=4 inclusive, moving temp 4 times forward.
  2. Step 2: Check list length and temp value

    List length is 3, so after 3 moves temp becomes nil; moving one more causes panic on temp.next.
  3. Final Answer:

    Loop runs too many times causing temp to be nil and panic -> Option D
  4. Quick Check:

    Loop beyond list length = nil pointer error [OK]
Hint: Loop only to position-1 within list length [OK]
Common Mistakes:
  • Looping beyond list length causing runtime errors
  • Not checking for nil before accessing next
  • Confusing <= with < in loop condition
5. Given a singly linked list and a position k, write a Go function to insert a new node with value v at position k. If k is greater than the list length + 1, insert at the end. What will be the list after inserting 50 at position 5 in 10 -> 20 -> 30 -> 40 -> null?
hard
A. 10 -> 20 -> 50 -> 30 -> 40 -> null
B. 10 -> 20 -> 30 -> 40 -> 50 -> null
C. 50 -> 10 -> 20 -> 30 -> 40 -> null
D. 10 -> 20 -> 30 -> 50 -> 40 -> null

Solution

  1. Step 1: Check position against list length

    List length is 4; position 5 is length+1, so insert at end.
  2. Step 2: Insert new node at end

    Traverse to last node (40), set its next to new node with value 50, new node next is nil.
  3. Final Answer:

    10 -> 20 -> 30 -> 40 -> 50 -> null -> Option B
  4. Quick Check:

    Position > length = insert at end [OK]
Hint: If position too big, insert at list end [OK]
Common Mistakes:
  • Trying to insert beyond list length without appending
  • Misplacing new node in middle instead of end
  • Not handling nil next pointer at list end
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