JCSU Unit 4 Problem Set 1 (Click for link to problem 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?
tigger
after applying a sequence of operations.HAPPY CASE Input: operations = ["trouncy", "flouncy", "flouncy"] Output: 2 Explanation: Start with tigger = 1. Subtract 1 for "trouncy", add 1 for each "flouncy" twice. Final value = 2.
EDGE CASE Input: operations = [] Output: 1 Explanation: With no operations, the initial value of tigger remains unchanged.
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.
For sequential update problems, we want to consider the following approaches:
Plan the solution with appropriate visualizations and pseudocode.
General Idea:
Iterate through the list of operations. If the operation is "bouncy" or "flouncy", increment tigger
. If the operation is "trouncy" or "pouncy", decrement tigger
.
tigger
to 1.operations
:
tigger
by 1.tigger
by 1.tigger
.Implement the code to solve the algorithm.
def bouncy_flouncy_trouncy_pouncy(operations):
tigger = 1 # Initialize tigger with a value of 1
for operation in operations: # Iterate through the list of operations
if operation in ["bouncy", "flouncy"]: # Both increment tigger
tigger += 1
elif operation in ["trouncy", "pouncy"]: # Both decrement tigger
tigger -= 1
return tigger # Return the final value of tigger
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
Example 1:
Example 2:
Example 3:
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Assume n is the length of the operations list.
tigger
.