Why is it Always You Three - codepath/compsci_guides GitHub Wiki
TIP102 Unit 6 Session 1 Standard (Click for link to problem statements)
Problem Highlights
- 💡 Difficulty: Easy
- ⏰ Time to complete: 5-10 mins
- 🛠️ Topics: Linked Lists, Constructor Nesting
1: U-nderstand
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 does the problem ask for?
- The problem asks to create a linked list using a single assignment statement.
- What should the structure of the linked list look like?
- The linked list should contain three nodes: "Harry", "Ron", and "Hermione", linked in that order.
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.
2: M-atch
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:
- Nesting Constructors: Create the linked list in a single line by nesting the constructor calls for each node.
3: P-lan
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")))
4: I-mplement
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")))
5: R-eview
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
- Example: Call
print_linked_list(head)
to verify that the linked list outputs as expected: "Harry -> Ron -> Hermione".
6: E-valuate
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
- Time Complexity:
O(1)
as we are only creating a fixed number of nodes. - Space Complexity:
O(1)
as we are using a constant amount of space.