-
Notifications
You must be signed in to change notification settings - Fork 244
DNA Loop Length
JCSU Unit 10 Problem Set 2 (Click for link to problem statements)
- 💡 Difficulty: Medium
- ⏰ Time to complete: 25-30 mins
- 🛠️ Topics: Linked Lists, Cycle Detection, Floyd's Algorithm
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 is the goal of the problem?
- Determine the length of a cycle (loop) in a linked list, if it exists. Return 0 if there is no cycle.
- Are there constraints on input?
- Input linked list may or may not contain a cycle.
HAPPY CASE Input: A linked list: A -> T -> C -> G -> A (loop) Output: 4 Explanation: The loop begins at 'T' and cycles through 4 nodes: 'T -> C -> G -> A'.
EDGE CASE Input: A linked list: C -> G -> A -> T -> None (no loop) Output: 0 Explanation: No loop exists in the DNA sequence.
EDGE CASE Input: A single node pointing to itself: A -> A Output: 1 Explanation: The loop contains only one node.
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 cycle detection in linked lists, we want to consider the following approaches:
- Floyd's Cycle Detection Algorithm: Use two pointers (slow and fast) to detect and measure the loop.
- Hash Set (Alternative): Use a hash set to track visited nodes (less efficient for space).
Plan the solution with appropriate visualizations and pseudocode.
General Idea:
Use Floyd's Cycle Detection Algorithm to detect a loop. If a loop is detected, measure its length by traversing the loop until returning to the starting point.
- Initialize two pointers,
slow
andfast
, both pointing to the head of the list. - Move
slow
one step andfast
two steps in each iteration. - If
slow
meetsfast
, a loop is detected. If the end of the list (None
) is reached, return 0. - To measure the loop length:
- Start from the meeting point and traverse the loop until returning to the meeting point.
- Count the number of steps taken.
- Return the calculated loop length.
Implement the code to solve the algorithm.
def detect_dna_loop_length(head):
# Use Floyd's Cycle Detection algorithm
slow, fast = head, head
# Step 1: Detect if a loop exists
while fast and fast.next:
slow = slow.next # Move slow pointer one step
fast = fast.next.next # Move fast pointer two steps
if slow == fast: # Loop detected
break
else:
# If no loop is detected, return 0
return 0
# Step 2: Calculate loop length
loop_length = 1 # Initialize loop length counter
current = slow.next
while current != slow: # Traverse the loop until returning to the starting point
loop_length += 1
current = current.next
return loop_length # Return the calculated loop length
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
Example 1:
- Input: dna_sequence = A -> T -> C -> G -> A (loop)
- Expected Output: 4
- Observed Output: 4
Example 2:
- Input: dna_sequence = C -> G -> A -> T -> None
- Expected Output: 0
- Observed Output: 0
Example 3:
- Input: dna_sequence = A -> A (single-node loop)
- Expected Output: 1
- Observed Output: 1
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Assume n is the number of nodes in the linked list.
- Time Complexity: O(n) because we traverse the list to detect the loop and then measure the loop length.
- Space Complexity: O(1) because we only use two pointers and no additional data structures.