TIP102 Unit 5 Session 1 Advanced (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?
HAPPY CASE
Input: nodes = Node("Daisy") -> Node("Peach") -> Node("Luigi") -> Node("Mario")
Output: "Daisy -> Peach -> Luigi -> Mario"
Explanation: The nodes are correctly linked to form the desired sequence.
EDGE CASE
Input: nodes = Node("Daisy"), Node("Peach"), Node("Luigi"), Node("Mario") (but not linked)
Output: "Daisy"
Explanation: Since the nodes are not linked, only the first node "Daisy" will be printed.
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 connection problems, we want to consider the following approaches:
next
pointer.Plan the solution with appropriate visualizations and pseudocode.
General Idea: Connect the nodes in the order daisy -> peach -> luigi -> mario
by setting each node's next
pointer to the following node.
1) Set the `next` pointer of `daisy` to `peach`.
2) Set the `next` pointer of `peach` to `luigi`.
3) Set the `next` pointer of `luigi` to `mario`.
4) Print the linked list starting from `daisy` to verify the connections.
⚠️ Common Mistakes
Implement the code to solve the algorithm.
class Node:
def __init__(self, value, next=None):
self.value = value
self.next = next
# For testing
def print_linked_list(head):
current = head
while current:
print(current.value, end=" -> " if current.next else "\n")
current = current.next
daisy = Node("Daisy")
peach = Node("Peach")
luigi = Node("Luigi")
mario = Node("Mario")
# Link the nodes to form the linked list
daisy.next = peach
peach.next = luigi
luigi.next = mario
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
Example:
print_linked_list(daisy) # Expected Output: "Daisy -> Peach -> Luigi -> Mario"
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Time Complexity:
Space Complexity: