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
node after it is removed?
dog
node is simply disconnected from the list; it is not deleted.HAPPY CASE
Input: cat -> mouse -> dog
Output: cat -> mouse -> cheese
Explanation: The `dog` node is removed, and a new node `cheese` with value "Gouda" is added to the end of the list.
EDGE CASE
Input: cat -> mouse
Output: cat -> mouse -> cheese
Explanation: The list already does not contain a `dog` node, so just the `cheese` node is added.
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:
dog
node.dog
node and add the cheese
node.Plan the solution with appropriate visualizations and pseudocode.
General Idea: Traverse the linked list to find and remove the dog
node, then traverse to the end of the list and add the cheese
node.
1) Start with the head of the list.
2) Traverse the list to find the `dog` node.
3) Disconnect the `dog` node by updating the previous node's next pointer.
4) Traverse to the end of the list.
5) Add the `cheese` node to the end of the list.
⚠️ Common Mistakes
dog
node.cheese
node.Implement the code to solve the algorithm.
class Node:
def __init__(self, value, next=None):
self.value = value
self.next = next
# Existing code
cat = Node("Tom")
mouse = Node("Jerry")
cat.next = mouse
dog = Node("Spike")
dog.next = cat
# New code
dog.next = None # notice that the node is not deleted, but is no longer connected to the rest of the list
cheese = Node("Gouda")
mouse.next = cheese
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 an input to check for the expected output:
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Assume N represents the number of nodes in the linked list.