Codepath

Wild Goose Chase

TIP102 Unit 6 Session 2 Standard (Click for link to problem statements)

Problem Highlights

  • 💡 Difficulty: Medium
  • Time to complete: 15-25 mins
  • 🛠️ Topics: Linked Lists, Cycle Detection

1: U-nderstand

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 does the problem ask for?
    • The problem asks to determine whether the tail of a linked list points back to the head, making the linked list circular.
  • What should be returned?
    • The function should return True if the linked list is circular, otherwise False.
HAPPY CASE
Input: clue1 = Node("The stolen goods are at an abandoned warehouse")
       clue2 = Node("The mayor is accepting bribes")
       clue3 = Node("They dumped their disguise in the lake")
       clue1.next = clue2
       clue2.next = clue3
       clue3.next = clue1  # Circular link
Output: True
Explanation: The linked list is circular because the last node points back to the head.

EDGE CASE
Input: clue1 = Node("The stolen goods are at an abandoned warehouse")
       clue2 = Node("The mayor is accepting bribes")
       clue3 = Node("They dumped their disguise in the lake")
       clue1.next = clue2
       clue2.next = clue3
       clue3.next = None  # Not circular
Output: False
Explanation: The linked list is not circular because the last node does not point back to the head.

2: M-atch

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 Cycle Detection (Circularity Check), we want to consider the following approaches:

  • Traversal: Traverse the list to check if the tail node's next pointer points back to the head.

3: P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: We will traverse the linked list and check if any node's next pointer points back to the head of the list. If we reach the end of the list without finding such a pointer, the list is not circular.

1) If the linked list is empty, return False.
2) Initialize a pointer `current` to the head of the list.
3) Traverse the list:
    a) If `current.next` is the head of the list, return True (the list is circular).
    b) Move the `current` pointer to the next node.
4) If the end of the list is reached without finding a circular link, return False.

⚠️ Common Mistakes

  • Forgetting to handle the case where the list is empty.
  • Incorrectly managing pointers, leading to an infinite loop or incorrect results.

4: I-mplement

Implement the code to solve the algorithm.

class Node:
    def __init__(self, value, next=None):
        self.value = value
        self.next = next

def is_circular(clues):
    if not clues:
        return False

    current = clues
    while current.next:
        if current.next == clues:
            return True
        current = current.next
    
    return False

5: R-eview

Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.

  • Example: Use the provided clue1, clue2, and clue3 linked list to verify that the function correctly identifies whether the list is circular.

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 each node is visited exactly once.
  • Space Complexity: O(1) because the algorithm uses a constant amount of extra space for pointers.
Fork me on GitHub