JCSU Unit 10 Problem Set 1 (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: memory_stream1 = 2 -> 4 -> 6 memory_stream2 = 1 -> 3 -> 5 Output: 1 -> 2 -> 3 -> 4 -> 5 -> 6 Explanation: The nodes from both memory streams are merged in ascending order.
EDGE CASE Input: memory_stream1 = None memory_stream2 = None Output: None Explanation: Both input lists are empty, so the merged list is also empty.
EDGE CASE Input: memory_stream1 = None memory_stream2 = 1 -> 3 -> 5 Output: 1 -> 3 -> 5 Explanation: One input list is empty, so the result is the non-empty list.
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 sorted linked lists, we want to consider the following approaches:
Plan the solution with appropriate visualizations and pseudocode.
General Idea:
Use two pointers to traverse both linked lists. At each step, attach the smaller node to the merged list and move the respective pointer forward. At the end, attach any remaining nodes from the non-exhausted list.
current
to build the merged list starting from the dummy node.memory_stream1
and memory_stream2
) using a while
loop:
current.next
and move the pointer in the respective list.current
forward in the merged list.dummy.next
, skipping the dummy node.Implement the code to solve the algorithm.
class Node:
def __init__(self, value, next=None):
self.value = value
self.next = next
# For testing
def print_linked_list(head):
current = head
while current:
print(current.value, end=" -> " if current.next else "\n")
current = current.next
def merge_memories(memory_stream1, memory_stream2):
# Create a dummy node to act as the head of the merged list
dummy = Node(0)
current = dummy # Pointer to build the merged list
# Traverse both input lists and merge them in sorted order
while memory_stream1 and memory_stream2:
if memory_stream1.value <= memory_stream2.value:
current.next = memory_stream1 # Attach the smaller node to the merged list
memory_stream1 = memory_stream1.next # Move to the next node in memory_stream1
else:
current.next = memory_stream2 # Attach the smaller node to the merged list
memory_stream2 = memory_stream2.next # Move to the next node in memory_stream2
current = current.next # Move the pointer in the merged list
# Attach any remaining nodes from either list
if memory_stream1:
current.next = memory_stream1
if memory_stream2:
current.next = memory_stream2
# Return the head of the merged list, skipping the dummy node
return dummy.next
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
Example 1:
Example 2:
Example 3:
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Assume n is the length of memory_stream1
and m is the length of memory_stream2
.