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?
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.
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:
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
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
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
clue1
, clue2
, and clue3
linked list to verify that the function correctly identifies whether the list is circular.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.