0
0
DSA Pythonprogramming~20 mins

Linked List vs Array When to Choose Which in DSA Python - Compare & Choose

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Linked List vs Array Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
When is a linked list better than an array?

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?

ALinked list, because it allows easy insertion and deletion anywhere without shifting elements.
BArray, because it uses less memory and is faster for all operations.
CArray, because it allows quick insertion and deletion in the middle.
DLinked list, because it stores elements contiguously in memory.
Attempts:
2 left
💡 Hint

Think about what happens when you add or remove items in the middle of an array versus a linked list.

Predict Output
intermediate
2:00remaining
Output of inserting in array vs linked list

What is the printed state of the data structure after inserting 99 at index 2?

DSA Python
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()
A
[10, 20, 30, 99, 40]
10 -> 20 -> 30 -> 99 -> 40 -> null
B
[10, 20, 30, 40, 99]
10 -> 20 -> 30 -> 40 -> 99 -> null
C
[99, 10, 20, 30, 40]
99 -> 10 -> 20 -> 30 -> 40 -> null
D
[10, 20, 99, 30, 40]
10 -> 20 -> 99 -> 30 -> 40 -> null
Attempts:
2 left
💡 Hint

Check how insert works in array and linked list at index 2.

Predict Output
advanced
2:00remaining
Output after deleting element from linked list

What is the printed linked list after deleting the node with value 30?

DSA Python
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()
A10 -> 20 -> 30 -> 40 -> null
B10 -> 20 -> 40 -> null
C20 -> 30 -> 40 -> null
D10 -> 30 -> 40 -> null
Attempts:
2 left
💡 Hint

Think about how the linked list changes when the node with value 30 is removed.

🧠 Conceptual
advanced
2:00remaining
Why arrays are better for random access than linked lists?

Which reason best explains why arrays allow faster access to elements by index compared to linked lists?

ALinked lists store elements contiguously, making access slower.
BArrays use pointers to link elements, so traversal is faster.
CArrays store elements in continuous memory locations, so index calculation is direct.
DLinked lists use index-based addressing, which is slower than arrays.
Attempts:
2 left
💡 Hint

Think about how memory layout affects speed of accessing elements by position.

🚀 Application
expert
3:00remaining
Choosing data structure for a text editor buffer

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?

ALinked list, because it allows efficient insertions and deletions anywhere without shifting.
BArray, because it uses less memory and is faster for all operations.
CStack, because it supports fast push and pop operations.
DQueue, because it processes characters in order.
Attempts:
2 left
💡 Hint

Think about how insertions and deletions happen in the middle of the text.