Bird
Raised Fist0
DSA Goprogramming~20 mins

Create and Initialize 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 of Doubly Linked List Initialization
What is the printed state of the doubly linked list after initializing it with values 10, 20, 30 in order?
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) Append(value int) {
    newNode := &Node{data: value}
    if dll.head == nil {
        dll.head = newNode
        dll.tail = newNode
        return
    }
    dll.tail.next = newNode
    newNode.prev = dll.tail
    dll.tail = newNode
}

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

func main() {
    dll := DoublyLinkedList{}
    dll.Append(10)
    dll.Append(20)
    dll.Append(30)
    dll.PrintForward()
}
A10 <-> 20 <-> 30 <-> nil
B30 <-> 20 <-> 10 <-> nil
C10 -> 20 -> 30 -> nil
Dnil <-> 10 <-> 20 <-> 30
Attempts:
2 left
💡 Hint
Remember that Append adds nodes at the end, so the order is the same as insertion.
❓ Predict Output
intermediate
2:00remaining
Output after inserting at head in Doubly Linked List
What is the printed state of the doubly linked list after inserting 5 at the head of a list initialized with 10, 20, 30?
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) Append(value int) {
    newNode := &Node{data: value}
    if dll.head == nil {
        dll.head = newNode
        dll.tail = newNode
        return
    }
    dll.tail.next = newNode
    newNode.prev = dll.tail
    dll.tail = newNode
}

func (dll *DoublyLinkedList) InsertAtHead(value int) {
    newNode := &Node{data: value}
    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) PrintForward() {
    current := dll.head
    for current != nil {
        fmt.Print(current.data)
        if current.next != nil {
            fmt.Print(" <-> ")
        }
        current = current.next
    }
    fmt.Println(" <-> nil")
}

func main() {
    dll := DoublyLinkedList{}
    dll.Append(10)
    dll.Append(20)
    dll.Append(30)
    dll.InsertAtHead(5)
    dll.PrintForward()
}
Anil <-> 5 <-> 10 <-> 20 <-> 30
B10 <-> 20 <-> 30 <-> 5 <-> nil
C5 <-> 10 <-> 20 <-> 30 <-> nil
D5 -> 10 -> 20 -> 30 -> nil
Attempts:
2 left
💡 Hint
InsertAtHead adds a new node before the current head.
🔧 Debug
advanced
2:00remaining
Identify the bug in doubly linked list Append method
What error will occur when running this Append method code for a doubly linked list?
DSA Go
func (dll *DoublyLinkedList) Append(value int) {
    newNode := &Node{data: value}
    if dll.head == nil {
        dll.head = newNode
        dll.tail = newNode
        return
    }
    dll.tail.next = newNode
    newNode.prev = dll.head
    dll.tail = newNode
}
ANo error; list appends correctly
BRuntime error due to nil pointer dereference
CCompilation error due to missing return statement
DLogical error: newNode.prev points to head instead of tail
Attempts:
2 left
💡 Hint
Check the assignment of newNode.prev in relation to tail and head.
❓ Predict Output
advanced
2:00remaining
Output after removing a node from doubly linked list
What is the printed state of the doubly linked list after removing the node with value 20 from the list 10 <-> 20 <-> 30?
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) Append(value int) {
    newNode := &Node{data: value}
    if dll.head == nil {
        dll.head = newNode
        dll.tail = newNode
        return
    }
    dll.tail.next = newNode
    newNode.prev = dll.tail
    dll.tail = newNode
}

func (dll *DoublyLinkedList) Remove(value int) {
    current := dll.head
    for current != nil {
        if current.data == value {
            if current.prev != nil {
                current.prev.next = current.next
            } else {
                dll.head = current.next
            }
            if current.next != nil {
                current.next.prev = current.prev
            } else {
                dll.tail = current.prev
            }
            return
        }
        current = current.next
    }
}

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

func main() {
    dll := DoublyLinkedList{}
    dll.Append(10)
    dll.Append(20)
    dll.Append(30)
    dll.Remove(20)
    dll.PrintForward()
}
A10 <-> 20 <-> 30 <-> nil
B10 <-> 30 <-> nil
C30 <-> 10 <-> nil
Dnil <-> 10 <-> 30
Attempts:
2 left
💡 Hint
Removing 20 should link 10 directly to 30.
🧠 Conceptual
expert
2:00remaining
Correct order of steps to initialize a doubly linked list with one node
Which is the correct sequence of steps to initialize a doubly linked list with a single node containing value 15?
A1,4,2,3
B4,1,2,3
C1,2,3,4
D2,3,1,4
Attempts:
2 left
💡 Hint
The node must be created first, then its pointers set, then list pointers assigned.

Practice

(1/5)
1. What is the main purpose of a doubly linked list in data structures?
easy
A. To sort data automatically
B. To store data in a fixed-size array
C. To allow traversal in both forward and backward directions
D. To store data only in a single direction

Solution

  1. Step 1: Understand doubly linked list structure

    A doubly linked list has nodes with pointers to both previous and next nodes.
  2. Step 2: Identify traversal capability

    This structure allows moving forward and backward through the list easily.
  3. Final Answer:

    To allow traversal in both forward and backward directions -> Option C
  4. Quick Check:

    Doubly linked list = bidirectional traversal [OK]
Hint: Remember: doubly means two directions [OK]
Common Mistakes:
  • Confusing doubly linked list with arrays
  • Thinking it sorts data automatically
  • Assuming it only moves forward
2. Which of the following Go code snippets correctly defines a node struct for a doubly linked list?
easy
A. type Node struct { data int; next *Node; prev *Node }
B. type Node struct { data int; next Node; prev Node }
C. type Node struct { data int; next *int; prev *int }
D. type Node struct { data int; next int; prev int }

Solution

  1. Step 1: Check pointer usage for next and prev

    In Go, linked list nodes must use pointers to other nodes, so next and prev should be *Node.
  2. Step 2: Verify data types

    Data is int, and next and prev are pointers to Node, matching type Node struct { data int; next *Node; prev *Node }.
  3. Final Answer:

    type Node struct { data int; next *Node; prev *Node } -> Option A
  4. Quick Check:

    Pointers for next and prev = type Node struct { data int; next *Node; prev *Node } [OK]
Hint: Use *Node for next and prev pointers [OK]
Common Mistakes:
  • Using value types instead of pointers for next/prev
  • Using wrong pointer types like *int
  • Missing semicolons or commas (Go allows semicolons but usually uses new lines)
3. What will be the output of the following Go code that creates a doubly linked list with nodes 1, 2, 3 and prints forward traversal?
package main
import "fmt"
type Node struct {
  data int
  next *Node
  prev *Node
}
func main() {
  head := &Node{data: 1}
  second := &Node{data: 2}
  third := &Node{data: 3}
  head.next = second
  second.prev = head
  second.next = third
  third.prev = second
  current := head
  for current != nil {
    fmt.Print(current.data)
    if current.next != nil {
      fmt.Print(" -> ")
    }
    current = current.next
  }
  fmt.Println(" -> null")
}
medium
A. 3 -> 2 -> 1 -> null
B. null -> 1 -> 2 -> 3
C. 1 2 3 null
D. 1 -> 2 -> 3 -> null

Solution

  1. Step 1: Understand list creation and linking

    Nodes 1, 2, 3 are linked forward: head.next = second, second.next = third.
  2. Step 2: Trace forward traversal loop

    Starting at head, prints 1, then 2, then 3, each followed by ' -> ', ending with ' -> null'.
  3. Final Answer:

    1 -> 2 -> 3 -> null -> Option D
  4. Quick Check:

    Forward traversal prints nodes in order [OK]
Hint: Follow next pointers from head to print forward [OK]
Common Mistakes:
  • Confusing forward and backward traversal
  • Missing arrows in output
  • Printing data without separators
4. Identify the error in the following Go code that tries to initialize a doubly linked list node:
type Node struct {
  data int
  next *Node
  prev *Node
}

func main() {
  node := Node{data: 10, next: nil, prev: nil}
  node.next.prev = &node
}
medium
A. prev pointer should not be assigned
B. node.next is nil, so node.next.prev causes a runtime panic
C. Syntax error: missing & before node initialization
D. data field must be a pointer

Solution

  1. Step 1: Analyze node initialization

    node.next is set to nil, so node.next does not point to any node.
  2. Step 2: Check node.next.prev assignment

    Accessing node.next.prev tries to dereference nil, causing runtime panic (nil pointer dereference).
  3. Final Answer:

    node.next is nil, so node.next.prev causes a runtime panic -> Option B
  4. Quick Check:

    Dereferencing nil pointer = runtime panic [OK]
Hint: Never access fields of nil pointers [OK]
Common Mistakes:
  • Ignoring nil pointer dereference
  • Thinking &node is needed for struct literal
  • Confusing data type requirements
5. You want to create a doubly linked list in Go with nodes containing values 5, 10, 15, and 20. Which code snippet correctly initializes the list and sets all next and prev pointers properly?
hard
A. head := &Node{data: 5} second := &Node{data: 10} third := &Node{data: 15} fourth := &Node{data: 20} head.next = second second.prev = head second.next = third third.prev = second third.next = fourth fourth.prev = third
B. head := Node{data: 5} second := Node{data: 10} third := Node{data: 15} fourth := Node{data: 20} head.next = &second second.prev = &head second.next = &third third.prev = &second third.next = &fourth fourth.prev = &third
C. head := &Node{data: 5, next: &Node{data: 10, prev: head}} head.next.next = &Node{data: 15, prev: head.next} head.next.next.next = &Node{data: 20, prev: head.next.next}
D. head := &Node{data: 5} head.next = &Node{data: 10, prev: head} head.next.next = &Node{data: 15, prev: head.next} head.next.next.next = &Node{data: 20, prev: head.next.next} head.next.next.next.next = nil

Solution

  1. Step 1: Check pointer usage and linking

    head := &Node{data: 5} second := &Node{data: 10} third := &Node{data: 15} fourth := &Node{data: 20} head.next = second second.prev = head second.next = third third.prev = second third.next = fourth fourth.prev = third uses pointers for all nodes and sets next and prev pointers explicitly and correctly.
  2. Step 2: Verify all links are consistent

    Each node's next points forward, and prev points backward, matching doubly linked list rules.
  3. Final Answer:

    Code snippet in Option A correctly initializes and links all nodes -> Option A
  4. Quick Check:

    Proper pointer linking = head := &Node{data: 5} second := &Node{data: 10} third := &Node{data: 15} fourth := &Node{data: 20} head.next = second second.prev = head second.next = third third.prev = second third.next = fourth fourth.prev = third [OK]
Hint: Use pointers and set both next and prev for each node [OK]
Common Mistakes:
  • Using value types instead of pointers for nodes
  • Not setting prev pointers
  • Incorrect nested initialization causing nil prev
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