Build a Stateful Counter Function

Quick Overview

Practice JavaScript closures by returning a counter function with independent state, an exact starting value, and one-step increments on each call.

Build a Stateful Counter Function

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem Implement `createCounter(n)` in JavaScript. It must return a function that remembers its next value between calls. The first call to the returned function produces `n`. Each later call produces one more than the preceding call: `n + 1`, then `n + 2`, and so on. Creating another counter must give it its own state. ### Invocation Format Call `createCounter(n)` once to obtain a counter, then invoke that returned function with no arguments. A fixture may list the word `"call"` for each invocation and collect the returned numbers in order. That list describes the test driver; it is not a second parameter of `createCounter`. ### Examples **Example 1:** ```javascript const counter = createCounter(4); counter(); // 4 counter(); // 5 counter(); // 6 ``` The starting value is returned before the counter advances. **Example 2:** ```javascript const first = createCounter(-1); const second = createCounter(8); first(); // -1 second(); // 8 first(); // 0 second(); // 9 ``` Calling one counter does not change the other counter's next value. ### Constraints - `n` is an integer from `-1000` through `1000`, inclusive. - A counter is invoked between `0` and `1000` times, inclusive. - Every listed operation is `"call"`; there are no reset or decrement operations. - Use the JavaScript function-returning interface. Returning a precomputed array of numbers does not satisfy it. ### What a Strong Answer Covers - Retain the next number across calls to the returned function. - Return the original starting value on the first call and advance by exactly one afterward. - Keep the state of separately created counters independent. ### Follow-up Questions - What changes if the implementation increments the stored value before returning it? - Why would a single shared variable outside `createCounter` break the second example?

Overview: Practice JavaScript closures by returning a counter function with independent state, an exact starting value, and one-step increments on each call.

Google logo
Google
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

Implement createCounter(n) in JavaScript. It must return a function that remembers its next value between calls.

The first call to the returned function produces n. Each later call produces one more than the preceding call: n + 1, then n + 2, and so on. Creating another counter must give it its own state.

Invocation Format

Call createCounter(n) once to obtain a counter, then invoke that returned function with no arguments. A fixture may list the word "call" for each invocation and collect the returned numbers in order. That list describes the test driver; it is not a second parameter of createCounter.

Examples

Example 1:

const counter = createCounter(4);
counter(); // 4
counter(); // 5
counter(); // 6

The starting value is returned before the counter advances.

Example 2:

const first = createCounter(-1);
const second = createCounter(8);
first();  // -1
second(); // 8
first();  // 0
second(); // 9

Calling one counter does not change the other counter's next value.

Constraints

  • n is an integer from -1000 through 1000 , inclusive.
  • A counter is invoked between 0 and 1000 times, inclusive.
  • Every listed operation is "call" ; there are no reset or decrement operations.
  • Use the JavaScript function-returning interface. Returning a precomputed array of numbers does not satisfy it.

What a Strong Answer Covers Guidance

  • Retain the next number across calls to the returned function.
  • Return the original starting value on the first call and advance by exactly one afterward.
  • Keep the state of separately created counters independent.

Follow-up Questions Guidance

  • What changes if the implementation increments the stored value before returning it?
  • Why would a single shared variable outside createCounter break the second example?