Update Chase - codepath/compsci_guides GitHub Wiki
Unit 5 Session 1 (Click for link to problem statements)
TIP102 Unit 5 Session 1 Standard (Click for link to problem statements)
Problem Highlights
- 💡 Difficulty: Medium
- ⏰ Time to complete: 20 mins
- 🛠️ Topics: Linked Lists
1: U-nderstand
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?
- What happens to the
dog
node after it is removed?- The
dog
node is simply disconnected from the list; it is not deleted.
- The
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.
2: M-atch
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:
- Traversal: We need to traverse the list to find and remove the
dog
node. - Modification: We need to modify the pointers to remove the
dog
node and add thecheese
node.
3: P-lan
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
- Forgetting to properly update the next pointers when removing the
dog
node. - Not traversing to the very end of the list before adding the
cheese
node.
4: I-mplement
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
5: R-eview
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:
- Start with the dog node pointing to cat.
- Disconnect the dog node.
- Connect the cheese node to the end of the list after mouse.
6: E-valuate
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.
- Time Complexity: O(N) because we need to traverse all the nodes in the linked list.
- Space Complexity: O(1) because we only need a constant amount of additional memory