EasyTypeScript

Singleton

TypeScriptDesign PatternsCreationalSingleton

Ensure a class has only one instance, and provide a global point of access to it — every call to getInstance() must return the exact same object, so state persists across "modules" that each grab their own reference.

Implement `Counter.getInstance()` so it lazily creates the instance once and reuses it forever after, plus increment()/getValue() on that instance.

solve(ops) runs a sequence of 'increment' | 'getValue' operations against Counter.getInstance(), collecting the results of every 'getValue' call.

solve(['increment', 'increment', 'getValue'])[2] — both increments landed on the same instance.

Sample tests

Test #1Two increments land on the same instance
Input: [["increment","increment","getValue"]]
Output: [2]
Test #2Fresh instance starts at 0
Input: [["getValue"]]
Output: [0]
Test #3No ops → no results
Input: [[]]
Output: []