TIP102 Unit 5 Session 1 (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?
tom_nook
node after it is removed?
next
pointer is set to None
.saharah
be added?
tommy
Initial list: tom_nook -> timmy -> tommy
After disconnecting tom_nook: timmy -> tommy
After adding saharah: timmy -> tommy -> saharah
HAPPY CASE
Input: A linked list with nodes `tom_nook -> timmy -> tommy`
Output: A linked list with nodes `timmy -> tommy -> saharah`
Explanation: The `tom_nook` node is removed and the `saharah` node is added to the end.
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 disconnect the tom_nook
node from the list and then add a new node saharah
to the end of the list.
1) Create the initial linked list with nodes `tom_nook -> timmy -> tommy`.
2) Disconnect the `tom_nook` node by setting `timmy.next` to `tommy`.
3) Set `tom_nook.next` to `None`.
4) Create a new node `saharah`.
5) Add `saharah` to the end of the list by setting `tommy.next` to `saharah`.
⚠️ Common Mistakes
tom_nook
node.Implement the code to solve the algorithm.
class Node:
def __init__(self, value, next=None):
self.value = value
self.next = next
# From previous problem
tom_nook = Node("Tom Nook")
tommy = Node("Tommy")
timmy = Node("Timmy")
tom_nook.next = timmy
timmy.next = Tommy
# Disconnect tom_nook from the rest of the list
tom_nook.next = None
# Create Saharah
saharah = Node("Saharah")
tommy.next = saharah
5: R-eview
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
Initial list: tom_nook -> timmy -> tommy
After disconnecting tom_nook: timmy -> tommy
After adding saharah: timmy -> tommy -> saharah
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 the list to find the nodes to modify.
Space Complexity: O(1) because we are only using a fixed amount of additional space.