Challenge - 5 Problems
Timer Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
Output of recursive setTimeout vs setInterval timing
Consider the following Node.js code snippets. What will be the output behavior of each snippet over 3 seconds?
Snippet 1 uses
Snippet 2 uses recursive
Which statement best describes the difference in their output timing?
Snippet 1 uses
setInterval to print a message every 1 second.Snippet 2 uses recursive
setTimeout to print a message every 1 second.Which statement best describes the difference in their output timing?
Node.js
Snippet 1: setInterval(() => { console.log('Interval tick'); }, 1000); Snippet 2: function recursiveTimeout() { console.log('Timeout tick'); setTimeout(recursiveTimeout, 1000); } recursiveTimeout();
Attempts:
2 left
💡 Hint
Think about how each method schedules the next execution relative to the callback duration.
✗ Incorrect
setInterval schedules the callback every fixed interval regardless of how long the callback takes, which can cause overlapping if the callback is slow. Recursive setTimeout schedules the next call only after the current callback finishes, avoiding overlap and allowing dynamic intervals.
📝 Syntax
intermediate1:30remaining
Identify the syntax error in recursive setTimeout usage
Which option contains a syntax error that prevents the recursive setTimeout from working correctly?
Node.js
function tick() {
console.log('Tick');
setTimeout(tick, 1000)
}Attempts:
2 left
💡 Hint
Check the syntax of the setTimeout function call arguments.
✗ Incorrect
Option A is missing a comma between the function reference and the delay argument, causing a syntax error. Other options have correct syntax; passing delay as string (option A) is allowed but not recommended.
❓ state_output
advanced2:00remaining
State after 3 recursive setTimeout calls with varying delays
Given this code, what will be the value of
count after approximately 3500 milliseconds?Node.js
let count = 0; function tick() { count++; console.log('Tick', count); setTimeout(tick, count * 1000); } tick();
Attempts:
2 left
💡 Hint
Each delay increases by 1000ms multiplied by the current count.
✗ Incorrect
The first tick runs immediately (count=1), schedules next after 1000ms, second tick (count=2) schedules next after 2000ms, third tick (count=3) schedules next after 3000ms. After ~3500ms, three ticks have occurred.
🔧 Debug
advanced2:00remaining
Why does this recursive setTimeout cause a memory leak?
Examine this code snippet. Why might it cause increasing memory usage over time?
Node.js
function tick() {
setTimeout(() => {
console.log('Tick');
tick();
}, 1000);
}
tick();Attempts:
2 left
💡 Hint
Think about how closures capture variables and references in recursive asynchronous calls.
✗ Incorrect
Each arrow function passed to setTimeout forms a closure capturing the previous context. Over time, these nested closures accumulate, causing increased memory usage.
🧠 Conceptual
expert2:30remaining
Choosing between setInterval and recursive setTimeout for precise timing
You need to run a task every 1000ms, but the task duration varies and can sometimes take longer than 1000ms. Which approach ensures the task never overlaps and runs immediately after the previous finishes?
Attempts:
2 left
💡 Hint
Consider how each method handles task duration longer than the interval.
✗ Incorrect
Recursive setTimeout schedules the next call only after the current task completes, preventing overlap. setInterval fires at fixed intervals regardless of task duration, causing overlap if tasks take longer.