Codepath

Chase List

Unit 5 Session 1 (Click for link to problem statements)

TIP102 Unit 5 Session 1 Standard (Click for link to problem statements)

Problem Highlights

  • 💡 Difficulty: Easy-Medium
  • Time to complete: 5-10 mins
  • 🛠️ Topics: Linked Lists, Pointers, Node Class

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 is the task?
    • Create a new node "Spike" and make it the head of the linked list by pointing it to "Tom", forming the list dog -> cat -> mouse.
HAPPY CASE
Input: Node("Spike") -> Node("Tom") -> Node("Jerry")
Output:
Spike
<__main__.Node object at 0x...>
Tom
<__main__.Node object at 0x...>
Jerry
None
Explanation: The node "Spike" points to "Tom", who points to "Jerry", and "Jerry" is the last node, so its `next` is `None`.

EDGE CASE
Input: Node("Spike") -> None
Output:
Spike
None
Explanation: If "Spike" has no next node, its `next` should be `None`.

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, we want to consider the following approaches:

  • Use a Node class to represent each element in the list.
  • Link nodes by setting the next attribute to point to the next node in the sequence.
  • Reassign the next pointer of existing nodes to change the structure of the list.

3: P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: Create a new node "Spike", link it to the head of the existing list ("Tom"), and then adjust pointers to form the final list.

1) Start with the existing linked list: Node("Tom") -> Node("Jerry").
2) Create a new node `dog` with the value "Spike".
3) Point `dog.next` to `cat`, making "Spike" the head of the list.
4) Test the linked list by printing the values and references.

⚠️ Common Mistakes

  • Forgetting to correctly link the new head node to the existing head node.
  • Confusing the direction of the pointers when creating the new head.

4: I-mplement

Implement the code to solve the algorithm.

# Existing code
cat = Node("Tom")
mouse = Node("Jerry")
cat.next = mouse

# New code
dog = Node("Spike")
dog.next = cat

5: R-eview

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

Trace through your code with the following input:

  • dog = Node("Spike")
  • dog.next = cat
  • Verify that the list structure is correctly formed as "Spike -> Tom -> Jerry".

6: E-valuate

Evaluate the performance of your algorithm and state any strong/weak or future potential work.

  • Time Complexity: O(1) for creating and linking nodes since each operation is constant time.
  • Space Complexity: O(1) as we are only creating and storing a few nodes.
Fork me on GitHub