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.
- Ha5: R-eview Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
Initial list: "Carp" -> "Dace" -> "Cherry Salmon" After catching "Carp": "Dace" -> "Cherry Salmon" When the list is empty: "Aw! Better luck next time!" and returns None 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(1) because the operation is done in constant time. Space Complexity: O(1) because we are using a fixed amount of additional space. ve you verified any Time/Space Constraints for this problem?
None
.HAPPY CASE
Input: A linked list with nodes "Carp" -> "Dace" -> "Cherry Salmon"
Output:
"I caught a Carp!"
New list: "Dace" -> "Cherry Salmon"
Explanation: The first fish "Carp" is caught and removed from the list.
EDGE CASE
Input: An empty list
Output:
"Aw! Better luck next time!"
New list: None
Explanation: There are no fish to catch, so the function returns 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:
Plan the solution with appropriate visualizations and pseudocode.
General Idea: We need to handle two cases: when the list is empty and when the list is not empty. If the list is empty, print the appropriate message and return None
. If the list is not empty, print the name of the fish in the head node and update the head to the next node.
1) Check if the head is `None`.
a) If it is, print "Aw! Better luck next time!" and return `None`.
2) If the head is not `None`:
a) Print "I caught a <fish name>!" where <fish name> is the value of the head node.
b) Return the next node as the new head of the list.
⚠️ Common Mistakes
Implement the code to solve the algorithm.
class Node:
def __init__(self, fish_name, next=None):
self.fish_name = fish_name
self.next = next
# For testing
def print_linked_list(head):
current = head
while current:
print(current.fish_name, end=" -> " if current.next else "\n")
current = current.next
def catch_fish(head):
if head is None:
print("Aw! Better luck next time!")
return None
else:
print(f"I caught a {head.fish_name}!")
return head.next
# Example Usage:
fish_list = Node("Carp", Node("Dace", Node("Cherry Salmon")))
print_linked_list(fish_list) # Output: "Carp -> Dace -> Cherry Salmon"
new_head = catch_fish(fish_list) # Output: "I caught a Carp!"
print_linked_list(new_head) # Output: "Dace -> Cherry Salmon"
empty_list = None
new_head = catch_fish(empty_list) # Output: "Aw! Better luck next time!"
print(new_head) # Output: None
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
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.