-
Notifications
You must be signed in to change notification settings - Fork 252
Pokemon Party
JCSU Unit 9 Problem Set 1 (Click for link to problem statements)
- 💡 Difficulty: Easy
- ⏰ Time to complete: 10-15 mins
- 🛠️ Topics: Linked Lists, Node Creation, Pointers
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 "Squirtle", "Pidgey", "Rattata", and "Gengar" linked sequentially.
- Are there constraints on input?
- The
Node
class is provided, and the problem assumes valid input.
- The
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 Creation and Assignment: Instantiate
Node
objects and use theirnext
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.
- Instantiate
node_1
with the value"Squirtle"
. - Instantiate
node_2
with the value"Pidgey"
. - Instantiate
node_3
with the value"Rattata"
. - Instantiate
node_4
with the value"Gengar"
. - Link the nodes sequentially by setting the
next
property of each node:-
node_1.next
points tonode_2
. -
node_2.next
points tonode_3
. -
node_3.next
points tonode_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:
- Input: node_1 = Node("Squirtle"), node_2 = Node("Pidgey"), node_3 = Node("Rattata"), node_4 = Node("Gengar") node_1.next = node_2, node_2.next = node_3, node_3.next = node_4
- Expected 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
- Observed 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
Example 2:
- Input: node_1 = Node("Squirtle")
- Expected Output: node_1.value = "Squirtle" node_1.next = None
- Observed Output: node_1.value = "Squirtle" node_1.next = None
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.