Unit 5 Session 2 (Click for link to problem statements)
TIP102 Unit 5 Session 2 Standard (Click for link to problem statements)
Understand what the interviewer is asking for by using test cases and questions about the problem.
- Established a set (2-3) of test cases to verify their own solution later.
- Established a set (1-2) of edge cases to verify their solution handles complexities.
- Have fully understood the problem and have no clarifying questions.
- Have you verified any Time/Space Constraints for this problem?
prev
and next
attributes of the single node should both be None
.HAPPY CASE
Input: head = Node("Isabelle"), tail = Node("K.K. Slider")
Output: head.next = tail, tail.prev = head
Explanation: The `next` pointer of the head points to the tail, and the `prev` pointer of the tail points to the head.
EDGE CASE
Input: head = Node("Isabelle")
Output: head.next = None, head.prev = None
Explanation: If there is only one node, both `next` and `prev` pointers should be `None`.
Match what this problem looks like to known categories of problems, e.g. Linked List or Dynamic Programming, and strategies or patterns in those categories.
For Linked List problems, we want to consider the following approaches:
next
and prev
attributesPlan the solution with appropriate visualizations and pseudocode.
General Idea: Modify the Node
class to include a prev
attribute and link the nodes to form a doubly linked list.
1) Update the `Node` class to include a `prev` parameter in the constructor.
2) Link the `next` pointer of the head node to the tail node.
3) Link the `prev` pointer of the tail node to the head node.
⚠️ Common Mistakes
prev
attribute, leading to errors when trying to set the prev
pointer.Implement the code to solve the algorithm.
class Node:
def __init__(self, value, next=None, prev=None):
self.value = value
self.next = next
self.prev = prev
# Create the nodes
head = Node("Isabelle")
tail = Node("K.K. Slider")
# Link the nodes to form a doubly linked list
head.next = tail
tail.prev = head
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
next
and prev
pointers for both head and tail nodes to ensure the doubly linked list is correctly formed.Example:
print(head.value, "<->", head.next.value)
print(tail.prev.value, "<->", tail.value)
Expected Output:
Isabelle <-> K.K. Slider
Isabelle <-> K.K. Slider
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
- Time Complexity: O(1) because we are only setting pointers.
- Space Complexity: O(1) because we are only storing references to nodes.