Bird
Raised Fist0
Node.jsframework~20 mins

Single-threaded non-blocking I/O concept in Node.js - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Node.js Non-blocking Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
How does Node.js handle multiple I/O operations?
Node.js uses a single thread for JavaScript execution. How does it manage to handle multiple I/O operations without blocking the thread?
AIt uses multiple threads internally to run JavaScript code in parallel.
BIt delegates I/O operations to the system kernel and uses callbacks to handle results asynchronously.
CIt queues all I/O operations and runs them one by one synchronously.
DIt pauses JavaScript execution until each I/O operation completes.
Attempts:
2 left
💡 Hint
Think about how Node.js can keep running code while waiting for files or network responses.
component_behavior
intermediate
2:00remaining
What is the output order of this Node.js code?
Consider this code snippet: console.log('Start'); setTimeout(() => console.log('Timeout'), 0); console.log('End'); What will be the order of the printed lines?
Node.js
console.log('Start');
setTimeout(() => console.log('Timeout'), 0);
console.log('End');
AStart, Timeout, End
BTimeout, Start, End
CStart, End, Timeout
DEnd, Start, Timeout
Attempts:
2 left
💡 Hint
Remember that setTimeout with 0 delay still runs after the current code finishes.
🔧 Debug
advanced
2:00remaining
Why does this Node.js code block the event loop?
Look at this code: const fs = require('fs'); const data = fs.readFileSync('file.txt'); console.log('File read'); What is the problem with this code in terms of Node.js single-threaded non-blocking I/O?
Node.js
const fs = require('fs');

const data = fs.readFileSync('file.txt');
console.log('File read');
AreadFileSync blocks the event loop until the file is read, stopping other code from running.
BreadFileSync runs asynchronously and does not block the event loop.
CThe code will throw an error because readFileSync requires a callback.
DThe console.log will run before the file is read.
Attempts:
2 left
💡 Hint
Check if readFileSync is synchronous or asynchronous.
📝 Syntax
advanced
2:00remaining
Which option correctly uses a Promise to read a file asynchronously in Node.js?
You want to read a file asynchronously using Promises. Which code snippet is correct?
A
const fs = require('fs');
const data = fs.readFile('file.txt').then(console.log);
B
const fs = require('fs/promises');
const data = fs.readFileSync('file.txt');
console.log(data);
C
const fs = require('fs');
fs.readFile('file.txt', (err, data) => { if (!err) console.log(data); });
D
const fs = require('fs/promises');
fs.readFile('file.txt').then(data => console.log(data.toString()));
Attempts:
2 left
💡 Hint
Look for the correct module and method that returns a Promise.
state_output
expert
2:00remaining
What is the output of this Node.js event loop example?
Analyze this code: console.log('1'); setTimeout(() => console.log('2'), 0); Promise.resolve().then(() => console.log('3')); console.log('4');
Node.js
console.log('1');
setTimeout(() => console.log('2'), 0);
Promise.resolve().then(() => console.log('3'));
console.log('4');
A1, 4, 3, 2
B1, 3, 4, 2
C1, 2, 3, 4
D1, 4, 2, 3
Attempts:
2 left
💡 Hint
Remember microtasks (Promises) run before timers in the event loop.

Practice

(1/5)
1. What does it mean that Node.js uses a single-threaded non-blocking I/O model?
easy
A. Node.js blocks the main thread until each task completes.
B. Node.js runs one main thread but can handle many tasks without waiting for each to finish.
C. Node.js uses multiple threads to run tasks in parallel.
D. Node.js cannot handle multiple tasks at the same time.

Solution

  1. Step 1: Understand single-threaded meaning

    Node.js runs on one main thread, unlike some systems that use many threads.
  2. Step 2: Understand non-blocking I/O meaning

    It does not wait for tasks like file reads to finish before moving on; it uses callbacks or events.
  3. Final Answer:

    Node.js runs one main thread but can handle many tasks without waiting for each to finish. -> Option B
  4. Quick Check:

    Single-threaded + non-blocking = handle many tasks without waiting [OK]
Hint: Single thread means one main path; non-blocking means no waiting [OK]
Common Mistakes:
  • Thinking Node.js uses multiple threads for tasks
  • Assuming Node.js waits for each task to finish before continuing
  • Confusing blocking with non-blocking I/O
2. Which of the following is the correct way to write a non-blocking file read in Node.js?
easy
A. fs.readFile('file.txt', (err, data) => { if (err) throw err; console.log(data); });
B. const data = fs.readFileSync('file.txt'); console.log(data);
C. const data = fs.readFile('file.txt'); console.log(data);
D. fs.readFile('file.txt'); console.log('done');

Solution

  1. Step 1: Identify non-blocking syntax

    Non-blocking file read uses fs.readFile with a callback to handle data after reading.
  2. Step 2: Check options for callback usage

    fs.readFile('file.txt', (err, data) => { if (err) throw err; console.log(data); }); uses fs.readFile with a callback function correctly handling error and data.
  3. Final Answer:

    fs.readFile('file.txt', (err, data) => { if (err) throw err; console.log(data); }); -> Option A
  4. Quick Check:

    Non-blocking file read uses callback = fs.readFile('file.txt', (err, data) => { if (err) throw err; console.log(data); }); [OK]
Hint: Non-blocking uses callbacks, blocking uses sync functions [OK]
Common Mistakes:
  • Using synchronous readFileSync for non-blocking tasks
  • Calling readFile without a callback
  • Expecting immediate data return from async calls
3. What will the following Node.js code output?
console.log('Start');
setTimeout(() => { console.log('Timeout done'); }, 0);
console.log('End');
medium
A. Start\nEnd\nTimeout done
B. End\nStart\nTimeout done
C. Timeout done\nStart\nEnd
D. Start\nTimeout done\nEnd

Solution

  1. Step 1: Understand synchronous logs

    console.log('Start') runs immediately and prints 'Start'.
  2. Step 2: Understand setTimeout with 0 delay

    setTimeout callback runs after current code finishes, so 'Timeout done' prints last.
  3. Step 3: Understand console.log('End')

    This runs immediately after 'Start', printing 'End' before the timeout callback.
  4. Final Answer:

    Start End Timeout done -> Option A
  5. Quick Check:

    Sync logs first, then async callback = Start\nEnd\nTimeout done [OK]
Hint: setTimeout with 0ms runs after current code finishes [OK]
Common Mistakes:
  • Assuming setTimeout runs immediately
  • Mixing order of synchronous and asynchronous logs
  • Thinking 0ms delay means instant execution
4. Identify the error in this Node.js code snippet using non-blocking I/O:
const fs = require('fs');
let content;
fs.readFile('data.txt', (err, data) => {
  if (err) throw err;
  content = data.toString();
});
console.log(content);
medium
A. The callback function is missing the error parameter.
B. The readFile method should be readFileSync for async code.
C. The variable content is logged before the file read completes.
D. The data.toString() call is invalid.

Solution

  1. Step 1: Understand async callback timing

    readFile runs asynchronously, so the callback runs after console.log(content).
  2. Step 2: Check when content is logged

    console.log(content) runs immediately, before content is assigned inside the callback.
  3. Final Answer:

    The variable content is logged before the file read completes. -> Option C
  4. Quick Check:

    Async callback runs later, so content is undefined at log [OK]
Hint: Async callbacks run later; log inside callback to see data [OK]
Common Mistakes:
  • Logging async data before callback runs
  • Confusing sync and async readFile methods
  • Ignoring error parameter in callback
5. You want to read two files in Node.js and then combine their contents. Which approach correctly uses non-blocking I/O to do this?
hard
A. Use fs.readFile once and then read the second file inside the first callback synchronously.
B. Use fs.readFileSync twice and then combine results synchronously.
C. Call fs.readFile twice with callbacks, combine results immediately after calling both without waiting.
D. Call fs.readFile twice with callbacks, combine results inside the second callback only after both finish.

Solution

  1. Step 1: Understand non-blocking file reads

    Each fs.readFile runs asynchronously and needs a callback to get data.
  2. Step 2: Combine results after both reads finish

    To combine contents, wait for both callbacks to complete, then combine inside the second callback or use coordination logic.
  3. Final Answer:

    Call fs.readFile twice with callbacks, combine results inside the second callback only after both finish. -> Option D
  4. Quick Check:

    Combine after both async reads finish = Call fs.readFile twice with callbacks, combine results inside the second callback only after both finish. [OK]
Hint: Combine data only after both async callbacks complete [OK]
Common Mistakes:
  • Combining data before async reads finish
  • Using synchronous reads in async code
  • Nesting sync reads inside async callbacks incorrectly