Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to insert a new node at the given position in the linked list.
DSA Go
func InsertAtPosition(head *Node, data int, pos int) *Node {
newNode := &Node{data: data}
if pos == 1 {
newNode.next = head
return newNode
}
current := head
for i := 1; i < [1]-1 && current != nil; i++ {
current = current.next
}
if current == nil {
return head
}
newNode.next = current.next
current.next = newNode
return head
}Attempts:
3 left
💡 Hint
Common Mistakes
Using data or newNode instead of pos in the loop condition.
Looping until pos instead of pos-1 causing off-by-one error.
✗ Incorrect
The loop runs until the node before the position where the new node will be inserted, so it should run until pos-1.
2fill in blank
mediumComplete the code to check if the position is valid before insertion.
DSA Go
func InsertAtPosition(head *Node, data int, pos int) *Node {
if pos <= 0 {
return head
}
length := 0
current := head
for current != nil {
length++
current = current.next
}
if pos > [1]+1 {
return head
}
// insertion code continues...
return head
}Attempts:
3 left
💡 Hint
Common Mistakes
Comparing position with pos or data instead of length.
Allowing position greater than length+1 causing invalid insertion.
✗ Incorrect
The position should not be greater than length+1 because you can insert at the end (length+1).
3fill in blank
hardFix the error in the loop that traverses to the node before insertion.
DSA Go
func InsertAtPosition(head *Node, data int, pos int) *Node {
newNode := &Node{data: data}
if pos == 1 {
newNode.next = head
return newNode
}
current := head
for i := 1; i < [1]-1 && current != nil; i++ {
current = current.next
}
if current == nil {
return head
}
newNode.next = current.next
current.next = newNode
return head
}Attempts:
3 left
💡 Hint
Common Mistakes
Using <= instead of < causing the loop to move one node too far.
Using data or newNode instead of pos in the loop condition.
✗ Incorrect
The loop should run while i < pos-1, not i <= pos-1, to stop at the node before insertion.
4fill in blank
hardFill both blanks to create a map of word lengths for words longer than 3 characters.
DSA Go
lengths := map[string]int{}
for _, word := range words {
if len(word) [1] 3 {
lengths[word] = [2]
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of > causing wrong filtering.
Assigning word instead of len(word) as value.
✗ Incorrect
We want words longer than 3, so len(word) > 3, and the value is the length of the word.
5fill in blank
hardFill all three blanks to create a filtered map with values greater than 0.
DSA Go
result := map[string]int{}
for k, v := range data {
if v [3] 0 {
result[[1]] = [2]
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using k.upper() which is not valid Go syntax.
Using < instead of > causing wrong filtering.
✗ Incorrect
We check if v > 0, then assign result[k] = v. The key is k, value is v, and condition is > 0.
