An array stores elements in a continuous block of memory, where each element is placed right after the previous one.
Click to reveal answer
beginner
How is memory allocated for a linked list?
A linked list allocates memory for each element (node) separately and links them using pointers, so nodes can be scattered in memory.
Click to reveal answer
intermediate
Why can arrays be faster to access than linked lists?
Because arrays store elements continuously, the computer can quickly calculate the address of any element using its index, making access faster.
Click to reveal answer
intermediate
What is a disadvantage of linked lists in terms of memory?
Linked lists use extra memory for storing pointers in each node, which adds overhead compared to arrays.
Click to reveal answer
advanced
How does fragmentation affect linked lists?
Since linked list nodes can be scattered in memory, they may cause fragmentation, which can slow down access due to poor cache usage.
Click to reveal answer
Which data structure stores elements in a continuous block of memory?
AArray
BLinked List
CTree
DGraph
✗ Incorrect
Arrays store elements continuously, while linked lists store nodes separately.
What extra information does each node in a linked list store besides the data?
AMemory address of array
BIndex
CSize of list
DPointer to next node
✗ Incorrect
Each linked list node stores a pointer to the next node to link the list.
Why might accessing elements in a linked list be slower than in an array?
ABecause linked lists use more memory
BBecause nodes are scattered in memory, causing cache misses
CBecause linked lists store elements continuously
DBecause arrays use pointers
✗ Incorrect
Scattered nodes cause poor cache usage, slowing access.
Which data structure requires less memory overhead per element?
ALinked List
BBoth same
CArray
DDepends on data
✗ Incorrect
Arrays only store data, linked lists store data plus pointers.
What is a key advantage of linked lists over arrays?
ADynamic size and easy insertion/deletion
BFaster random access
CUses less memory
DElements stored continuously
✗ Incorrect
Linked lists can grow/shrink easily and insert/delete nodes without shifting elements.
Explain the difference in memory layout between arrays and linked lists.
Think about how elements are placed in memory and how that affects access.
You got /4 concepts.
Describe how memory layout affects performance in arrays and linked lists.
Consider how the computer reads memory and how that impacts speed.
You got /4 concepts.
Practice
(1/5)
1. Which of the following best describes the memory layout of an array compared to a linked list?
easy
A. Both array and linked list elements are scattered randomly in memory.
B. Both array and linked list elements are stored contiguously in memory.
C. Linked list elements are stored contiguously, but array elements are scattered in memory.
D. Array elements are stored in contiguous memory locations, while linked list nodes are scattered in memory.
Solution
Step 1: Understand array memory layout
Arrays store elements in a continuous block of memory, one after another.
Step 2: Understand linked list memory layout
Linked list nodes are allocated separately and linked via pointers, so they are scattered.
Final Answer:
Array elements are stored in contiguous memory locations, while linked list nodes are scattered in memory. -> Option D
Quick Check:
Array = contiguous, Linked list = scattered [OK]
Hint: Arrays are continuous; linked lists use pointers to scattered nodes [OK]
Common Mistakes:
Thinking linked list nodes are stored contiguously
Confusing array and linked list memory layouts
Assuming both have same memory layout
2. Which Go syntax correctly declares an array of 5 integers?
easy
A. var arr []int = [5]
B. arr := []int{5}
C. var arr [5]int
D. arr := [5]int
Solution
Step 1: Recall Go array declaration syntax
Arrays are declared with fixed size inside square brackets before the type, e.g., [5]int.
Step 2: Check each option
A: var arr [5]int uses correct syntax. B: arr := []int{5} creates a slice with one element. C: var arr []int = [5] is invalid syntax. D: arr := [5]int is invalid syntax - missing composite literal {}.
Final Answer:
var arr [5]int -> Option C
Quick Check:
Array declaration = var arr [5]int [OK]
Hint: Array size goes inside brackets before type in Go [OK]
Common Mistakes:
Using slice syntax instead of array syntax
Placing size after type
Incorrect initialization syntax
3. Consider this Go code snippet:
package main
import "fmt"
type Node struct {
value int
next *Node
}
func main() {
var head *Node
head = &Node{value: 1}
head.next = &Node{value: 2}
head.next.next = &Node{value: 3}
arr := [3]int{1, 2, 3}
fmt.Println(arr)
fmt.Println(head.value, head.next.value, head.next.next.value)
}
What will be the output?
medium
A. [1 2 3]
3 2 1
B. [1 2 3]
1 2 3
C. [1 2 3]
1 3 2
D. Compilation error
Solution
Step 1: Understand array print output
Array arr contains [1, 2, 3], so fmt.Println(arr) prints [1 2 3].
Step 2: Understand linked list traversal print
head.value = 1, head.next.value = 2, head.next.next.value = 3, so printing them in order prints '1 2 3'.
Final Answer:
[1 2 3]
1 2 3 -> Option B
Quick Check:
Array and linked list values match printed order [OK]
Hint: Print array and linked list values in order to verify [OK]
Common Mistakes:
Confusing linked list node order
Expecting commas in array print
Assuming compilation error due to pointers
4. The following Go code attempts to create a linked list but has an error:
type Node struct {
value int
next *Node
}
func main() {
var head *Node
head.value = 10
head.next = nil
}
What is the error and how to fix it?
medium
A. head is nil pointer; initialize head before assigning fields
B. next field should be value, not pointer
C. value field must be a pointer type
D. No error; code runs fine
Solution
Step 1: Identify pointer initialization issue
head is declared as *Node but not initialized, so it is nil (no memory allocated).
Step 2: Fix by allocating memory for head
Use head = &Node{} before assigning head.value and head.next to avoid nil pointer dereference.
Final Answer:
head is nil pointer; initialize head before assigning fields -> Option A
Quick Check:
Nil pointer must be initialized before use [OK]
Hint: Always allocate memory for pointer before field access [OK]
Common Mistakes:
Assigning fields on nil pointer
Changing field types unnecessarily
Ignoring runtime panic from nil dereference
5. You want to store 1 million integers and frequently insert and delete elements in the middle. Considering memory layout and performance, which data structure is better and why?
Array
Linked List
hard
A. Linked List, because scattered nodes make insertions and deletions faster despite poor cache usage
B. Linked List, because arrays cannot store large data
C. Array, because insertions and deletions are always faster in arrays
D. Array, because contiguous memory improves cache performance despite costly insertions
Solution
Step 1: Analyze insertion/deletion cost in arrays
Arrays store data contiguously, so inserting or deleting in the middle requires shifting many elements, which is slow.
Step 2: Analyze linked list insertion/deletion
Linked lists store nodes scattered but allow fast insert/delete by changing pointers without shifting elements.
Step 3: Consider memory and cache trade-offs
Arrays have better cache performance due to contiguous memory, but linked lists are better for frequent middle insertions/deletions.
Final Answer:
Linked List, because scattered nodes make insertions and deletions faster despite poor cache usage -> Option A
Quick Check:
Frequent middle insert/delete favors linked list [OK]