Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to find the middle node of the linked list.
DSA Python
def get_middle(head): if head is None: return head slow = head fast = head.next while fast and fast.next: slow = slow.[1] fast = fast.next.next return slow
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' instead of 'next' causes errors because singly linked lists don't have 'prev'.
Using 'fast.next' instead of 'fast.next.next' for the fast pointer.
✗ Incorrect
The slow pointer moves one step at a time using the 'next' attribute to find the middle node.
2fill in blank
mediumComplete the code to merge two sorted linked lists.
DSA Python
def merge_sorted_lists(l1, l2): dummy = ListNode(0) tail = dummy while l1 and l2: if l1.val < l2.val: tail.next = l1 l1 = l1.[1] else: tail.next = l2 l2 = l2.next tail = tail.next tail.next = l1 or l2 return dummy.next
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 lists.
Using 'child' or 'random' which are not standard pointers.
✗ Incorrect
To move forward in the linked list l1, we use l1.next.
3fill in blank
hardFix the error in the merge_sort function to correctly split the list.
DSA Python
def merge_sort(head): if not head or not head.next: return head middle = get_middle(head) next_to_middle = middle.next middle.[1] = None left = merge_sort(head) right = merge_sort(next_to_middle) sorted_list = merge_sorted_lists(left, right) return sorted_list
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting middle.prev which does not exist in singly linked lists.
Not breaking the list, causing infinite recursion.
✗ Incorrect
To split the list, we set middle.next to None to break the list into two halves.
4fill in blank
hardFill both blanks to create a dictionary of word lengths for words longer than 3 characters.
DSA Python
words = ['apple', 'bat', 'carrot', 'dog', 'elephant'] lengths = {word: [1] for word in words if len(word) [2] 3}
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' causing wrong filtering.
Using 'word' instead of 'len(word)' for dictionary values.
✗ Incorrect
We use len(word) to get the length and '>' to filter words longer than 3.
5fill in blank
hardFill all three blanks to create a dictionary with uppercase keys and values greater than 0.
DSA Python
data = {'a': 1, 'b': 0, 'c': 3}
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 '>' for filtering values.
✗ Incorrect
Keys are converted to uppercase with k.upper(), values are v, and filter uses '>' to keep values greater than 0.