-
Notifications
You must be signed in to change notification settings - Fork 247
Race String
JCSU Unit 9 Problem Set 2 (Click for link to problem statements)
- 💡 Difficulty: Easy
- ⏰ Time to complete: 10-15 mins
- 🛠️ Topics: Linked Lists, Iteration, String Manipulation
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 is the goal of the problem?
- Traverse a linked list and return a string of its values separated by
"followed by"
.
- Traverse a linked list and return a string of its values separated by
- Are there constraints on input?
- The input is a valid linked list with at least one node.
HAPPY CASE
Input:
A linked list: Mario -> Luigi -> Wario -> Toad
Output:
"Mario followed by Luigi followed by Wario followed by Toad"
Explanation:
Traverse the list and combine the node values with the separator "followed by"
.
EDGE CASE Input: A linked list with one node: Mario Output: "Mario" Explanation: A single-node list results in the node value as the string without any separator.
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 traversal problems, we want to consider the following approaches:
-
Iteration: Traverse the linked list using a
while
loop and accumulate node values. - Recursive Traversal (Alternative): Use recursion to traverse and collect node values (not optimal for long lists due to stack depth limitations).
Plan the solution with appropriate visualizations and pseudocode.
General Idea:
Iterate through the linked list and collect the values of the nodes into a list. Convert the list of values into a single string using the separator "followed by"
.
- Initialize an empty list
result
to store node values. - Set
current
to the head of the linked list. - Use a
while
loop to traverse the linked list:- Append the value of
current
toresult
. - Move
current
to the next node.
- Append the value of
- After traversal, join the elements of
result
with the separator"followed by"
. - Return the resulting string.
Implement the code to solve the algorithm.
def race_report(head):
result = [] # Initialize an empty list to store node values
current = head # Start with the head of the linked list
while current: # Traverse the linked list until the end (when current is None)
result.append(current.value) # Append the value of the current node to the result
current = current.next # Move to the next node
# Join the values with the separator "followed by"
return " followed by ".join(result)
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
Example 1:
- Input: Mario -> Luigi -> Wario -> Toad
- Expected Output: "Mario followed by Luigi followed by Wario followed by Toad"
- Observed Output: "Mario followed by Luigi followed by Wario followed by Toad"
Example 2:
- Input: Mario
- Expected Output: "Mario"
- Observed Output: "Mario"
Example 3:
- Input: (Empty linked list, head=None)
- Expected Output: ""
- Observed Output: ""
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Assume n is the number of nodes in the linked list.
- Time Complexity: O(n) because we traverse the list once to collect the values.
- Space Complexity: O(n) because we use a list to store node values before creating the final string.