Imagine you need to store a list of items that will change often by adding or removing elements in the middle. Which data structure is better to use?
Think about what happens when you add or remove items in the middle of an array versus a linked list.
Linked lists let you add or remove elements anywhere by changing pointers, without moving other elements. Arrays need to shift elements, which is slower.
What is the printed state of the data structure after inserting 99 at index 2?
arr = [10, 20, 30, 40] arr.insert(2, 99) print(arr) class Node: def __init__(self, val): self.val = val self.next = None class LinkedList: def __init__(self): self.head = None def append(self, val): if not self.head: self.head = Node(val) return curr = self.head while curr.next: curr = curr.next curr.next = Node(val) def insert_at(self, index, val): new_node = Node(val) if index == 0: new_node.next = self.head self.head = new_node return curr = self.head for _ in range(index - 1): if curr is None: return curr = curr.next new_node.next = curr.next curr.next = new_node def print_list(self): curr = self.head res = [] while curr: res.append(str(curr.val)) curr = curr.next print(" -> ".join(res) + " -> null") ll = LinkedList() for v in [10, 20, 30, 40]: ll.append(v) ll.insert_at(2, 99) ll.print_list()
Check how insert works in array and linked list at index 2.
Both array and linked list insert 99 at position 2, shifting elements after it.
What is the printed linked list after deleting the node with value 30?
class Node: def __init__(self, val): self.val = val self.next = None class LinkedList: def __init__(self): self.head = None def append(self, val): if not self.head: self.head = Node(val) return curr = self.head while curr.next: curr = curr.next curr.next = Node(val) def delete_value(self, val): curr = self.head prev = None while curr: if curr.val == val: if prev: prev.next = curr.next else: self.head = curr.next return prev = curr curr = curr.next def print_list(self): curr = self.head res = [] while curr: res.append(str(curr.val)) curr = curr.next print(" -> ".join(res) + " -> null") ll = LinkedList() for v in [10, 20, 30, 40]: ll.append(v) ll.delete_value(30) ll.print_list()
Think about how the linked list changes when the node with value 30 is removed.
Deleting node 30 removes it and links 20 directly to 40.
Which reason best explains why arrays allow faster access to elements by index compared to linked lists?
Think about how memory layout affects speed of accessing elements by position.
Arrays store elements one after another in memory, so accessing by index is quick using simple math. Linked lists require following pointers step-by-step.
You are designing a text editor. The user can insert and delete characters anywhere in the text frequently. Which data structure is best to represent the text buffer for efficient editing?
Think about how insertions and deletions happen in the middle of the text.
Linked lists allow quick insertions and deletions anywhere by changing pointers, which is ideal for text editing. Arrays require shifting elements, which is slower.