TIP102 Unit 6 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?
HAPPY CASE
Input: None (assignment statement)
Output: Harry -> Ron -> Hermione
Explanation: The linked list is correctly formed using a single nested constructor statement.
EDGE CASE
Input: None
Output: Harry -> Ron -> Hermione
Explanation: As this problem doesn't involve dynamic inputs, edge cases might involve incorrect or incomplete list formation.
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 involving Constructor Nesting, we want to consider the following approaches:
Plan the solution with appropriate visualizations and pseudocode.
General Idea: We will use the Node
constructor to create the linked list. By nesting the constructors, the linked list will be created in a single assignment statement.
1) Create the last node first: Node("Hermione")
2) Use this node as the `next` for the second node: Node("Ron", Node("Hermione"))
3) Finally, use the second node as the `next` for the first node: Node("Harry", Node("Ron", Node("Hermione")))
Implement the code to solve the algorithm.
class Node:
def __init__(self, value, next=None):
self.value = value
self.next = next
# Single assignment statement to create the linked list
head = Node("Harry", Node("Ron", Node("Hermione")))
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
print_linked_list(head)
to verify that the linked list outputs as expected: "Harry -> Ron -> Hermione".Evaluate the performance of your algorithm and state any strong/weak or future potential work.
O(1)
as we are only creating a fixed number of nodes.O(1)
as we are using a constant amount of space.