Skip to content
Raymond Chen edited this page Aug 1, 2024 · 4 revisions

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

Problem Highlights

  • 💡 Difficulty: Easy
  • Time to complete: 5 mins
  • 🛠️ Topics: List Iteration, Conditionals

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?
  • The function can_pair() should take a list of integers item_quantities and return True if every number in the list is even, otherwise return False.
HAPPY CASE
Input: [2, 4, 6, 8]
Expected Output: True

EDGE CASE
Input: [1, 2, 3, 4]
Expected Output: False

Input: []
Expected Output: True

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.

This problem falls under: List Iteration and Conditionals.

3: P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: Define a function that iterates through the list and checks if each number is even.

1. Define the function `can_pair(item_quantities)`.
2. Iterate through each quantity in the list `item_quantities`.
3. Check if the quantity is odd using the modulo operator (`quantity % 2 != 0`).
4. If any quantity is odd, return `False`.
5. If no odd quantities are found, return `True`.

⚠️ Common Mistakes

  • Forgetting to handle the empty list case, which should return True.

4: I-mplement

Implement the code to solve the algorithm.

def can_pair(item_quantities):
    # Iterate through each quantity in the list
    for quantity in item_quantities:
        # Check if the quantity is odd
        if quantity % 2 != 0:
            return False
    # If no odd quantities are found, return True
    return True

5: R-eview

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

Call the function with the provided examples:

print(can_pair([2, 4, 6, 8]))  # Expected Output: True
print(can_pair([1, 2, 3, 4]))  # Expected Output: False
print(can_pair([]))            # Expected Output: True

Expected outputs:

True
False
True

6: E-valuate

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

  • Time Complexity: O(n) where n is the number of elements in the list since we need to iterate through all elements.
  • Space Complexity: O(1) as no additional data structures are used beyond the iteration variable.
Clone this wiki locally