Memory Layout Comparison Array vs Linked List in DSA Javascript - Complexity Comparison
We want to understand how accessing elements differs between arrays and linked lists.
How does the memory layout affect the speed of operations?
Analyze the time complexity of accessing an element at index i in an array and a linked list.
// Access element at index i in an array
function accessArray(arr, i) {
return arr[i];
}
// Access element at index i in a linked list
function accessLinkedList(head, i) {
let current = head;
let count = 0;
while (current !== null && count < i) {
current = current.next;
count++;
}
return current ? current.value : null;
}
The first function accesses an element directly by index in an array.
The second function traverses nodes one by one to reach the desired index in a linked list.
Look at what repeats when accessing the element.
- Primary operation in array: Direct access by index, no repetition.
- Primary operation in linked list: Loop through nodes one by one until index
i. - How many times: For linked list, loop runs
itimes; for array, zero loops.
Access time in array stays the same no matter the index.
Access time in linked list grows as index i grows.
| Index (i) | Array Access Operations | Linked List Access Operations |
|---|---|---|
| 10 | 1 (direct) | 10 (loop 10 times) |
| 100 | 1 (direct) | 100 (loop 100 times) |
| 1000 | 1 (direct) | 1000 (loop 1000 times) |
Pattern observation: Array access time stays constant; linked list access time grows linearly with index.
Time Complexity: O(1) for array access, O(n) for linked list access
This means accessing an element by index is instant in arrays but takes longer as the index grows in linked lists.
[X] Wrong: "Accessing any element in a linked list is as fast as in an array because both store data."
[OK] Correct: Linked lists store elements scattered in memory, so you must follow links one by one, making access slower as you go further.
Understanding how memory layout affects access speed helps you choose the right data structure for your needs.
This skill shows you can think about performance beyond just code correctness.
"What if the linked list was doubly linked? How would that affect the time complexity of accessing an element by index?"
Practice
array compared to a linked list?Solution
Step 1: Understand array memory layout
Arrays store elements in contiguous memory blocks, meaning elements are next to each other in memory.Step 2: Understand linked list memory layout
Linked list nodes are allocated separately and linked by pointers, so they can be scattered in memory.Final Answer:
Array elements are stored in contiguous memory locations, while linked list nodes are scattered in memory. -> Option CQuick Check:
Array = contiguous, Linked List = scattered [OK]
- Thinking linked list nodes are stored contiguously
- Confusing array scattered storage with linked list
- Assuming both have same memory layout
Solution
Step 1: Check array declaration syntax
Arrays in JavaScript are declared with square brackets, e.g., [1, 2, 3].Step 2: Check linked list node syntax
A linked list node is an object with properties like value and next, e.g., { value: 1, next: null }.Final Answer:
const arr = [1, 2, 3]; const node = { value: 1, next: null }; -> Option AQuick Check:
Array uses [], node uses object {} [OK]
- Using curly braces for arrays
- Using array syntax for linked list nodes
- Mixing parentheses for array declaration
const arr = [10, 20, 30];
const node1 = { value: 10, next: null };
const node2 = { value: 20, next: node1 };
const node3 = { value: 30, next: node2 };
console.log(arr[1]);
console.log(node3.next.value);What will be the output?
Solution
Step 1: Evaluate array access
arr[1] accesses the second element of the array, which is 20.Step 2: Evaluate linked list access
node3.next points to node2, whose value is 20, so node3.next.value is 20.Final Answer:
20 and 20 -> Option BQuick Check:
arr[1] = 20, node3.next.value = 20 [OK]
- Confusing linked list next pointer direction
- Mixing array indices starting at 0
- Reading node values incorrectly
function printList(head) {
let current = head;
while (current !== null) {
console.log(current.value);
current = current.value;
}
}
const node = { value: 5, next: null };
printList(node);What is the error and how to fix it?
Solution
Step 1: Identify the traversal mistake
The code sets current = current.value, which is a number, not the next node.Step 2: Correct the pointer update
To traverse, current should be updated to current.next, the pointer to the next node.Final Answer:
Error: current.value is not a node; fix by setting current = current.next; -> Option DQuick Check:
Traverse uses current = current.next [OK]
- Assigning current = current.value instead of current.next
- Not checking for null before accessing value
- Assuming value holds next node
Solution
Step 1: Analyze array insertion at start
Inserting at the start of an array requires shifting all elements, which is slow and uses extra memory temporarily.Step 2: Analyze linked list insertion at start
Linked list insertion at start only changes the head pointer and new node's next pointer, which is fast and uses minimal extra memory.Final Answer:
Linked List, because it allows quick insertion by changing pointers without shifting elements. -> Option AQuick Check:
Linked list insertion at start is faster and memory efficient [OK]
- Assuming arrays insert at start quickly
- Ignoring shifting cost in arrays
- Thinking element value affects insertion speed
Ready to prove it?
Pass the test and this pattern moves unknown
