Bird
Raised Fist0
DSA Goprogramming~20 mins

Node Structure and Pointer Design in DSA Go - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Node Structure Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Linked List Node Insertion
What is the printed output of the linked list after inserting nodes in this Go code?
DSA Go
package main
import "fmt"
type Node struct {
    value int
    next  *Node
}
func main() {
    head := &Node{value: 1}
    second := &Node{value: 2}
    third := &Node{value: 3}
    head.next = second
    second.next = third
    newNode := &Node{value: 0, next: head}
    head = newNode
    current := head
    for current != nil {
        fmt.Print(current.value)
        if current.next != nil {
            fmt.Print(" -> ")
        }
        current = current.next
    }
    fmt.Println(" -> null")
}
A0 -> 1 -> 2 -> 3 -> null
B1 -> 2 -> 3 -> 0 -> null
C0 -> 2 -> 3 -> null
D1 -> 0 -> 2 -> 3 -> null
Attempts:
2 left
💡 Hint
Think about how the new node is linked before the current head.
❓ Predict Output
intermediate
2:00remaining
Output after Reversing a Singly Linked List
What is the output of the linked list after running this reversal code?
DSA Go
package main
import "fmt"
type Node struct {
    value int
    next  *Node
}
func reverse(head *Node) *Node {
    var prev *Node = nil
    current := head
    for current != nil {
        nextNode := current.next
        current.next = prev
        prev = current
        current = nextNode
    }
    return prev
}
func main() {
    head := &Node{value: 1}
    head.next = &Node{value: 2}
    head.next.next = &Node{value: 3}
    head = reverse(head)
    current := head
    for current != nil {
        fmt.Print(current.value)
        if current.next != nil {
            fmt.Print(" -> ")
        }
        current = current.next
    }
    fmt.Println(" -> null")
}
Anull
B3 -> 2 -> 1 -> null
C1 -> 2 -> 3 -> null
D1 -> 3 -> 2 -> null
Attempts:
2 left
💡 Hint
Reversing changes the direction of the next pointers.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Node Pointer Assignment
What error will this Go code produce when trying to print the linked list?
DSA Go
package main
import "fmt"
type Node struct {
    value int
    next  *Node
}
func main() {
    var head *Node
    head.value = 1
    head.next = nil
    fmt.Println(head.value)
}
A1
B0
Cruntime error: invalid memory address or nil pointer dereference
Dcompilation error: cannot assign to struct field
Attempts:
2 left
💡 Hint
Check if head is initialized before accessing its fields.
🧠 Conceptual
advanced
2:00remaining
Effect of Changing Node Pointer in Function
Given this Go code, what will be the output after calling modifyNode?
DSA Go
package main
import "fmt"
type Node struct {
    value int
    next  *Node
}
func modifyNode(n *Node) {
    n = &Node{value: 99}
}
func main() {
    head := &Node{value: 1}
    modifyNode(head)
    fmt.Println(head.value)
}
A1
B99
C0
Dcompilation error
Attempts:
2 left
💡 Hint
Consider if the original pointer is changed or only the local copy.
🚀 Application
expert
3:00remaining
Determine the Number of Nodes in a Circular Linked List
What is the output of this Go program that counts nodes in a circular linked list?
DSA Go
package main
import "fmt"
type Node struct {
    value int
    next  *Node
}
func countNodes(head *Node) int {
    if head == nil {
        return 0
    }
    count := 1
    current := head.next
    for current != nil && current != head {
        count++
        current = current.next
    }
    return count
}
func main() {
    node1 := &Node{value: 1}
    node2 := &Node{value: 2}
    node3 := &Node{value: 3}
    node1.next = node2
    node2.next = node3
    node3.next = node1
    fmt.Println(countNodes(node1))
}
Aruntime error
B0
C1
D3
Attempts:
2 left
💡 Hint
The loop stops when it reaches the head again.

Practice

(1/5)
1. What does a next pointer in a linked list node typically represent?
easy
A. The address of the next node in the list
B. The value stored in the current node
C. The total number of nodes in the list
D. The address of the previous node in the list

Solution

  1. Step 1: Understand the role of next pointer

    The next pointer in a linked list node is used to link to the following node, allowing traversal.
  2. Step 2: Differentiate from other options

    It does not store the node's value, count nodes, or point backwards (that would be prev in doubly linked lists).
  3. Final Answer:

    The address of the next node in the list -> Option A
  4. Quick Check:

    next pointer = next node address [OK]
Hint: Remember: next always points forward in the list [OK]
Common Mistakes:
  • Confusing next with node value
  • Thinking next stores count
  • Mixing next with prev pointer
2. Which of the following is the correct way to define a singly linked list node struct in Go?
easy
A. struct Node { int value; Node* next; }
B. type Node struct { int value; Node next }
C. type Node struct { value int; next *Node }
D. type Node struct { value int; next Node }

Solution

  1. Step 1: Recall Go struct syntax for pointers

    In Go, pointers are declared with an asterisk before the type, e.g., *Node.
  2. Step 2: Check each option

    type Node struct { value int; next *Node } correctly uses Go syntax: type Node struct { value int; next *Node }. Options B and C use invalid Go syntax (C is C language style). type Node struct { value int; next Node } uses next Node which is a value, not a pointer, causing infinite recursion.
  3. Final Answer:

    type Node struct { value int; next *Node } -> Option C
  4. Quick Check:

    Pointer syntax in Go = *Type [OK]
Hint: In Go, pointers use * before type, not after [OK]
Common Mistakes:
  • Using C or Java syntax in Go
  • Forgetting * for pointer type
  • Declaring next as value type causing recursion
3. What will be the output of this Go code snippet?
package main
import "fmt"
type Node struct {
    value int
    next  *Node
}

func main() {
    n1 := &Node{value: 1}
    n2 := &Node{value: 2}
    n1.next = n2
    n2.next = nil
    current := n1
    for current != nil {
        fmt.Print(current.value, " -> ")
        current = current.next
    }
    fmt.Print("null")
}
medium
A. 2 -> 1 -> null
B. Compilation error
C. 1 -> null
D. 1 -> 2 -> null

Solution

  1. Step 1: Trace node linking

    Node n1 points to n2 via next. n2's next is nil, ending the list.
  2. Step 2: Follow the loop printing values

    Loop prints n1.value (1), then moves to n2, prints 2, then moves to nil and stops. Finally prints "null".
  3. Final Answer:

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

    Traverse from n1: 1 then 2 then end [OK]
Hint: Follow next pointers step-by-step [OK]
Common Mistakes:
  • Mixing node order
  • Forgetting to print 'null' at end
  • Assuming reversed list output
4. Identify the error in this Go code defining a linked list node:
type Node struct {
    value int
    next Node
}
medium
A. The next field should be a pointer to Node, not Node itself
B. The value field must be a pointer
C. Struct fields cannot be named next
D. Missing semicolon after value int

Solution

  1. Step 1: Understand struct field types

    Declaring next Node means the struct contains another full Node inside it, causing infinite size and recursion.
  2. Step 2: Correct usage of pointers in linked lists

    To link nodes, next must be a pointer *Node to avoid infinite nesting and allow dynamic linking.
  3. Final Answer:

    The next field should be a pointer to Node, not Node itself -> Option A
  4. Quick Check:

    next must be *Node, not Node [OK]
Hint: Use pointers for recursive struct fields [OK]
Common Mistakes:
  • Using value type causing infinite recursion
  • Thinking value field needs pointer
  • Syntax errors unrelated to pointers
5. You want to design a doubly linked list node in Go. Which struct definition correctly supports both forward and backward traversal?
hard
A. type Node struct { value int; next *Node; prev Node }
B. type Node struct { value int; next *Node; prev *Node }
C. type Node struct { value int; next Node; prev Node }
D. type Node struct { value int; next Node; prev *Node }

Solution

  1. Step 1: Understand doubly linked list node requirements

    It needs two pointers: one to the next node and one to the previous node, both as pointers to avoid infinite recursion.
  2. Step 2: Check pointer usage in options

    type Node struct { value int; next *Node; prev *Node } uses *Node for both next and prev, which is correct. Options A, B, and D use value types for either next or prev, causing invalid struct definitions.
  3. Final Answer:

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

    Doubly linked list nodes use two pointers [OK]
Hint: Both next and prev must be pointers [OK]
Common Mistakes:
  • Using value types for next or prev
  • Confusing singly and doubly linked list design
  • Forgetting pointer syntax in Go
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