Codepath

Collecting Points at Festival Booths

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

U-nderstand

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

  • Q: What is the input to the problem?
    • A: The input is a list points where each element represents the number of points available at a festival booth.
  • Q: What is the output?
    • A: The output is the total number of points collected after visiting all booths.
  • Q: How should the points be collected?
    • A: Points should be collected by simulating a process where points are added to a stack and then immediately popped from the stack, summing the total points collected.

P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: Use a stack to simulate the process of collecting points from each booth. Each point value is pushed onto the stack and then immediately popped, adding to the total points collected.

1. Initialize an empty stack and a variable `total_points` to track the sum of points collected.
2. Iterate through the list `points`:
   1. Push the current point value onto the stack.
   2. Pop the value from the stack and add it to `total_points`.
3. Return the value of `total_points` after processing all booths.

⚠️ Common Mistakes

  • Forgetting to correctly manage the stack operations, which may lead to incorrect point summation.
  • Overcomplicating the stack usage when a direct summation would suffice.
  • Not initializing the total_points variable correctly.

I-mplement

def collect_festival_points(points):
    stack = []
    total_points = 0

    for point in points:
        stack.append(point)
        total_points += stack.pop()

    return total_points

# Example usage
print(collect_festival_points([5, 8, 3, 10]))  # Output: 26
print(collect_festival_points([2, 7, 4, 6]))   # Output: 19
print(collect_festival_points([1, 5, 9, 2, 8]))# Output: 25
Fork me on GitHub