0
0
Node.jsframework~20 mins

SharedArrayBuffer for shared memory in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
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.