TIP102 Unit 6 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.
k
consecutive segments where the segments are as equal in length as possible, with earlier segments being greater than or equal in size compared to later segments.HAPPY CASE
Input: protein1 = Node('Ala', Node('Gly', Node('Leu', Node('Val', Node('Pro', Node('Ser', Node('Thr', Node('Cys')))))))), k = 3
Output:
Ala -> Gly -> Leu
Val -> Pro -> Ser
Thr -> Cys
Explanation: The linked list has been split into three segments with sizes 3, 3, and 2.
EDGE CASE
Input: protein2 = Node('Ala', Node('Gly', Node('Leu', Node('Val')))), k = 5
Output:
Ala
Gly
Leu
Val
Empty List
Explanation: Since `k` is greater than the length of the list, some segments will be empty.
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 Segmentation, we want to consider the following approaches:
Plan the solution with appropriate visualizations and pseudocode.
General Idea: We will traverse the linked list and split it into k
segments. The sizes of the segments are calculated such that the first few segments are larger if the list cannot be evenly divided.
1) Calculate the total length of the linked list.
2) Determine the base size of each segment and the number of larger segments.
3) Traverse the list, creating each segment by linking the nodes accordingly.
4) Append each segment to the output list.
5) If there are fewer nodes than `k`, the remaining segments will be empty.
6) Return the list of `k` segments.
⚠️ Common Mistakes
k
is greater than the length of the list, resulting in empty segments.Implement the code to solve the algorithm.
class Node:
def __init__(self, value, next=None):
self.value = value
self.next = next
# Function to split the protein chain into k segments
def split_protein_chain(protein, k):
# Step 1: Calculate the total length of the list
total_length = 0
current = protein
while current:
total_length += 1
current = current.next
# Step 2: Determine the size of each segment
base_size = total_length // k
larger_segments = total_length % k # Segments that will be one node larger
parts = []
current = protein
for i in range(k):
head = current
segment_size = base_size + (1 if i < larger_segments else 0)
for j in range(segment_size - 1):
if current:
current = current.next
if current:
next_segment = current.next
current.next = None
current = next_segment
parts.append(head)
return parts
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
protein1
and protein2
linked lists to verify that the function correctly segments the list into the desired number of parts.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 and k
represents the number of segments.
O(N)
because each node is visited exactly once.O(k)
because we store k
segments in the output list.