0
0
DSA Pythonprogramming~10 mins

Why Doubly Linked List Over Singly Linked List in DSA Python - Test Your Knowledge

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Adata
Bprev
Chead
Dnext
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.
2fill in blank
medium

Complete 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'
Anext
Bprev
Chead
Ddata
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.
3fill in blank
hard

Fix 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'
Ahead
Bnext
Cprev
Ddata
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.
4fill in blank
hard

Complete 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'
B>
C<
D.upper()
Attempts:
3 left
💡 Hint
Common Mistakes
Adding '.upper()' to the key which changes the word.
Using '<' instead of '>' in the condition.
5fill in blank
hard

Fill 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'
Ak.upper()
Bv
C>
Dk
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'k' instead of 'k.upper()' for keys.
Using '<' instead of '>' in the condition.