Challenge - 5 Problems
Linked List Length Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat 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)) }
Attempts:
2 left
💡 Hint
Count how many nodes are linked starting from head.
✗ Incorrect
The linked list has three nodes: 10 -> 20 -> 30 -> nil. The getLength function counts each node until it reaches nil, so the length is 3.
❓ Predict Output
intermediateWhat 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)) }
Attempts:
2 left
💡 Hint
An empty list has no nodes.
✗ Incorrect
Since head is nil, the loop does not run and length remains 0. So the output is 0.
🔧 Debug
advancedWhy 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)) }
Attempts:
2 left
💡 Hint
Check if the loop moves to the next node.
✗ Incorrect
Inside the loop, head is never updated to head.next, so the loop runs infinitely without incrementing length. This causes the function to never return the correct length.
🧠 Conceptual
advancedWhat 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?
Attempts:
2 left
💡 Hint
The function visits each node once.
✗ Incorrect
The function must visit each node once to count them, so the time complexity is linear, O(n).
❓ Predict Output
expertWhat 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)) }
Attempts:
2 left
💡 Hint
Count nodes recursively until nil is reached.
✗ Incorrect
The recursive function counts one node and calls itself on the next node until it reaches nil. There are 4 nodes, so output is 4.
