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.
HAPPY CASE
Input: top_hits_2010s = SongNode("Uptown Funk", SongNode("Party Rock Anthem", SongNode("Telephone")))
Output: Uptown Funk -> Party Rock Anthem -> 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 (Party Rock Anthem) and link it to the last node (Telephone).
3) Create the first node (Uptown Funk) and link it to the second node (Party Rock Anthem).
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.song = song
self.next = next
node3 = SongNode("Telephone")
node2 = SongNode("Party Rock Anthem", 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 Party Rock Anthem
and Party Rock Anthem.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.