- 
                Notifications
    
You must be signed in to change notification settings  - Fork 266
 
Double Trouble
TIP102 Unit 1 Session 1 Standard (Click for link to problem statements)
- 💡 Difficulty: Easy
 - ⏰ Time to complete: 5 mins
 - 🛠️ Topics: List Iteration, Loops, Return Statements
 
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 
doubled()should take a list of integers,hunny_jars, and return a new list where each element is multiplied by 2. 
HAPPY CASE
Input: [1, 2, 3]
Expected Output: [2, 4, 6]
Input: [4, 5, 6]
Expected Output: [8, 10, 12]
EDGE CASE
Input: []
Expected Output: []
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 Transformation.
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Define a function that iterates through the list, multiplies each element by 2, and stores the results in a new list which is then returned.
1. Define the function `doubled(hunny_jars)`.
2. Initialize an empty list `doubled_jars` to store the doubled values.
3. Iterate through each element in `hunny_jars`.
4. Multiply each element by 2 and append it to `doubled_jars`.
5. Return `doubled_jars`.
- Forgetting to initialize the result list.
 - Not correctly multiplying each element by 2.
 
Implement the code to solve the algorithm.
def doubled(hunny_jars):
    # Create a new list to store the doubled values
    doubled_jars = []
    
    # Loop through each element in the input list
    for jar in hunny_jars:
        # Multiply the element by 2 and add it to the new list
        doubled_jars.append(jar * 2)
    
    # Return the new list
    return doubled_jars
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(doubled([1, 2, 3]))   # Expected Output: [2, 4, 6]
print(doubled([4, 5, 6]))   # Expected Output: [8, 10, 12]
print(doubled([]))          # Expected Output: []
print(doubled([0, 1, -1]))  # Expected Output: [0, 2, -2]
Expected outputs:
[2, 4, 6]
[8, 10, 12]
[]
[0, 2, -2]
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(n) for the new list that stores the doubled values.