Bird
Raised Fist0
DSA Goprogramming~20 mins

Get Length of Linked List in DSA Go - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Linked List Length Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the length of the linked list after insertion?
Consider the following Go code that creates a linked list and inserts nodes. What will be the printed length of the linked list?
DSA Go
package main
import "fmt"
type Node struct {
    value int
    next  *Node
}
func getLength(head *Node) int {
    length := 0
    current := head
    for current != nil {
        length++
        current = current.next
    }
    return length
}
func main() {
    head := &Node{value: 10}
    head.next = &Node{value: 20}
    head.next.next = &Node{value: 30}
    fmt.Println(getLength(head))
}
A3
B2
C4
D0
Attempts:
2 left
💡 Hint
Count how many nodes are linked starting from head.
❓ Predict Output
intermediate
2:00remaining
What is the output when the linked list is empty?
What will be the output of the following Go code when the linked list is empty (head is nil)?
DSA Go
package main
import "fmt"
type Node struct {
    value int
    next  *Node
}
func getLength(head *Node) int {
    length := 0
    current := head
    for current != nil {
        length++
        current = current.next
    }
    return length
}
func main() {
    var head *Node = nil
    fmt.Println(getLength(head))
}
Anil
B1
C0
Druntime error
Attempts:
2 left
💡 Hint
An empty list has no nodes.
🔧 Debug
advanced
2:00remaining
Why does this code print 0 instead of the correct length?
The following Go code is intended to print the length of the linked list, but it always prints 0. What is the reason?
DSA Go
package main
import "fmt"
type Node struct {
    value int
    next  *Node
}
func getLength(head *Node) int {
    length := 0
    for head != nil {
        length++
    }
    return length
}
func main() {
    head := &Node{value: 1}
    head.next = &Node{value: 2}
    fmt.Println(getLength(head))
}
AThe linked list is empty, so length is zero.
BThe length variable is not initialized to zero.
CThe function getLength is not called in main.
DThe loop never advances head to the next node, causing an infinite loop and no length increment.
Attempts:
2 left
💡 Hint
Check if the loop moves to the next node.
🧠 Conceptual
advanced
1:00remaining
What is the time complexity of getting the length of a singly linked list?
Given a singly linked list with n nodes, what is the time complexity of a function that returns its length by traversing the list once?
AO(n)
BO(1)
CO(n^2)
DO(log n)
Attempts:
2 left
💡 Hint
The function visits each node once.
❓ Predict Output
expert
2:30remaining
What is the output of this recursive length function?
What will be the output of the following Go code that calculates the length of a linked list recursively?
DSA Go
package main
import "fmt"
type Node struct {
    value int
    next  *Node
}
func getLengthRecursive(head *Node) int {
    if head == nil {
        return 0
    }
    return 1 + getLengthRecursive(head.next)
}
func main() {
    head := &Node{value: 5}
    head.next = &Node{value: 10}
    head.next.next = &Node{value: 15}
    head.next.next.next = &Node{value: 20}
    fmt.Println(getLengthRecursive(head))
}
Aruntime error
B4
C5
D3
Attempts:
2 left
💡 Hint
Count nodes recursively until nil is reached.

Practice

(1/5)
1. What does the length of a linked list represent?
easy
A. The memory size of the linked list
B. The value of the first node
C. The number of nodes in the linked list
D. The sum of all node values

Solution

  1. Step 1: Understand linked list structure

    A linked list is made of nodes connected one after another.
  2. Step 2: Define length meaning

    The length counts how many nodes are present in the list.
  3. Final Answer:

    The number of nodes in the linked list -> Option C
  4. Quick Check:

    Length = count of nodes [OK]
Hint: Length means count nodes, not values or memory [OK]
Common Mistakes:
  • Confusing length with sum of values
  • Thinking length is memory size
  • Using first node value as length
2. Which Go code snippet correctly initializes a variable to count the length of a linked list?
easy
A. length := 0
B. length := nil
C. length := "0"
D. length := false

Solution

  1. Step 1: Identify correct variable type for counting

    Length is a number, so it should be an integer starting at zero.
  2. Step 2: Check Go syntax for initialization

    Using length := 0 correctly declares an int variable with zero value.
  3. Final Answer:

    length := 0 -> Option A
  4. Quick Check:

    Count variable starts at 0 [OK]
Hint: Count starts at zero as int, not nil or string [OK]
Common Mistakes:
  • Using nil instead of zero
  • Using string "0" instead of int 0
  • Using boolean false instead of number
3. What is the output of this Go code that counts the length of a linked list?
type Node struct {
    value int
    next  *Node
}

func getLength(head *Node) int {
    length := 0
    current := head
    for current != nil {
        length++
        current = current.next
    }
    return length
}

// Linked list: 10 -> 20 -> 30 -> nil
head := &Node{10, &Node{20, &Node{30, nil}}}
fmt.Println(getLength(head))
medium
A. 2
B. 3
C. 0
D. Runtime error

Solution

  1. Step 1: Trace the loop through nodes

    Start at head (10), count 1, move to next (20), count 2, move to next (30), count 3, next is nil, stop.
  2. Step 2: Return the count

    The loop counted 3 nodes, so length is 3.
  3. Final Answer:

    3 -> Option B
  4. Quick Check:

    Nodes counted = 3 [OK]
Hint: Count increments once per node until nil [OK]
Common Mistakes:
  • Stopping loop too early
  • Not incrementing length inside loop
  • Confusing nil check
4. Find the error in this Go function that tries to get the length of a linked list:
func getLength(head *Node) int {
    length := 0
    for head.next != nil {
        length++
        head = head.next
    }
    return length
}
medium
A. It misses counting the last node
B. It causes a nil pointer dereference
C. It counts nodes twice
D. It returns length as a string

Solution

  1. Step 1: Analyze loop condition

    The loop runs while head.next != nil, so it stops before the last node.
  2. Step 2: Check counting logic

    Since last node's next is nil, it is not counted, so length is one less than actual.
  3. Final Answer:

    It misses counting the last node -> Option A
  4. Quick Check:

    Loop condition excludes last node [OK]
Hint: Loop until current != nil to count all nodes [OK]
Common Mistakes:
  • Using head.next != nil instead of head != nil
  • Not counting last node
  • Assuming loop covers all nodes
5. Given a linked list where each node contains an integer, how would you modify the length function to count only nodes with even values?
func getLength(head *Node) int {
    length := 0
    current := head
    for current != nil {
        // Your condition here
        current = current.next
    }
    return length
}
hard
A. Return length without any loop
B. Increment length unconditionally inside the loop
C. Check if current.value > 0 before incrementing length
D. Add if current.value%2 == 0 { length++ } inside the loop

Solution

  1. Step 1: Add condition to count only even values

    Inside the loop, check if current.value is divisible by 2 (even).
  2. Step 2: Increment length only if condition is true

    Use if current.value%2 == 0 { length++ } to count even nodes.
  3. Final Answer:

    Add if current.value%2 == 0 { length++ } inside the loop -> Option D
  4. Quick Check:

    Count only even nodes [OK]
Hint: Use modulo 2 check inside loop to count even nodes [OK]
Common Mistakes:
  • Counting all nodes without condition
  • Checking wrong condition like > 0
  • Skipping loop entirely
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