Linked Up - codepath/compsci_guides GitHub Wiki

TIP102 Unit 5 Session 1 Advanced (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: 5 mins
  • 🛠️ Topics: Linked List, Pointers

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 is a linked list?
    • A data structure where each element (node) contains a value and a reference to the next node in the sequence.
  • How do we create a linked list?
    • By linking nodes together using their next pointers.
HAPPY CASE
Input: Nodes with values "K.K. Slider", "Harriet", "Saharah", "Isabelle"
Output: "K.K. Slider -> Harriet -> Saharah -> Isabelle"
Explanation: The nodes are linked in the correct order.

EDGE CASE
Input: A single node with value "Isabelle"
Output: "Isabelle"
Explanation: The list contains only one node.

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

  • Creating nodes
  • Linking nodes together using pointers

3: P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: Create nodes and link them using their next pointers to form the desired linked list.

1) Create the node `kk_slider` with the value "K.K. Slider".
2) Create the node `harriet` with the value "Harriet".
3) Create the node `saharah` with the value "Saharah".
4) Create the node `isabelle` with the value "Isabelle".
5) Set `kk_slider.next` to `harriet`.
6) Set `harriet.next` to `saharah`.
7) Set `saharah.next` to `isabelle`.

⚠️ Common Mistakes

  • Forgetting to set the next pointer for a node, resulting in a broken list.
  • Incorrectly ordering the nodes.

4: I-mplement

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

kk_slider = Node("K.K. Slider")
harriet = Node("Harriet")
saharah = Node("Saharah")
isabelle = Node("Isabelle")

# Link the nodes to form the linked list
kk_slider.next = harriet
harriet.next = saharah
saharah.next = isabelle

# Example Usage:
print_linked_list(kk_slider)  # Output: "K.K. Slider -> Harriet -> Saharah -> Isabelle"

5: R-eview

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

  • Verify the linked list is correctly formed: kk_slider -> harriet -> saharah -> isabelle
  • Print the values to ensure the correct order.

6: E-valuate

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.

  • Time Complexity: O(N) because we need to traverse all the nodes in the linked list.
  • Space Complexity: O(1) because we are using a fixed amount of additional space.