Bird
Raised Fist0
Node.jsframework~20 mins

SharedArrayBuffer for shared memory 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
🎖️
Shared Memory Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
What does SharedArrayBuffer enable in Node.js?
Which of the following best describes the purpose of SharedArrayBuffer in Node.js?
AIt creates isolated memory copies for each thread to avoid conflicts.
BIt encrypts data automatically when shared between threads.
CIt allows multiple threads to share the same memory space for faster data exchange.
DIt manages asynchronous file system operations efficiently.
Attempts:
2 left
💡 Hint
Think about how threads can communicate without copying data.
component_behavior
intermediate
1:30remaining
What is the output of this Node.js code using SharedArrayBuffer?
Consider the following code snippet. What will be logged to the console?
Node.js
const sab = new SharedArrayBuffer(4);
const int32 = new Int32Array(sab);
int32[0] = 42;
console.log(int32[0]);
A42
Bundefined
C0
DThrows a TypeError
Attempts:
2 left
💡 Hint
Check how Int32Array views the SharedArrayBuffer.
📝 Syntax
advanced
2:00remaining
Which option correctly creates a SharedArrayBuffer of 16 bytes and a Uint8Array view?
Select the code snippet that correctly creates a SharedArrayBuffer of 16 bytes and a Uint8Array view over it.
A
const sab = new SharedArrayBuffer();
const view = new Uint8Array(sab, 16);
B
const sab = SharedArrayBuffer(16);
const view = Uint8Array(sab);
C
const sab = new SharedArrayBuffer(16);
const view = new Uint8Array();
D
const sab = new SharedArrayBuffer(16);
const view = new Uint8Array(sab);
Attempts:
2 left
💡 Hint
Remember the correct syntax for constructors in JavaScript.
🔧 Debug
advanced
2:00remaining
Why does this code throw a RangeError?
Given the code below, why does it throw a RangeError?
Node.js
const sab = new SharedArrayBuffer(8);
const view = new Int32Array(sab, 4, 2);
console.log(view.length);
AThe byteOffset 4 plus length 2 exceeds the buffer size of 8 bytes.
BInt32Array cannot be created from SharedArrayBuffer.
CThe length parameter must be omitted when byteOffset is specified.
DSharedArrayBuffer size must be a multiple of 16 bytes.
Attempts:
2 left
💡 Hint
Calculate total bytes needed: byteOffset + length * bytes per element.
state_output
expert
2:30remaining
What is the final value of sharedArray[0] after two workers increment it?
Assume two worker threads share a SharedArrayBuffer with an Int32Array view named sharedArray. Each worker runs this code once: Atomics.add(sharedArray, 0, 1); If the initial value of sharedArray[0] is 0, what is the final value after both workers finish?
A0
B2
CUndefined due to race condition
D1
Attempts:
2 left
💡 Hint
Atomics methods ensure safe updates even with multiple threads.

Practice

(1/5)
1. What is the main purpose of SharedArrayBuffer in Node.js?
easy
A. To create a memory area that multiple threads can access simultaneously
B. To store large strings efficiently
C. To replace regular arrays with faster versions
D. To handle file system operations asynchronously

Solution

  1. Step 1: Understand Shared Memory Concept

    SharedArrayBuffer is designed to create a block of memory that can be shared between multiple threads or workers.
  2. Step 2: Compare with Other Options

    Options B, C, and D describe unrelated features: string storage, array speed, and file system operations, which are not the purpose of SharedArrayBuffer.
  3. Final Answer:

    To create a memory area that multiple threads can access simultaneously -> Option A
  4. Quick Check:

    Shared memory = multiple threads access [OK]
Hint: SharedArrayBuffer is about sharing memory across threads [OK]
Common Mistakes:
  • Thinking it stores strings or files
  • Confusing with normal arrays
  • Assuming it handles async file tasks
2. Which of the following is the correct way to create a SharedArrayBuffer of 1024 bytes in Node.js?
easy
A. const sab = SharedArrayBuffer(1024);
B. const sab = SharedArrayBuffer.new(1024);
C. const sab = new SharedArrayBuffer();
D. const sab = new SharedArrayBuffer(1024);

Solution

  1. Step 1: Check the correct constructor usage

    The SharedArrayBuffer must be created with the new keyword and a size in bytes as argument.
  2. Step 2: Validate each option

    const sab = new SharedArrayBuffer(1024); uses new SharedArrayBuffer(1024), which is correct. const sab = SharedArrayBuffer(1024); misses new. const sab = new SharedArrayBuffer(); misses size argument. const sab = SharedArrayBuffer.new(1024); uses invalid syntax.
  3. Final Answer:

    const sab = new SharedArrayBuffer(1024); -> Option D
  4. Quick Check:

    Use new with size in bytes [OK]
Hint: Always use 'new' with SharedArrayBuffer and specify size [OK]
Common Mistakes:
  • Omitting 'new' keyword
  • Not providing size argument
  • Using incorrect constructor syntax
3. Given the code below, what will be the output?
const sab = new SharedArrayBuffer(4);
const int32 = new Int32Array(sab);
int32[0] = 10;
Atomics.add(int32, 0, 5);
console.log(int32[0]);
medium
A. 10
B. 5
C. 15
D. NaN

Solution

  1. Step 1: Understand initial value and Atomics.add

    The int32[0] is set to 10. Then Atomics.add adds 5 to this value atomically.
  2. Step 2: Calculate the new value

    10 + 5 = 15, so int32[0] becomes 15.
  3. Final Answer:

    15 -> Option C
  4. Quick Check:

    Atomics.add adds value safely = 15 [OK]
Hint: Atomics.add adds value and returns old value, array updates [OK]
Common Mistakes:
  • Expecting Atomics.add to return new value
  • Ignoring atomic operation effect
  • Confusing initial and updated values
4. What is wrong with the following code snippet?
const sab = new SharedArrayBuffer(8);
const uint8 = new Uint8Array(sab);
uint8[0] = 255;
Atomics.store(uint8, 0, 256);
console.log(uint8[0]);
medium
A. Atomics.store cannot be used with Uint8Array
B. 256 is out of range for Uint8Array element
C. SharedArrayBuffer size is too small
D. Uint8Array cannot be created from SharedArrayBuffer

Solution

  1. Step 1: Check Uint8Array element range

    Uint8Array elements can only hold values from 0 to 255. The value 256 is out of this range.
  2. Step 2: Understand effect of storing 256

    Storing 256 wraps around to 0 because 256 mod 256 = 0, so uint8[0] becomes 0, not 256.
  3. Final Answer:

    256 is out of range for Uint8Array element -> Option B
  4. Quick Check:

    Uint8 max value 255, 256 wraps to 0 [OK]
Hint: Uint8Array values must be 0-255; higher values wrap [OK]
Common Mistakes:
  • Assuming Atomics.store rejects Uint8Array
  • Ignoring value wrapping behavior
  • Thinking SharedArrayBuffer size is insufficient
5. You want to safely increment a shared counter in a Node.js worker thread using SharedArrayBuffer. Which code snippet correctly increments the counter without race conditions? // Shared buffer and typed array const sab = new SharedArrayBuffer(4); const counter = new Int32Array(sab); counter[0] = 0; // Increment function function increment() { // Which line correctly increments? }
hard
A. Atomics.add(counter, 0, 1);
B. counter[0] = counter[0] + 1;
C. counter[0] += 1;
D. counter[0]++;

Solution

  1. Step 1: Understand race conditions in shared memory

    Directly modifying counter[0] with normal operators can cause race conditions when multiple threads run concurrently.
  2. Step 2: Use atomic operations for safe increments

    Atomics.add(counter, 0, 1) safely increments the value at index 0 without conflicts.
  3. Final Answer:

    Atomics.add(counter, 0, 1); -> Option A
  4. Quick Check:

    Use Atomics for safe shared memory updates [OK]
Hint: Always use Atomics methods to update shared memory safely [OK]
Common Mistakes:
  • Using normal arithmetic on shared memory
  • Ignoring atomicity causing race conditions
  • Assuming ++ or += are thread-safe