What is a key difference between arrays and linked lists in terms of memory layout?
Arrays store elements in contiguous memory locations, while linked lists store elements in nodes scattered in memory connected by pointers.
Click to reveal answer
beginner
Which data structure allows faster insertion and deletion at the beginning or middle: array or linked list?
Linked lists allow faster insertion and deletion at the beginning or middle because they only need to update pointers, while arrays require shifting elements.
Click to reveal answer
beginner
Why might arrays be better than linked lists for accessing elements by index?
Arrays provide constant time (O(1)) access to elements by index because elements are stored contiguously, while linked lists require traversal from the head node (O(n)).
Click to reveal answer
intermediate
When should you choose a linked list over an array?
Choose a linked list when you need frequent insertions and deletions in the middle or beginning of the list and don't need fast random access.
Click to reveal answer
intermediate
What is a disadvantage of linked lists compared to arrays?
Linked lists use extra memory for storing pointers and have slower access time for elements by index compared to arrays.
Click to reveal answer
Which data structure provides O(1) time complexity for accessing elements by index?
ANeither
BLinked List
CBoth
DArray
✗ Incorrect
Arrays store elements contiguously, allowing direct access by index in constant time.
Which data structure is better for frequent insertions at the start of the list?
ALinked List
BArray
CBoth are equal
DDepends on size
✗ Incorrect
Linked lists can insert at the start by updating pointers without shifting elements.
What is a downside of using linked lists compared to arrays?
ASlower insertion
BMore memory usage
CFaster access by index
DFixed size
✗ Incorrect
Linked lists require extra memory for pointers in each node.
If you need fast random access and rarely modify data, which should you choose?
AEither works
BLinked List
CArray
DHash Table
✗ Incorrect
Arrays provide fast random access, ideal when modifications are rare.
Which operation is costly in arrays but cheap in linked lists?
AInsertion in the middle
BAccess by index
CTraversal
DAppending at the end
✗ Incorrect
Insertion in the middle requires shifting elements in arrays but just pointer updates in linked lists.
Explain when you would choose a linked list over an array and why.
Think about operations that involve changing the list structure often.
You got /4 concepts.
Describe the main advantages and disadvantages of arrays compared to linked lists.
Consider access speed and modification costs.
You got /4 concepts.
Practice
(1/5)
1. Which data structure is generally better for frequent insertions and deletions in the middle of the collection?
easy
A. Array
B. Neither is suitable
C. Both are equally efficient
D. Linked List
Solution
Step 1: Understand insertion and deletion costs in arrays
Arrays require shifting elements when inserting or deleting in the middle, which is costly.
Step 2: Understand linked list insertion and deletion
Linked lists can insert or delete nodes by changing pointers without shifting elements.
Final Answer:
Linked List -> Option D
Quick Check:
Insert/delete middle = Linked List [OK]
Hint: Choose linked list for fast middle insert/delete [OK]
2. Which of the following is the correct way to declare an empty linked list node in JavaScript?
easy
A. const node = { value: null, next: null };
B. const node = [null, null];
C. const node = null;
D. const node = new Array();
Solution
Step 1: Understand linked list node structure
A linked list node typically has a value and a pointer to the next node, represented as an object with 'value' and 'next' properties.
Step 2: Check each option
const node = { value: null, next: null }; correctly creates an object with 'value' and 'next' set to null. Others do not represent a linked list node properly.
arr.splice(1, 1, 4) means start at index 1, remove 1 element, then insert 4 at that position.
Step 2: Apply splice to array
Original array is [1, 2, 3]. Removing element at index 1 (which is 2) and inserting 4 results in [1, 4, 3].
Final Answer:
[1, 4, 3] -> Option A
Quick Check:
splice replaces element at index 1 with 4 [OK]
Hint: splice(start, deleteCount, item) replaces elements [OK]
Common Mistakes:
Thinking splice inserts without deleting
Confusing splice with slice
Expecting original array unchanged
4. Identify the error in the following linked list insertion code snippet:
function insertAtHead(head, value) {
const newNode = { value: value, next: head };
head = newNode;
return head;
}
let head = null;
insertAtHead(head, 5);
console.log(head);
medium
A. newNode is not created correctly
B. The head variable is not updated outside the function
C. The function should not return head
D. The next pointer should be null
Solution
Step 1: Understand variable scope in JavaScript
Inside the function, head is a local parameter. Assigning head = newNode changes only local copy.
Step 2: Check effect on external head variable
Outside the function, head remains null because the updated head inside the function is not reflected outside.
Final Answer:
The head variable is not updated outside the function -> Option B
Quick Check:
Function parameter reassignment doesn't affect external variable [OK]
Hint: Return and assign new head to update linked list [OK]
Common Mistakes:
Assuming head changes globally without return
Not returning the new head
Misunderstanding pointer assignment
5. You need to store a large collection of items where you frequently add elements at the end and access elements by index quickly. Which data structure should you choose and why?
hard
A. Linked List, because it allows fast access by index
B. Linked List, because it uses less memory than arrays
C. Array, because it allows fast access by index and efficient append
D. Array, because it allows fast insertion and deletion in the middle
Solution
Step 1: Analyze access by index requirement
Arrays provide constant time access by index, linked lists require traversal which is slower.
Step 2: Analyze frequent append requirement
Arrays can append efficiently if capacity allows; linked lists also append efficiently but slower access is a drawback.
Final Answer:
Array, because it allows fast access by index and efficient append -> Option C
Quick Check:
Fast index access + append = Array [OK]
Hint: Use array for fast index access and appending [OK]
Common Mistakes:
Choosing linked list for fast index access
Assuming linked list uses less memory always
Confusing insertion in middle with append
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.