Linked List Game - codepath/compsci_guides GitHub Wiki

TIP102 Unit 6 Session 1 Advanced (Click for link to problem statements)

Problem Highlights

  • 💡 Difficulty: Medium
  • Time to complete: 15-25 mins
  • 🛠️ Topics: Linked Lists, Pair Comparison

1: U-nderstand

Understand what the interviewer is asking for by using test cases and questions about the problem.

  • Q: What does the problem ask for?
    • A: The problem asks to determine the winner of a game by comparing pairs of nodes in a linked list, where each pair consists of an even-indexed node and the next odd-indexed node.
  • Q: How should the points be calculated?
    • A: Compare the values in each pair:
      • If the even-indexed node is greater, the "Even" team gets a point.
      • If the odd-indexed node is greater, the "Odd" team gets a point.
      • If the points are tied, return "Tie".
HAPPY CASE
Input: game1 = Node(2, Node(1))
Output: "Even"
Explanation: The pair (2, 1) results in a point for the Even team.

HAPPY CASE
Input: game2 = Node(2, Node(5, Node(4, Node(7, Node(20, Node(5))))))
Output: "Odd"
Explanation: The three pairs are (2, 5), (4, 7), (20, 5). The Odd team wins with 2 points.

EDGE CASE
Input: game3 = Node(4, Node(5, Node(2, Node(1))))
Output: "Tie"
Explanation: The two pairs are (4, 5) and (2, 1). Each team gets 1 point.

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 involving Pair Comparison, we want to consider the following approaches:

  • Traversal: Traverse the list and compare the values of nodes in each pair.

3: P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: We will traverse the linked list, comparing the values of each pair of nodes. We will keep track of the points for each team and determine the winner based on the accumulated points.

1) Initialize two counters: `odd_points` and `even_points`.
2) Traverse the linked list in pairs:
    a) Compare the value of the `even`-indexed node with the value of the next `odd`-indexed node.
    b) Update the respective counter based on which `node` has a higher value.
3) After the loop ends, compare `odd_points` and `even_points`:
    a) If `odd_points > even_points`, return `"Odd"`.
    b) If `even_points > odd_points`, return `"Even"`.
    c) If points are tied, return `"Tie"`.

⚠️ Common Mistakes

  • Forgetting to handle cases where the list has fewer than two nodes.
  • Incorrectly updating the points or skipping nodes during traversal.

4: I-mplement

Implement the code to solve the algorithm.

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

# Function to determine the game result
def game_result(head):
    odd_points = 0
    even_points = 0
    current = head

    while current and current.next:
        even_value = current.value
        odd_value = current.next.value

        if even_value > odd_value:
            even_points += 1
        elif odd_value > even_value:
            odd_points += 1

        # Move to the next pair
        current = current.next.next

    if odd_points > even_points:
        return "Odd"
    elif even_points > odd_points:
        return "Even"
    else:
        return "Tie"

5: R-eview

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

  • Example: Use the provided game1, game2, and game3 linked lists to verify that the function correctly calculates the game result.

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 each node is visited exactly once.
  • Space Complexity: O(1) because the algorithm uses a constant amount of extra space for counters.