TIP102 Unit 5 Session 1 Standard (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?
What is a linked list?
How do we create a linked list?
next
attribute.HAPPY CASE
Input:
tom_nook = Node("Tom Nook")
tommy = Node("Tommy")
tom_nook.next = tommy
print(tom_nook.value) # Output: Tom Nook
print(tom_nook.next.value) # Output: Tommy
print(tommy.value) # Output: Tommy
print(tommy.next) # Output: None
Explanation:
The linked list is created correctly, with `tom_nook` pointing to `tommy`.
EDGE CASE
Input:
node1 = Node("Node 1")
print(node1.value) # Output: Node 1
print(node1.next) # Output: None
Explanation:
A single node without a next node should have its `next` attribute set to `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 Linked List problems, we want to consider the following approaches:
next
attribute.None
.Plan the solution with appropriate visualizations and pseudocode.
General Idea: Create the nodes and link them to form the linked list.
1) Define the `Node` class with `__init__` method to initialize `value` and `next` attributes.
2) Create an instance of `Node` for `tom_nook` with value "Tom Nook".
3) Create an instance of `Node` for `tommy` with value "Tommy".
4) Link `tom_nook` to `tommy` using the `next` attribute.
⚠️ Common Mistakes
next
attribute to link the nodes.next
attribute.Implement the code to solve the algorithm.
class Node:
def __init__(self, value, next=None):
self.value = value
self.next = next
# Create the nodes
tom_nook = Node("Tom Nook")
tommy = Node("Tommy")
# Link the nodes
tom_nook.next = tommy
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
tom_nook
and tommy
.value
and next
attributes.Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Assume N
represents the number of nodes in the linked list.
O(1)
because creating and linking nodes are constant-time operations.O(1)
for each node instance created.