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: top_hits_2010s = SongNode("Uptown Funk", SongNode("Dynamite", SongNode("Telephone")))
Output: Uptown Funk -> Dynamite -> Telephone
Explanation: The output is the linked list where each node points to the next in sequence.
EDGE CASE
Input: top_hits_2010s = None
Output: (no output)
Explanation: An empty list (no nodes) should simply print nothing.
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:
Plan the solution with appropriate visualizations and pseudocode.
General Idea: We will break down the construction of the linked list into multiple lines where each node is created separately and linked to the next node.
1) Create the last node (Telephone) without linking it to any other node.
2) Create the second node (Dynamite) and link it to the last node (Telephone).
3) Create the first node (Uptown Funk) and link it to the second node (Dynamite).
4) The first node (Uptown Funk) will now serve as the head of the linked list.
⚠️ Common Mistakes
Implement the code to solve the algorithm.
class Node:
def __init__(self, value, next=None):
self.value = value
self.next = next
node3 = SongNode("Telephone")
node2 = SongNode("Dynamite", node3)
top_hits_2010s = SongNode("Uptown Funk", node2)
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
top_hits_2010s
and trace through to ensure each SongNode
is linked correctly.top_hits_2010s.next
points to Dynamite
and Dynamite.next
points to Telephone
.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(N)
because creating and linking the nodes requires a linear traversal through the list.O(1)
because we are not using any extra space beyond the list itself.