TIP102 Unit 7 Session 1 Standard (Click for link to problem statements)
A magician can double a spell's power if they merge two incantations together. Given the heads of two linked lists spell_a
and spell_b
where each node in the lists contains a spell segment, write a recursive function weave_spells()
that weaves spells in the pattern:
a1 -> b1 -> a2 -> b2 -> a3 -> b3 -> ...
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:
spell_a = Node('A', Node('C', Node('E')))
spell_b = Node('B', Node('D', Node('F')))
Output: A -> B -> C -> D -> E -> F
Explanation: The nodes from both linked lists are alternated to create the resulting list.
EDGE CASE
Input: spell_a = Node('A'), spell_b = None
Output: A
Explanation: If one list is empty, the other list should be returned as it is.
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 Merging Linked Lists, we want to consider the following approaches:
Plan the solution with appropriate visualizations and pseudocode.
General Idea:
Recursive Approach:
1) Base case: If either `spell_a` or `spell_b` is `None`, return the non-empty list.
2) Weave the current node from `spell_a` with the current node from `spell_b`.
3) Recurse with the next node of `spell_a` and the next node of `spell_b`.
4) Return the head of the new list, starting with the node from `spell_a`.
⚠️ Common Mistakes
next
pointers, leading to broken links in the resulting list.Implement the code to solve the algorithm.
class Node:
def __init__(self, value, next=None):
self.value = value
self.next = next
def weave_spells(spell_a, spell_b):
# Base cases
if not spell_a:
return spell_b
if not spell_b:
return spell_a
# Weave the current nodes
spell_a.next = weave_spells(spell_b, spell_a.next)
# Return the new head of the list
return spell_a
# For testing
def print_linked_list(head):
current = head
while current:
print(current.value, end=" -> " if current.next else "\n")
current = current.next
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
weave_spells
function with the inputs spell_a = Node('A', Node('C', Node('E')))
, spell_b = Node('B', Node('D', Node('F')))
. The function should return a linked list with nodes A -> B -> C -> D -> E -> F
.Evaluate the performance of your algorithm and state any strong/weak or future potential work.
O(N + M)
where N
and M
are the lengths of the two linked lists. The function processes each node exactly once.O(N + M)
due to the recursion stack. The depth of recursion is proportional to the total number of nodes in both lists.