Skip to content

Poohsticks

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

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 count_less_than() should take a list of integers race_times and an integer threshold and return the count of elements in race_times that are strictly less than threshold.
HAPPY CASE
Input: race_times = [1, 2, 3, 4, 5, 6], threshold = 4
Expected Output: 3

Input: race_times = [5, 6, 7, 8], threshold = 7
Expected Output: 2

EDGE CASE
Input: race_times = [], threshold = 4
Expected Output: 0

P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: Define a function that iterates through the list, counts how many elements are less than the threshold, and returns the count.

1. Define the function `count_less_than(race_times, threshold)`.
2. Initialize a variable `count` to 0.
3. Iterate through each element in `race_times`.
4. If an element is less than `threshold`, increment `count`.
5. Return `count`

⚠️ Common Mistakes

  • Forgetting to initialize the count variable.
  • Incorrectly comparing elements to the threshold.

I-mplement

Implement the code to solve the algorithm.

def count_less_than(race_times, threshold):
    # Initialize the count variable to 0
    count = 0
    
    # Iterate through each time in the race_times list
    for time in race_times:
        # If the time is less than the threshold, increment the count
        if time < threshold:
            count += 1
    
    # Return the count of race times less than the threshold
    return count
Clone this wiki locally