Unit 5 Session 1 (Click for link to problem statements)
TIP102 Unit 5 Session 1 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?
dog -> cat -> mouse
.HAPPY CASE
Input: Node("Spike") -> Node("Tom") -> Node("Jerry")
Output:
Spike
<__main__.Node object at 0x...>
Tom
<__main__.Node object at 0x...>
Jerry
None
Explanation: The node "Spike" points to "Tom", who points to "Jerry", and "Jerry" is the last node, so its `next` is `None`.
EDGE CASE
Input: Node("Spike") -> None
Output:
Spike
None
Explanation: If "Spike" has no next node, its `next` 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:
Node
class to represent each element in the list.next
attribute to point to the next node in the sequence.next
pointer of existing nodes to change the structure of the list.Plan the solution with appropriate visualizations and pseudocode.
General Idea: Create a new node "Spike", link it to the head of the existing list ("Tom"), and then adjust pointers to form the final list.
1) Start with the existing linked list: Node("Tom") -> Node("Jerry").
2) Create a new node `dog` with the value "Spike".
3) Point `dog.next` to `cat`, making "Spike" the head of the list.
4) Test the linked list by printing the values and references.
⚠️ Common Mistakes
Implement the code to solve the algorithm.
# Existing code
cat = Node("Tom")
mouse = Node("Jerry")
cat.next = mouse
# New code
dog = Node("Spike")
dog.next = cat
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
Trace through your code with the following input:
Evaluate the performance of your algorithm and state any strong/weak or future potential work.