TIP102 Unit 6 Session 2 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?
m
nodes, then remove the next n
nodes, repeating this pattern until the end of the list.HAPPY CASE
Input: trailhead = Node(1, Node(2, Node(3, Node(4, Node(5, Node(6, Node(7, Node(8, Node(9, Node(10))))))))))
m = 2
n = 3
Output: 1 -> 2 -> 6 -> 7 -> 11 -> 12
Explanation: The function keeps the first 2 nodes, deletes the next 3 nodes, and repeats the process until the end.
EDGE CASE
Input: trailhead = Node(1, Node(2, Node(3)))
m = 1
n = 2
Output: 1
Explanation: The function keeps the first node, deletes the next 2 nodes, and stops as it reaches the end of the list.
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 involving Selective Deletion, we want to consider the following approaches:
Plan the solution with appropriate visualizations and pseudocode.
General Idea: We will traverse the linked list, retaining the first m
nodes and then deleting the next n
nodes, repeating this process until the end of the list.
1) Initialize a pointer `current` to the head of the list.
2) While `current` is not None:
a) Traverse the first `m` nodes, moving `current` forward.
b) Check if `current` is None after traversing `m` nodes. If yes, return the head.
c) Traverse the next `n` nodes and remove them by updating the `next` pointer of the `m-th` node.
d) Move `current` to the node after the deleted nodes.
3) Return the head of the list.
⚠️ Common Mistakes
m + n
nodes.Implement the code to solve the algorithm.
class Node:
def __init__(self, value, next=None):
self.value = value
self.next = next
# Function to selectively clear the trail
def selective_trail_clearing(trailhead, m, n):
current = trailhead
while current:
# Traverse m nodes
for i in range(1, m):
if current is None:
return trailhead
current = current.next
if current is None:
return trailhead
# Now current is at the m-th node
# We will delete the next n nodes
temp = current.next
for j in range(n):
if temp is None:
break
temp = temp.next
# Connect the m-th node to the node after the n deleted nodes
current.next = temp
# Move current to the next kept node
current = temp
return trailhead
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
trailhead
linked list with the values of m
and n
to verify that the function correctly clears the trail by removing the appropriate nodes.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.
O(N)
because each node is visited exactly once.O(1)
because the algorithm uses a constant amount of extra space for pointers.