Bird
Raised Fist0
DSA Goprogramming~10 mins

Why Doubly Linked List Over Singly Linked List in DSA Go - Test Your Knowledge

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to add a new node at the end of a singly linked list.

DSA Go
func (list *SinglyLinkedList) Append(value int) {
    newNode := &Node{value: value, next: nil}
    if list.head == nil {
        list.head = newNode
        return
    }
    current := list.head
    for current.next [1] nil {
        current = current.next
    }
    current.next = newNode
}
Drag options to blanks, or click blank then click option'
A!=
B==
C<
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '!=' causes the loop to stop too early.
Using '<' or '>' is invalid for pointer comparison.
2fill in blank
medium

Complete the code to insert a new node before a given node in a doubly linked list.

DSA Go
func (list *DoublyLinkedList) InsertBefore(node *Node, value int) {
    newNode := &Node{value: value}
    newNode.next = node
    newNode.prev = node.prev
    if node.prev != nil {
        node.prev.[1] = newNode
    } else {
        list.head = newNode
    }
    node.prev = newNode
}
Drag options to blanks, or click blank then click option'
Avalue
Bnext
Cprev
Dhead
Attempts:
3 left
💡 Hint
Common Mistakes
Updating the wrong pointer like 'prev' instead of 'next'.
Not updating the head when inserting at the start.
3fill in blank
hard

Fix the error in the code to remove a node from a doubly linked list.

DSA Go
func (list *DoublyLinkedList) Remove(node *Node) {
    if node.prev != nil {
        node.prev.next = node.next
    } else {
        list.head = node.next
    }
    if node.next != nil {
        node.next.[1] = node.prev
    }
}
Drag options to blanks, or click blank then click option'
Ahead
Bnext
Cprev
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Updating 'next' pointer instead of 'prev'.
Not updating pointers leading to broken links.
4fill in blank
hard

Fill both blanks to traverse a doubly linked list backwards from the tail.

DSA Go
func (list *DoublyLinkedList) TraverseBackward() []int {
    var result []int
    current := list.tail
    for current != nil {
        result = append(result, current.value)
        current = current.[1]
    }
    return result
}
Drag options to blanks, or click blank then click option'
Aprev
Btail
Chead
Dnext
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'next' pointer causes infinite loop or wrong traversal.
Confusing 'head' or 'tail' as pointers to move.
5fill in blank
hard

Fill all three blanks to create a map of node values to their previous node's value in a doubly linked list.

DSA Go
func (list *DoublyLinkedList) MapPrevValues() map[int]int {
    result := make(map[int]int)
    current := list.head
    for current != nil {
        if current.prev != nil {
            result[current.[1]] = current.prev.[2]
        } else {
            result[current.value] = [3]
        }
        current = current.next
    }
    return result
}
Drag options to blanks, or click blank then click option'
Avalue
C-1
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong field names instead of 'value'.
Using 0 instead of -1 for no previous node.

Practice

(1/5)
1. Why might you choose a doubly linked list over a singly linked list in a program?
easy
A. Because it uses less memory per node
B. Because it only stores one pointer per node
C. Because it is simpler to implement
D. Because it allows traversal in both directions easily

Solution

  1. Step 1: Understand traversal capabilities

    A doubly linked list stores two pointers per node: one to the next node and one to the previous node, allowing movement forward and backward.
  2. Step 2: Compare with singly linked list

    A singly linked list stores only one pointer to the next node, so it can only be traversed forward easily.
  3. Final Answer:

    Because it allows traversal in both directions easily -> Option D
  4. Quick Check:

    Traversal both ways = B [OK]
Hint: Doubly means two directions for traversal [OK]
Common Mistakes:
  • Thinking doubly linked lists use less memory
  • Assuming singly linked lists allow backward traversal
  • Confusing simplicity of implementation
2. Which of the following is the correct way to declare a node struct for a doubly linked list in Go?
easy
A. type Node struct { data int; prev *Node; next *Node }
B. type Node struct { data int; next *Node }
C. type Node struct { data int; prev int; next int }
D. type Node struct { data int; next int }

Solution

  1. Step 1: Identify required pointers for doubly linked list node

    A doubly linked list node must have pointers to both previous and next nodes, so it needs two pointers of type *Node.
  2. Step 2: Check each option's struct fields

    type Node struct { data int; prev *Node; next *Node } correctly declares both prev *Node and next *Node. Others miss one or use wrong types.
  3. Final Answer:

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

    Two pointers prev and next = A [OK]
Hint: Doubly linked node needs prev and next pointers [OK]
Common Mistakes:
  • Using int instead of pointer for prev/next
  • Missing prev pointer in struct
  • Confusing singly linked list node with doubly linked
3. What will be the output of the following Go code snippet that uses a doubly linked list?
type Node struct {
    data int
    prev *Node
    next *Node
}

func printForward(head *Node) {
    for current := head; current != nil; current = current.next {
        fmt.Print(current.data, " ")
    }
}

func printBackward(tail *Node) {
    for current := tail; current != nil; current = current.prev {
        fmt.Print(current.data, " ")
    }
}

func main() {
    n1 := &Node{data: 1}
    n2 := &Node{data: 2}
    n3 := &Node{data: 3}

    n1.next = n2
    n2.prev = n1
    n2.next = n3
    n3.prev = n2

    printForward(n1)
    fmt.Println()
    printBackward(n3)
}
medium
A. 1 2 3 \n 1 2 3
B. 3 2 1 \n 1 2 3
C. 1 2 3 \n 3 2 1
D. 3 2 1 \n 3 2 1

Solution

  1. Step 1: Trace printForward function

    Starting at n1 (data=1), it prints 1, then moves to n2 (2), then n3 (3), then nil. Output: "1 2 3 "
  2. Step 2: Trace printBackward function

    Starting at n3 (3), it prints 3, then moves to n2 (2), then n1 (1), then nil. Output: "3 2 1 "
  3. Final Answer:

    1 2 3 \n 3 2 1 -> Option C
  4. Quick Check:

    Forward then backward print = A [OK]
Hint: Forward prints from head, backward from tail [OK]
Common Mistakes:
  • Confusing prev and next pointers in loops
  • Printing backward list in forward order
  • Missing nil check causing runtime error
4. The following Go code tries to insert a new node after a given node in a doubly linked list. What is the bug?
func insertAfter(node *Node, newNode *Node) {
    newNode.next = node.next
    node.next = newNode
    newNode.prev = node
}
medium
A. It does not set node.next correctly
B. It does not update the prev pointer of the node after newNode
C. It does not set newNode.prev correctly
D. It does not set newNode.next correctly

Solution

  1. Step 1: Analyze pointer updates in insertAfter

    The code sets newNode.next to node.next and newNode.prev to node, and updates node.next to newNode.
  2. Step 2: Identify missing pointer update

    If node.next was not nil, the previous pointer of node.next (the node after newNode) should be updated to newNode. This step is missing.
  3. Final Answer:

    It does not update the prev pointer of the node after newNode -> Option B
  4. Quick Check:

    Missing prev update of next node = C [OK]
Hint: Always update prev pointer of next node after insertion [OK]
Common Mistakes:
  • Forgetting to update next node's prev pointer
  • Confusing prev and next pointers
  • Assuming node.next is always nil
5. You want to implement a feature that requires frequent backward traversal and deletion of nodes from the middle of a list. Why is a doubly linked list better than a singly linked list for this task?
hard
A. Because doubly linked lists allow backward traversal and easy access to previous nodes for deletion
B. Because singly linked lists use less memory and are faster for deletion
C. Because doubly linked lists do not need pointers to previous nodes
D. Because singly linked lists allow backward traversal without extra pointers

Solution

  1. Step 1: Understand backward traversal requirement

    Backward traversal means moving from a node to its previous node, which singly linked lists cannot do easily because they only have next pointers.
  2. Step 2: Understand deletion from middle nodes

    To delete a node, you need to update the previous node's next pointer. Doubly linked lists have direct access to previous nodes, making deletion easier and efficient.
  3. Final Answer:

    Because doubly linked lists allow backward traversal and easy access to previous nodes for deletion -> Option A
  4. Quick Check:

    Backward traversal and deletion ease = D [OK]
Hint: Backward traversal needs prev pointers for easy deletion [OK]
Common Mistakes:
  • Thinking singly linked lists can traverse backward easily
  • Ignoring need to update previous node on deletion
  • Assuming doubly linked lists use less memory
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