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

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 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

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 Counting.

3: 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.

4: 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

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(count_less_than([1, 2, 3, 4, 5, 6], 4))  # Expected Output: 3
print(count_less_than([], 4))                  # Expected Output: 0
print(count_less_than([5, 6, 7, 8], 7))        # Expected Output: 2
print(count_less_than([7, 8, 9], 5))           # Expected Output: 0

Expected outputs:

3
0
2
0

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 count variable.
Clone this wiki locally