Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to add a new node at the end of a singly linked list.
DSA Python
class Node: def __init__(self, data): self.data = data self.next = None class SinglyLinkedList: def __init__(self): self.head = None def append(self, data): new_node = Node(data) if not self.head: self.head = new_node return last = self.head while last.next: last = last.[1] last.next = new_node
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' which does not exist in singly linked list nodes.
Trying to access 'head' from a node instead of moving to the next node.
✗ Incorrect
In a singly linked list, each node points to the next node using the 'next' attribute. To traverse the list, we move from one node to the next using 'next'.
2fill in blank
mediumComplete the code to add a new node at the end of a doubly linked list.
DSA Python
class Node: def __init__(self, data): self.data = data self.next = None self.prev = None class DoublyLinkedList: def __init__(self): self.head = None def append(self, data): new_node = Node(data) if not self.head: self.head = new_node return last = self.head while last.next: last = last.next last.next = new_node new_node.[1] = last
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting 'next' instead of 'prev' for the new node's backward link.
Forgetting to set the 'prev' pointer at all.
✗ Incorrect
In a doubly linked list, each node has a 'prev' pointer to the previous node. When adding a new node at the end, we set the new node's 'prev' to the last node.
3fill in blank
hardFix the error in the code that tries to traverse a doubly linked list backwards from the tail.
DSA Python
def traverse_backward(tail): current = tail while current: print(current.data) current = current.[1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'next' which moves forward, not backward.
Trying to access 'head' from a node which is not a pointer.
✗ Incorrect
To move backwards in a doubly linked list, we use the 'prev' pointer. Using 'next' would move forward and cause incorrect traversal.
4fill in blank
hardComplete the code to create a dictionary comprehension that maps each word to its length only if the length is greater than 3.
DSA Python
{word: len(word) for word in words if len(word) [1] 3} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Adding '.upper()' to the key which changes the word.
Using '<' instead of '>' in the condition.
✗ Incorrect
The dictionary key is the word itself (no change), so blank 1 is empty. The condition checks if length is greater than 3, so blank 2 is '>'.
5fill in blank
hardFill all three blanks to create a dictionary comprehension that maps uppercase keys to their values only if the value is positive.
DSA Python
result = [1]: [2] for k, v in data.items() if v [3] 0}
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'k' instead of 'k.upper()' for keys.
Using '<' instead of '>' in the condition.
✗ Incorrect
Keys are converted to uppercase with 'k.upper()', values are 'v', and the condition checks if 'v' is greater than 0.