Counter II
EasyUpdated: Aug 2, 2025
Practice on:
Problem
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 toinitand then returns it.
Examples
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"
Solution
Method 1 – Closure with Object Methods
Intuition
The key idea is to use a closure to capture the current value and initial value. The returned object exposes three methods that manipulate and return the value as required, maintaining state between calls.
Approach
- Define a function
createCounterthat takes an integerinit. - Inside, define a variable to store the current value.
- Return an object with three methods:
increment: increases the value by 1 and returns it.decrement: decreases the value by 1 and returns it.reset: sets the value toinitand returns it.
- The closure ensures the value persists across calls.
Code
JavaScript
function createCounter(init) {
let val = init;
return {
increment: () => ++val,
decrement: () => --val,
reset: () => (val = init)
};
}
TypeScript
function createCounter(init: number): { increment: () => number; decrement: () => number; reset: () => number } {
let val = init;
return {
increment: (): number => ++val,
decrement: (): number => --val,
reset: (): number => (val = init)
};
}
Complexity
- ⏰ Time complexity:
O(1), each call returns and updates a number in constant time. - 🧺 Space complexity:
O(1), only a single variable is stored in the closure.