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_1 = Node("Squirtle") node_2 = Node("Pidgey") node_3 = Node("Rattata") node_4 = Node("Gengar") Output: node_1.value = "Squirtle" node_1.next.value = "Pidgey" node_2.value = "Pidgey" node_2.next.value = "Rattata" node_3.value = "Rattata" node_3.next.value = "Gengar" node_4.value = "Gengar" node_4.next = None
EDGE CASE Input: Only one node created: Node("Squirtle") Output: node.value = "Squirtle" node.next = None Explanation: Without additional nodes, the linked list contains a single node with no next reference.
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 four nodes and set the next
property of each node to point to the subsequent node in the sequence.
node_1
with the value "Squirtle"
.node_2
with the value "Pidgey"
.node_3
with the value "Rattata"
.node_4
with the value "Gengar"
.next
property of each node:
node_1.next
points to node_2
.node_2.next
points to node_3
.node_3.next
points to node_4
.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)
# Create individual nodes
node_1 = Node("Squirtle") # First node
node_2 = Node("Pidgey") # Second node
node_3 = Node("Rattata") # Third node
node_4 = Node("Gengar") # Fourth node
# Link the nodes to form a linked list
node_1.next = node_2 # Squirtle -> Pidgey
node_2.next = node_3 # Pidgey -> Rattata
node_3.next = node_4 # Rattata -> Gengar
# Gengar -> None (default)
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.