DevPrep
  • Interview Prep
  • Projects
  • Resources
  • Pricing
  • About Us
Submit Question
DevPrep
  • Pricing
  • About Us
Submit Question

Practice

  • JavaScript
  • DSA
  • Machine Coding
  • System Design

Resources

  • Learning Tracks
  • Articles
  • Roadmaps
  • Compare Concepts
  • Glossary
  • Developer Tools
  • All Questions

Company

  • About
  • Pricing

Legal

  • Privacy Policy
  • Terms of Service
DevPrep

© 2026 DevPrep. All rights reserved.

← Back to Questions
MediumMachine Coding

Machine Coding: Fix the Stale Closure Bug

9 views

Implement a JavaScript function createStaleClosureDemo that simulates a common "Stale Closure" problem often found in React hooks like useEffect or useCallback, and provide a solution to fix it.

The Scenario

You have a counter that increments every second using setInterval. However, the function passed to setInterval "captures" the initial value of the counter and refuses to update, even though the counter itself is changing.

Requirements

  1. Create a function createCounter() that returns an object with:
    • increment(): A method to increase the count.
    • logDelayed(): A method that uses setTimeout to log the count after 1 second.
  2. Demonstrate the "Stale" bug where logDelayed logs an old value if increment is called quickly after.
  3. Implement a "Fixed" version using a Functional Update or Ref-based approach.

Example Behavior

const bugged = createCounter();
bugged.logDelayed(); // Should log 0 after 1s
bugged.increment();  // count is now 1
// If bugged, the log after 1s still shows 0

Constraints

  • Use only vanilla JavaScript (ES6+).
  • The solution must explain why the closure became stale.

Sample Test Cases

Case 1
Input
[]
Expected Output
{"description": "Demonstrates initial stale closure bug. The first logDelayed call should capture 0, even if increment is called before the timeout fires.", "actions": [{"type": "call", "method": "logDelayed", "delay": 0}, {"type": "call", "method": "increment", "delay": 50}, {"type": "expect_log", "value": 0, "delay": 1000}]}
Case 2
Input
[]
Expected Output
{"bugged_output": [0, 0], "fixed_output": [0, 1]}
Case 3
Input
[]
Expected Output
{"description": "Demonstrates the fixed version. The first logDelayedFixed call should capture the updated value (1) if increment is called before the timeout fires.", "actions": [{"type": "call", "method": "logDelayedFixed", "delay": 0}, {"type": "call", "method": "increment", "delay": 50}, {"type": "expect_log", "value": 1, "delay": 1000}]}
Case 4
Input
[]
Expected Output
{"bugged_output": [0, 0, 0], "fixed_output": [0, 1, 2]}
Case 5
Input
[]
Expected Output
{"description": "Multiple increments before a single bugged logDelayed. Should still log the initial value.", "actions": [{"type": "call", "method": "logDelayed", "delay": 0}, {"type": "call", "method": "increment", "delay": 10}, {"type": "call", "method": "increment", "delay": 20}, {"type": "call", "method": "increment", "delay": 30}, {"type": "expect_log", "value": 0, "delay": 1000}]}
Case 6
Input
[]
Expected Output
{"bugged_output": [0, 0, 0, 0], "fixed_output": [0, 1, 2, 3]}

No solutions yet

Be the first to share a solution for this question.

Comments (0)

Sign in to leave a comment.

No comments yet. Be the first to comment.

Stats

Views
9
Likes
0
Solutions
0
Comments
0

Category

Frontend Engineering

Languages

JavaScriptTypeScript