Choose the best scenario where using a linked list is more suitable than an array.
Think about which data structure allows easy insertion and deletion without shifting elements.
Linked lists allow fast insertion and deletion anywhere because they use pointers, unlike arrays which require shifting elements.
Pick the main advantage of arrays compared to linked lists.
Consider which data structure supports direct access to any element quickly.
Arrays store elements contiguously, so accessing any element by index is very fast, unlike linked lists which require traversal.
What is the output after inserting element 99 at index 2 in both an array and a linked list?
package main import "fmt" type Node struct { val int next *Node } func insertArray(arr []int, index int, val int) []int { arr = append(arr, 0) copy(arr[index+1:], arr[index:]) arr[index] = val return arr } func insertLinkedList(head *Node, index int, val int) *Node { newNode := &Node{val: val} if index == 0 { newNode.next = head return newNode } curr := head for i := 0; i < index-1 && curr != nil; i++ { curr = curr.next } if curr == nil { return head } newNode.next = curr.next curr.next = newNode return head } func printLinkedList(head *Node) { for curr := head; curr != nil; curr = curr.next { fmt.Print(curr.val) if curr.next != nil { fmt.Print(" -> ") } } fmt.Println(" -> null") } func main() { arr := []int{10, 20, 30, 40} arr = insertArray(arr, 2, 99) fmt.Println(arr) head := &Node{val: 10} head.next = &Node{val: 20} head.next.next = &Node{val: 30} head.next.next.next = &Node{val: 40} head = insertLinkedList(head, 2, 99) printLinkedList(head) }
Remember that insertion at index 2 means the new element goes before the old element at index 2.
Inserting 99 at index 2 shifts elements in the array and inserts the node at the correct position in the linked list.
What happens when you try to access index 5 in an array of length 4 and traverse 5 nodes in a linked list of length 4?
package main import "fmt" func main() { arr := []int{1, 2, 3, 4} fmt.Println(arr[5]) type Node struct { val int next *Node } head := &Node{val: 1} head.next = &Node{val: 2} head.next.next = &Node{val: 3} head.next.next.next = &Node{val: 4} curr := head for i := 0; i < 5; i++ { if curr == nil { fmt.Println("Reached end of linked list") break } fmt.Println(curr.val) curr = curr.next } }
Think about how Go handles out-of-bounds array access and linked list traversal.
Accessing arr[5] causes a runtime panic because the index is out of range. Linked list traversal stops safely when reaching nil.
You are designing a text editor. The buffer needs to support fast insertions and deletions anywhere, and also fast sequential access for rendering. Which data structure is best?
Consider which data structure balances fast insertions/deletions and sequential access.
Linked lists allow fast insertions and deletions anywhere without shifting elements, which is important for text editing. Arrays have slow insertions/deletions in the middle.
