Bird
Raised Fist0
DSA Goprogramming~20 mins

Insert at Beginning of Doubly Linked List in DSA Go - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Doubly Linked List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output after inserting nodes at the beginning
What is the printed state of the doubly linked list after inserting 10, then 20, then 30 at the beginning?
DSA Go
package main

import "fmt"

type Node struct {
    data int
    prev *Node
    next *Node
}

type DoublyLinkedList struct {
    head *Node
}

func (dll *DoublyLinkedList) InsertAtBeginning(data int) {
    newNode := &Node{data: data}
    if dll.head == nil {
        dll.head = newNode
        return
    }
    newNode.next = dll.head
    dll.head.prev = newNode
    dll.head = newNode
}

func (dll *DoublyLinkedList) PrintList() {
    current := dll.head
    for current != nil {
        fmt.Printf("%d", current.data)
        if current.next != nil {
            fmt.Print(" <-> ")
        }
        current = current.next
    }
    fmt.Println()
}

func main() {
    dll := DoublyLinkedList{}
    dll.InsertAtBeginning(10)
    dll.InsertAtBeginning(20)
    dll.InsertAtBeginning(30)
    dll.PrintList()
}
A10 <-> 20 <-> 30
B20 <-> 30 <-> 10
C30 <-> 20 <-> 10
D10 <-> 30 <-> 20
Attempts:
2 left
💡 Hint
Remember, inserting at the beginning means the newest node becomes the head.
🧠 Conceptual
intermediate
1:30remaining
Understanding pointers in doubly linked list insertion
When inserting a new node at the beginning of a doubly linked list, which pointers must be updated?
ANew node's next and prev both point to old head; head remains unchanged
BNew node's prev points to old head; old head's next points to new node; head updated to new node
COnly head pointer is updated to new node; no other pointers change
DNew node's next points to old head; old head's prev points to new node; head updated to new node
Attempts:
2 left
💡 Hint
Think about the direction of links in a doubly linked list.
🔧 Debug
advanced
2:00remaining
Find the bug in this insertion code
What error will this code cause when inserting at the beginning of a doubly linked list?
DSA Go
func (dll *DoublyLinkedList) InsertAtBeginning(data int) {
    newNode := &Node{data: data}
    newNode.next = dll.head
    dll.head.prev = newNode
    dll.head = newNode
}
ACompilation error: missing return statement
BRuntime panic: nil pointer dereference when list is empty
CLogical error: nodes are inserted at the end instead of beginning
DNo error; code works correctly
Attempts:
2 left
💡 Hint
What happens if dll.head is nil?
📝 Syntax
advanced
1:30remaining
Identify the syntax error in this insertion function
Which option correctly fixes the syntax error in this code snippet?
DSA Go
func (dll *DoublyLinkedList) InsertAtBeginning(data int) {
    newNode := &Node{data: data}
    if dll.head == nil {
        dll.head = newNode
    } else {
        newNode.next = dll.head
        dll.head.prev = newNode
        dll.head = newNode
    }
AAdd a closing brace '}' at the end of the function
BReplace 'newNode := &Node{data: data}' with 'newNode = Node{data: data}'
CChange 'dll.head.prev = newNode' to 'dll.head.next = newNode'
DRemove the if-else block entirely
Attempts:
2 left
💡 Hint
Check for matching braces in function definitions.
🚀 Application
expert
3:00remaining
Resulting list after mixed insertions
Given an empty doubly linked list, we perform these operations in order: 1. Insert 5 at beginning 2. Insert 10 at beginning 3. Insert 15 at end 4. Insert 20 at beginning What is the final printed list?
DSA Go
package main

import "fmt"

type Node struct {
    data int
    prev *Node
    next *Node
}

type DoublyLinkedList struct {
    head *Node
    tail *Node
}

func (dll *DoublyLinkedList) InsertAtBeginning(data int) {
    newNode := &Node{data: data}
    if dll.head == nil {
        dll.head = newNode
        dll.tail = newNode
        return
    }
    newNode.next = dll.head
    dll.head.prev = newNode
    dll.head = newNode
}

func (dll *DoublyLinkedList) InsertAtEnd(data int) {
    newNode := &Node{data: data}
    if dll.tail == nil {
        dll.head = newNode
        dll.tail = newNode
        return
    }
    dll.tail.next = newNode
    newNode.prev = dll.tail
    dll.tail = newNode
}

func (dll *DoublyLinkedList) PrintList() {
    current := dll.head
    for current != nil {
        fmt.Printf("%d", current.data)
        if current.next != nil {
            fmt.Print(" <-> ")
        }
        current = current.next
    }
    fmt.Println()
}

func main() {
    dll := DoublyLinkedList{}
    dll.InsertAtBeginning(5)
    dll.InsertAtBeginning(10)
    dll.InsertAtEnd(15)
    dll.InsertAtBeginning(20)
    dll.PrintList()
}
A20 <-> 10 <-> 5 <-> 15
B5 <-> 10 <-> 15 <-> 20
C20 <-> 5 <-> 10 <-> 15
D10 <-> 20 <-> 5 <-> 15
Attempts:
2 left
💡 Hint
Track the head and tail pointers carefully after each insertion.

Practice

(1/5)
1. What is the main purpose of inserting a node at the beginning of a doubly linked list?
easy
A. To add a new node before the current head node
B. To add a new node after the tail node
C. To delete the first node of the list
D. To reverse the entire list

Solution

  1. Step 1: Understand the head node position

    The head node is the first node in the doubly linked list.
  2. Step 2: Insert new node before head

    Inserting at the beginning means placing the new node before the current head and updating pointers.
  3. Final Answer:

    To add a new node before the current head node -> Option A
  4. Quick Check:

    Insert at beginning = add before head [OK]
Hint: Beginning means before the head node [OK]
Common Mistakes:
  • Confusing beginning with end insertion
  • Thinking insertion deletes nodes
  • Assuming insertion reverses list
2. Which of the following Go code snippets correctly updates the previous pointer of the old head node when inserting a new node at the beginning?
easy
A. newNode.prev = oldHead
B. oldHead.prev = newNode
C. newNode.next = oldHead.prev
D. oldHead.next = newNode

Solution

  1. Step 1: Identify pointer to update

    When inserting at beginning, old head's previous pointer must point to new node.
  2. Step 2: Correct assignment

    Setting oldHead.prev = newNode correctly links old head back to new node.
  3. Final Answer:

    oldHead.prev = newNode -> Option B
  4. Quick Check:

    Old head prev points to new node [OK]
Hint: Old head's prev points to new node [OK]
Common Mistakes:
  • Assigning newNode.prev incorrectly
  • Mixing next and prev pointers
  • Updating oldHead.next instead of prev
3. Consider this Go code inserting nodes at the beginning of a doubly linked list:
type Node struct {
  data int
  prev *Node
  next *Node
}

func insertAtBeginning(head **Node, data int) {
  newNode := &Node{data: data}
  newNode.next = *head
  if *head != nil {
    (*head).prev = newNode
  }
  *head = newNode
}

func main() {
  var head *Node
  insertAtBeginning(&head, 10)
  insertAtBeginning(&head, 20)
  insertAtBeginning(&head, 30)
  // Print list from head
  curr := head
  for curr != nil {
    fmt.Print(curr.data, " -> ")
    curr = curr.next
  }
  fmt.Print("null")
}
What is the printed output?
medium
A. 20 -> 30 -> 10 -> null
B. 10 -> 20 -> 30 -> null
C. null
D. 30 -> 20 -> 10 -> null

Solution

  1. Step 1: Trace insertions order

    Nodes inserted in order: 10, then 20 at beginning, then 30 at beginning.
  2. Step 2: List after insertions

    After all insertions, list is 30 (head) -> 20 -> 10 -> null.
  3. Final Answer:

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

    Last inserted node is new head [OK]
Hint: Last inserted node becomes new head [OK]
Common Mistakes:
  • Assuming insertion adds at end
  • Confusing order of nodes
  • Ignoring pointer updates
4. The following Go function attempts to insert a node at the beginning of a doubly linked list but causes a runtime panic:
func insertAtBeginning(head *Node, data int) {
  newNode := &Node{data: data}
  newNode.next = head
  head.prev = newNode
  head = newNode
}
What is the main error causing the panic?
medium
A. head is nil and head.prev causes nil pointer dereference
B. newNode.next is not assigned correctly
C. head = newNode does not update original head pointer
D. data field is not initialized

Solution

  1. Step 1: Check nil pointer usage

    If head is nil, accessing head.prev causes runtime panic (nil pointer dereference).
  2. Step 2: Identify cause of panic

    Function does not check if head is nil before head.prev = newNode, causing panic.
  3. Final Answer:

    head is nil and head.prev causes nil pointer dereference -> Option A
  4. Quick Check:

    Accessing prev on nil head causes panic [OK]
Hint: Check for nil before accessing head.prev [OK]
Common Mistakes:
  • Ignoring nil check before pointer access
  • Thinking assignment head = newNode updates caller
  • Assuming data field causes panic
5. You have a doubly linked list with nodes: 5 <-> 10 <-> 15. You want to insert a new node with value 3 at the beginning. After insertion, what will be the correct sequence of nodes when traversed from head to tail?
hard
A. 15 -> 10 -> 5 -> 3 -> null
B. 5 -> 10 -> 15 -> 3 -> null
C. 3 -> 5 -> 10 -> 15 -> null
D. 3 -> 15 -> 10 -> 5 -> null

Solution

  1. Step 1: Understand insertion at beginning

    New node with value 3 is added before current head (5).
  2. Step 2: Update pointers and traverse

    After insertion, list is 3 (head) -> 5 -> 10 -> 15 -> null.
  3. Final Answer:

    3 -> 5 -> 10 -> 15 -> null -> Option C
  4. Quick Check:

    New node becomes new head [OK]
Hint: New node becomes new head, old head shifts right [OK]
Common Mistakes:
  • Inserting at end instead of beginning
  • Reversing node order incorrectly
  • Not updating head pointer
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