Codepath

Print Backwards

Unit 5 Session 2 (Click for link to problem statements)

TIP102 Unit 5 Session 2 Standard (Click for link to problem statements)

Problem Highlights

  • 💡 Difficulty: Easy
  • Time to complete: 10-15 mins
  • 🛠️ Topics: Doubly Linked Lists, Traversal

1: U-nderstand

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 happens if the linked list is empty?
    • If the linked list is empty, the function should not print anything.
HAPPY CASE
Input: tail = Node("Saharah", prev=Node("K.K. Slider", prev=Node("Isabelle")))
Output: "Saharah K.K. Slider Isabelle"
Explanation: The function prints the values of the linked list from the tail to the head.

EDGE CASE
Input: tail = None
Output: (No output)
Explanation: When the linked list is empty, the function does not print anything.

2: M-atch

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 Doubly Linked List problems, we want to consider the following approaches:

  • Traversal in reverse direction using prev pointers
  • Iterative approach to visit each node until the head is reached

3: P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: Traverse the doubly linked list from the tail to the head and print each node's value.

1) Start at the tail of the doubly linked list.
2) While the current node is not `None`, do the following:
    a) Print the value of the current node.
    b) Move to the previous node using the `prev` pointer.
3) Ensure that the values are separated by a space.

⚠️ Common Mistakes

  • Forgetting to handle the case where the list is empty.
  • Not managing spaces between values correctly, leading to formatting issues.

4: I-mplement

Implement the code to solve the algorithm.

class Node:
    def __init__(self, value, next=None, prev=None):
        self.value = value
        self.next = next
        self.prev = prev

def print_reverse(tail):
    current = tail
    while current:
        if current.prev:
            print(current.value, end=" ")
        else:
            print(current.value)
        current = current.prev

5: R-eview

Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.

  • Check the output with a doubly linked list to ensure the values are printed in reverse order.

Example:

isabelle = Node("Isabelle")
kk_slider = Node("K.K. Slider")
saharah = Node("Saharah")
isabelle.next = kk_slider
kk_slider.next = saharah
saharah.prev = kk_slider
kk_slider.prev = isabelle

# Linked List: Isabelle <-> K.K. Slider <-> Saharah
print_reverse(saharah)
# Expected Output: "Saharah K.K. Slider Isabelle"

6: E-valuate

Evaluate the performance of your algorithm and state any strong/weak or future potential work.

  • Time Complexity: O(N) because we need to traverse all nodes in the doubly linked list.
  • Space Complexity: O(1) because we are only using a constant amount of extra space for traversal.
Fork me on GitHub