0
0
Node.jsframework~20 mins

Worker pool pattern in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Worker Pool Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this worker pool code snippet?
Consider this Node.js worker pool code using the 'worker_threads' module. What will be logged to the console?
Node.js
import { Worker, isMainThread, parentPort } from 'worker_threads';

if (isMainThread) {
  const worker = new Worker(new URL(import.meta.url));
  worker.on('message', (msg) => console.log('Main thread received:', msg));
  worker.postMessage('start');
} else {
  parentPort.on('message', (msg) => {
    if (msg === 'start') {
      parentPort.postMessage('worker done');
    }
  });
}
AMain thread received: worker done
BMain thread received: start
CNo output, code hangs
DSyntaxError due to import.meta.url usage
Attempts:
2 left
💡 Hint
Focus on the message events between main thread and worker.
state_output
intermediate
2:00remaining
How many tasks are processed concurrently in this worker pool?
Given this simplified worker pool code, how many tasks run at the same time?
Node.js
import { Worker } from 'worker_threads';

class WorkerPool {
  constructor(size) {
    this.size = size;
    this.workers = [];
    this.queue = [];
    this.active = 0;
  }

  runTask(task) {
    if (this.active < this.size) {
      this.active++;
      const worker = new Worker(task);
      worker.on('exit', () => {
        this.active--;
        if (this.queue.length) this.runTask(this.queue.shift());
      });
      this.workers.push(worker);
    } else {
      this.queue.push(task);
    }
  }
}

const pool = new WorkerPool(3);
pool.runTask('task1.js');
pool.runTask('task2.js');
pool.runTask('task3.js');
pool.runTask('task4.js');
AOnly 1 task runs at a time
B4 tasks run concurrently
C3 tasks run concurrently, 1 waits in queue
DNo tasks run because workers array is empty
Attempts:
2 left
💡 Hint
Check the active count compared to pool size.
🔧 Debug
advanced
2:30remaining
Why does this worker pool code cause a memory leak?
Identify the cause of the memory leak in this worker pool snippet.
Node.js
import { Worker } from 'worker_threads';

class Pool {
  constructor(size) {
    this.size = size;
    this.workers = [];
  }

  run(task) {
    const worker = new Worker(task);
    this.workers.push(worker);
    worker.on('exit', () => {
      console.log('Worker exited');
    });
  }
}

const pool = new Pool(2);
pool.run('task1.js');
pool.run('task2.js');
pool.run('task3.js');
AWorkers are never removed from the workers array after exit, causing memory leak
BThe pool size is ignored, so too many workers spawn
CWorkers are not started with worker.run(), causing them to hang
DMissing error event handler causes unhandled exceptions
Attempts:
2 left
💡 Hint
Check how the workers array is managed after workers exit.
📝 Syntax
advanced
2:00remaining
Which option correctly creates a worker pool with 4 workers?
Select the code snippet that correctly initializes a worker pool with 4 workers using 'worker_threads'.
Aconst pool = Array(4).fill(new Worker('worker.js'));
Bconst pool = Array.from({ length: 4 }, () => new Worker('worker.js'));
Cconst pool = new Array(4).forEach(() => new Worker('worker.js'));
Dconst pool = [new Worker('worker.js')] * 4;
Attempts:
2 left
💡 Hint
Remember how Array.fill() and Array.map() behave differently.
🧠 Conceptual
expert
2:30remaining
What is the main advantage of using a worker pool in Node.js?
Choose the best explanation for why a worker pool is used in Node.js applications.
ATo share memory directly between workers without serialization overhead
BTo run all tasks sequentially on the main thread to avoid concurrency issues
CTo automatically restart workers on failure without manual intervention
DTo limit the number of concurrent threads and reuse them for multiple tasks, improving performance and resource use
Attempts:
2 left
💡 Hint
Think about resource management and task handling.