2620. Counter - Detailed Explanation

Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!

Problem Statement

Description:
Implement a function called counter that returns another function (a “counter function”). Each time the returned function is called, it should return an integer that increments by one, starting from 1.

How It Works:

  • The first call returns 1.
  • The second call returns 2.
  • The third call returns 3, and so on.

The key idea is that the returned function must remember the count between calls. This is typically achieved using a closure (or a similar mechanism in languages that do not support closures directly).

Constraints:

  • The counter starts at 1.
  • The solution should use the idea of closure or an equivalent method to maintain state between calls.
  • The function returned by counter should have O(1) time per call.

Example 1:

const counter1 = counter(); console.log(counter1()); // Output: 1 console.log(counter1()); // Output: 2 console.log(counter1()); // Output: 3

Example 2:

const counter2 = counter(); console.log(counter2()); // Output: 1 console.log(counter2()); // Output: 2

Note: Each counter instance maintains its own state.

Hints

  • Hint 1: Think about how to use a closure to capture a variable that tracks the current count. In languages like JavaScript and Python, you can define an inner function that accesses a variable defined in an outer function.
  • Hint 2: Make sure the counter variable is not re-initialized on every call of the inner function. It must persist between calls.
  • Hint 3: In Python, remember to use the nonlocal keyword when modifying a variable defined in an outer (but non-global) scope.

Approaches

Closure-Based Approach (Optimal)

Idea:
Define a function counter that initializes a counter variable (starting at 0) and returns an inner function. This inner function increments the counter variable and returns its value each time it is called.

Steps:

  1. In the outer function counter, initialize a variable (e.g., count) to 0.
  2. Define an inner function (e.g., increment) that:
    • Increments the count variable.
    • Returns the updated value.
  3. Return the inner function from counter. The inner function “remembers” the variable count between calls.

Why It Works:
The inner function has access to the count variable in its lexical scope. Each call to the inner function updates this variable, thus producing the next number in the sequence.

Alternative Variations

  • Using a Class or Object:
    Instead of using a closure, you could define a class that encapsulates the count as an instance variable and provides a method (e.g., next()) to increment and return the count. This approach is common in languages without first-class function closures (or if object-oriented design is preferred).

  • Functional Programming Variations:
    Some functional languages may use stateful constructs or monads to achieve a similar effect without mutable state.

Detailed Walkthrough (Closure-Based Approach)

Let’s walk through the closure approach using a Python-like pseudocode:

  1. Initialization in the Outer Function:

    • Define a variable count and initialize it to 0.
  2. Define the Inner Function:

    • The inner function, say increment, uses the variable count from the outer scope.
    • When increment is called, it increases count by 1 and returns the new value.
    • In Python, use the nonlocal keyword to indicate that count is defined in an outer scope.
  3. Return the Inner Function:

    • The function counter returns increment, which retains access to the variable count.
  4. Example Execution:

    • When you create a counter instance by calling counter(), a new copy of count is created.
    • Calling the returned function repeatedly yields: 1, 2, 3, etc.

Code Implementation

Python Code

Python3
Python3

. . . .

Java Code

Java
Java

. . . .

Complexity Analysis

  • Time Complexity per Call:
    Each call to the returned function (or the next() method in Java) performs a constant number of operations (increment and return), so the time complexity is O(1).

  • Space Complexity:
    The closure or class instance holds a single integer variable. Therefore, the space complexity is O(1).

Additional Sections

Common Mistakes

  • Re-initializing the Counter:
    A common mistake is to reinitialize the counter variable on each call rather than maintaining it in a persistent closure or object state.

  • Forgetting to Use nonlocal (in Python):
    Without the nonlocal keyword, Python will treat the variable as local to the inner function, leading to errors or unexpected behavior.

  • Using Global Variables:
    Relying on global variables for state can lead to bugs when multiple counter instances are required.

Edge Cases

  • Multiple Instances:
    Ensure that each call to counter() returns a new independent counter. The state of one counter should not affect another.

  • Single Call:
    If the returned function is called only once, it should correctly return 1.

  • Variations:
    • Implement a counter that starts from a specified number.

    • Implement a counter that can also decrement or reset.

    • Create a counter that returns a formatted string along with the count.

Related Problems for Further Practice

Conclusion

The Counter problem is an exercise in maintaining state across function calls. The optimal solution leverages closures (or an equivalent object-oriented design) to capture and update a variable between calls. The solution is efficient (O(1) per call) and demonstrates an important concept used widely in many programming languages. The provided Python and Java implementations, along with detailed explanations, should help solidify your understanding of closures and state management in coding interviews.

TAGS
leetcode
CONTRIBUTOR
Design Gurus Team
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
What is the best format for an IT CV?
How do you implement data partitioning in microservices?
How much does PayPal pay their employees?
Related Courses
Image
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
Image
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
Image
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
Image
One-Stop Portal For Tech Interviews.
Copyright © 2025 Design Gurus, LLC. All rights reserved.
;