Write a function createCounter. It should accept an initial integer init. It should return an object with three functions.
The three functions are:
increment() increases the current value by 1 and then returns it.decrement() reduces the current value by 1 and then returns it.reset() sets the current value to init and then returns it.Example 1:
Input: init = 5, calls = ["increment","reset","decrement"] Output: [6,5,4] Explanation: const counter = createCounter(5); counter.increment(); // 6 counter.reset(); // 5 counter.decrement(); // 4
Example 2:
Input: init = 0, calls = ["increment","increment","decrement","reset","reset"] Output: [1,2,1,0,0] Explanation: const counter = createCounter(0); counter.increment(); // 1 counter.increment(); // 2 counter.decrement(); // 1 counter.reset(); // 0 counter.reset(); // 0
Constraints:
-1000 <= init <= 10000 <= calls.length <= 1000calls[i] is one of "increment", "decrement" and "reset"When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force way to create a counter involves going through all possible numbers within the specified range. We'll manually check each number to see if it matches our criteria, incrementing one by one. This guarantees we'll find the correct value eventually.
Here's how the algorithm would work step-by-step:
class Counter2:
def __init__(self, initial_value: int):
self.current_value = initial_value
self.initial_value = initial_value
def increment(self) -> int:
# Store the current value for return
return_value = self.current_value
self.current_value += 1
# Check if the counter exceeded maximum
if self.current_value > 1000000:
self.current_value = self.initial_value
return return_valueThe challenge is to create a function that acts like a counter, allowing you to increment or decrement a number. The efficient approach is to use a feature that allows you to remember the initial value and track changes relative to that starting point.
Here's how the algorithm would work step-by-step:
def createCounter(initial_value):
current_value = initial_value
def increment():
nonlocal current_value
current_value += 1
return current_value
def decrement():
nonlocal current_value
current_value -= 1
return current_value
def reset():
nonlocal current_value
# Reset current value to initial value.
current_value = initial_value
return current_value
return {
"increment": increment,
"decrement": decrement,
"reset": reset
}
def counter_ii(initial_value):
# Capture the initial value in the closure.
def counter():
nonlocal initial_value
# Store original value
original_value = initial_value
# Increment initial value
initial_value += 1
return original_value
# We return the object with special functions attached
return counter| Case | How to Handle |
|---|---|
| Initial value is positive infinity | Handle this case by either throwing an error, setting it to the maximum safe integer value, or letting the increment continue, depending on the expected behavior |
| Initial value is negative infinity | Handle this case by either throwing an error, setting it to the minimum safe integer value, or letting the increment continue, depending on the expected behavior |
| Incrementing beyond the maximum safe integer | Check for potential integer overflow and either throw an exception, cap the value, or use a larger data type. |
| Decrementing beyond the minimum safe integer | Check for potential integer underflow and either throw an exception, cap the value, or use a larger data type. |
| Initial value is NaN | Return NaN immediately as any subsequent operations will also result in NaN. |
| Incrementing after reaching maximum safe integer | Define if the counter rolls over, stays at max, or throws an error. |
| Decrementing after reaching minimum safe integer | Define if the counter rolls over, stays at min, or throws an error. |
| Calling increment/decrement excessively | Consider if limits or resource exhaustion would become a concern in the environment, and implement appropriate safeguards or logging. |