JCSU Unit 9 Problem Set 1 (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: node_one = Node("a") node_two = Node("b") node_one.next = node_two Output: node_one.value = "a" node_one.next.value = "b" node_two.value = "b" node_two.next = None
EDGE CASE
Input:
Only one node created: Node("a")
Output:
node.next = None
Explanation:
Without a second node to link, next
remains as 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.
node_one
with the value "a"
.node_two
with the value "b"
.node_one.next
to reference node_two
.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 two nodes
node_one = Node("a") # Create the first node with value "a"
node_two = Node("b") # Create the second node with value "b"
# Step 2: Link the nodes
node_one.next = node_two # Set the 'next' of node_one to reference node_two
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.