Codepath

Bowser and Yoshi

JCSU Unit 9 Problem Set 2 (Click for link to problem statements)

Problem Highlights

  • 💡 Difficulty: Easy
  • Time to complete: 10-15 mins
  • 🛠️ Topics: Linked Lists, Node Creation, Pointers

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 goal of the problem?
    • Create a linked list with nodes containing the values "Pirahna Prowler" and "Super Blooper" linked sequentially.
  • Are there constraints on input?
    • The Node class is provided, and the problem assumes valid input.

HAPPY CASE Input: yoshi = Node("Super Blooper") bowser = Node("Pirahna Prowler", yoshi) Output: bowser.value = "Pirahna Prowler" bowser.next = yoshi yoshi.value = "Super Blooper" yoshi.next = None

EDGE CASE Input: Single node created: yoshi = Node("Super Blooper") Output: yoshi.value = "Super Blooper" yoshi.next = None Explanation: Without an additional node to link, the next remains 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 node creation and linking problems, we want to consider the following approaches:

  • Node Creation and Assignment: Instantiate Node objects and use their next property to link them.

3: P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea:
Use the Node class to create two nodes and set the next property of the first node to point to the second node.

Steps:

  1. Instantiate yoshi with the value "Super Blooper".
  2. Instantiate bowser with the value "Pirahna Prowler" and set its next property to reference yoshi.

4: I-mplement

Implement the code to solve the algorithm.

class Node:
    def __init__(self, value, next=None):
        self.value = value  # Store the value of the node
        self.next = next    # Reference to the next node (defaults to None)

# Step 1: Create the nodes
yoshi = Node("Super Blooper")  # Create the 'yoshi' node with value "Super Blooper"
bowser = Node("Pirahna Prowler", yoshi)  # Create the 'bowser' node with value "Pirahna Prowler" and point it to 'yoshi'

5: R-eview

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

Example 1:

  • Input: yoshi = Node("Super Blooper"), bowser = Node("Pirahna Prowler", yoshi)
  • Expected Output: bowser.value = "Pirahna Prowler" bowser.next = yoshi yoshi.value = "Super Blooper" yoshi.next = None
  • Observed Output: bowser.value = "Pirahna Prowler" bowser.next = yoshi yoshi.value = "Super Blooper" yoshi.next = None

Example 2:

  • Input: yoshi = Node("Super Blooper")
  • Expected Output: yoshi.value = "Super Blooper" yoshi.next = None
  • Observed Output: yoshi.value = "Super Blooper" yoshi.next = None

6: E-valuate

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

Assume n is the number of nodes created.

  • Time Complexity: O(1) for creating and linking each node, as operations are constant-time.
  • Space Complexity: O(1) additional space beyond the storage required for the nodes themselves.
Fork me on GitHub