JCSU Unit 9 Problem Set 2 (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?
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
.
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
objects and use their next
property to link them.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.
yoshi
with the value "Super Blooper"
.bowser
with the value "Pirahna Prowler"
and set its next
property to reference yoshi
.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'
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
Example 1:
Example 2:
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Assume n is the number of nodes created.