JavaScript Typed Array Interview Questions: Views, Offsets, and Numeric Conversion
Quick Overview
Use original fixed-buffer browser exercises to distinguish aliasing from copying, view spans from buffers and unsigned conversion from clamping without native-endian assumptions.
A JavaScript typed-array view does not own an independent list of numbers just because it looks like an array. It interprets bytes in a buffer, and its offset, element width and conversion rules determine the result. Trace the view’s span and conversion rule before predicting output or designing a binary parser.
This guide uses current MDN references and original browser exercises. No candidate report is used to predict an employer’s questions. The examples cover fixed-size ordinary buffers and supported Number-based views; they do not claim shared-memory synchronization, resizable-buffer behavior or worker-transfer performance. Rehearse with PracHub’s Explain arrays and memory layout and explain your reasoning before checking the discussion.

Separate the storage from its interpretation
MDN describes buffers and views as separate concepts. An ArrayBuffer supplies storage; a typed-array view supplies an element type, starting offset and length. A DataView provides explicit typed reads and writes with controllable byte order. These distinctions are the foundation for interpreting a binary layout. Typed arrays guide.
Begin an explanation by naming the underlying buffer and every view that refers to it. Two views can have different lengths and element types while sharing the same storage. Conversely, two arrays containing equal numeric values can refer to independent buffers.
Our original aliasing exercise starts with eight bytes containing 1 through 8. A Uint8Array has eight one-byte elements, so its element length and byte length happen to match. That coincidence is not a rule for every typed-array type.
A Uint16Array view over the same buffer, starting at byte offset 2 with element length 2, covers four bytes. Its length is two, its byteLength is four and its byteOffset is two. State the unit beside each number instead of referring ambiguously to an “offset of two.”
The values read through that Uint16Array depend on the platform’s native byte order. Do not infer a protocol’s byte order from the name of the view. Use an explicitly ordered read when the format defines one.
Which constructor argument is measured in bytes?
For new Uint16Array(buffer, 2, 2), the second argument is a byte offset and the third is an element count. Multiplying the count by two bytes per element explains the four-byte span. Treating the element count as bytes changes the parser’s span and can expose the wrong portion of the message.
Construction also checks alignment and bounds. In the original fixed-buffer exercise, starting a Uint16Array at byte offset 1 is misaligned and rejected. A view that extends past the buffer is also rejected. Those construction failures are different from an indexed access outside an already constructed typed array.
A DataView read uses a byte offset relative to that DataView. If the DataView itself begins two bytes into the buffer, a read at offset zero starts there. It does not jump back to the buffer’s first byte or add a typed-array element index automatically.
The getUint16() reference documents a two-byte read, explicit endianness and bounds behavior without an alignment requirement. This differs from the alignment constraint of a Uint16Array constructor over a buffer. DataView.getUint16.
Draw byte addresses and underline the view’s span. Mark the read offset inside that span. This reveals an out-of-bounds read before any arithmetic about the encoded number begins.
Show aliasing with one mutation
Create a shared window with bytes.subarray(2, 5). It contains the original values 3, 4 and 5. Setting its first element to 99 changes the original array’s element at index 2 because the two views share those bytes.
MDN’s subarray() contract specifies a new view on the same buffer with inclusive begin and exclusive end element offsets. It does not copy the selected values into independent storage. TypedArray.subarray.
Now create copy = bytes.slice(2, 5) after that first mutation. The copy contains 99, 4 and 5. Changing the shared window’s second element to 88 changes the original byte at index 3, while the copy’s second element remains 4.
const bytes = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]);
const shared = bytes.subarray(2, 5);
shared[0] = 99;
const copy = bytes.slice(2, 5);
shared[1] = 88;
// bytes: [1, 2, 99, 88, 5, 6, 7, 8]
// shared: [99, 88, 5]
// copy: [99, 4, 5]
The order matters. A copy taken before the first mutation would have a different first element. When an interviewer asks for output, trace the statements in sequence rather than describing only the final relationship between variables.
Do not call every new typed-array object a copy. The constructor overload receiving a buffer can share storage; the overload receiving another typed array creates a new typed array from its element values. Explain which overload is used and whether it converts values or reinterprets existing bytes.
Preserve the span when passing a subview
Suppose a received frame contains two prefix bytes followed by the big-endian payload bytes 0x12, 0x34. A subarray selects only the payload. Constructing new DataView(payload.buffer) exposes the whole underlying frame, including the prefix; its offset-zero read is no longer the payload’s first byte.
For this original fixed-buffer contract, construct the DataView with the payload’s buffer, byte offset and byte length. That preserves the selected span. Then perform the read at the intended offset within it.
const frame = new Uint8Array([0xaa, 0xbb, 0x12, 0x34]);
const payload = frame.subarray(2);
const view = new DataView(
payload.buffer, payload.byteOffset, payload.byteLength
);
const value = view.getUint16(0, false); // 4660
This example does not define a complete network protocol. A real parser must also validate framing, declared lengths and incomplete input. A valid view span prevents one class of mistake; it does not establish that the bytes represent a supported message.
The distinction is useful in a review. “Pass the buffer” is incomplete when the caller supplied a view into a larger allocation. State whether the function expects the whole allocation or the view’s exact span, and test a nonzero-offset view so that the contract is exercised.
Make byte order explicit
For the two bytes 0x12, 0x34, a big-endian unsigned 16-bit read produces 4,660. A little-endian read produces 13,330. Both are valid interpretations of the same bytes under different conventions; the protocol determines which one is intended.
A DataView call can make the choice visible through its littleEndian argument. Do not depend on a developer’s current machine being little-endian when the data format specifies network or file order. An implementation that happens to pass locally can still encode the wrong contract.
When writing tests, assert both interpretations of a deliberately asymmetric byte pair. A pair such as 0x00, 0x00 or identical repeated bytes cannot reveal an endianness reversal. Use the asymmetric 0x12/0x34 pair so reversed order changes the expected result.
Also specify signedness. A 16-bit unsigned value and a 16-bit signed value interpret the high bit differently. Endianness and signedness are independent questions; changing one does not resolve an error caused by the other.

Explain conversion instead of saying values are truncated
For the original conversion input 257, -1 and 1.9, Uint8Array produces 1, 255 and 1. These results reflect integer conversion into an eight-bit unsigned representation; they are not a policy that clips every value into the range zero through 255.
Uint8ClampedArray instead produces 255, 0 and 2 for those inputs. MDN describes this view as clamping to the 0–255 range. Keep clamping distinct from the unsigned-integer conversion used by Uint8Array. Uint8ClampedArray.
The original browser fixture also checks fractional tie cases rather than assuming every conversion uses Math.round(). For 0.5, 1.5, 2.5 and 3.5, the clamped results are 0, 2, 2 and 4. This makes a half-to-even boundary visible in the executed example.
| Original input | Uint8Array result | Uint8ClampedArray result |
|---|---|---|
| 257 | 1 | 255 |
| -1 | 255 | 0 |
| 1.9 | 1 | 2 |
| NaN | 0 | 0 |
A resulting zero is therefore not evidence that the caller supplied a valid zero. If a product must reject nonfinite or out-of-range values, validate before conversion. Conversion can turn invalid input into an ordinary-looking byte, so validate before the write when rejection is required.
Likewise, a byte sequence does not carry units or business meaning by itself. A value of 255 might be a color channel, a flag or an invalid sentinel under the format. The application contract determines which interpretation is allowed.
Distinguish the ways a bounds error can appear
An out-of-range indexed read on the original Uint8Array returns undefined. An indexed write beyond its length does not extend the view or allocate another byte. Do not assume the behavior of a growable ordinary Array, and do not assume every invalid access throws the same exception.
A two-byte DataView read from a one-byte span is different: the requested value does not fit and the read throws a RangeError. Constructing a misaligned Uint16Array is another distinct failure, occurring before indexed access begins. Name the operation that fails instead of saying only that the buffer is invalid.
For an application parser, an explicit remaining-length check can turn these platform behaviors into a clear protocol result such as incomplete input. That result must remain separate from an actual decoded value of zero. Catching every exception and returning zero would merge malformed or incomplete input with valid data.
Use a boundary test at the final valid two-byte start and one immediately beyond it. Then pass a subview whose span is shorter than the underlying allocation. A parser that checks only the allocation’s byte length can read outside the caller’s intended payload even when the physical bytes still exist. Validate against the declared view boundary.
Choose tests that reveal the intended boundary
The local browser exercise uses fixed buffers and explicit expected arrays. It checks shared mutations, an independent copy, byte and element lengths, a nonzero-offset DataView, the two endian interpretations and numeric conversion cases. It also checks an out-of-bounds read and a misaligned typed-array construction separately.
The executed checks cover original JavaScript behavior in one current native Chrome, using only the declared fixed-buffer cases. They do not benchmark throughput, validate a production parser or prove support for every buffer feature across deployment targets. Keep those limits beside the results when explaining what was tested.
A useful negative test passes a payload view whose underlying buffer has extra prefix bytes. Another gives only one byte to a two-byte read. A third passes an out-of-range numeric input that the application should reject before a typed-array write. Each case exposes a different failure that an all-zero happy path could miss.
Practice binary contracts and memory reasoning
These PracHub questions provide adjacent practice in memory layout, byte order, serialization and incomplete binary input. Some use other languages; they are not claims that the same language APIs or exact prompts appear in a JavaScript interview.
| PracHub practice question | What to rehearse here |
|---|---|
| Explain arrays and memory layout | Separate contiguous storage from a view of values. |
| Reason About Endianness, Ring Buffers, and Stack Behavior in Firmware | State byte order before interpreting a number. |
| Design a Streaming TLV Message Reassembler in C | Keep offsets and partial binary records explicit. |
| Implement KV store serialization | Define a serialization contract and its bounds. |
| Implement a robust socket message reader | Handle incomplete input without reading past available bytes. |
For your next rehearsal, predict the shared and copied arrays after both mutations. Then explain why a payload’s .buffer alone loses its selected span and why reading 0x12, 0x34 requires a byte-order decision.
Finish by choosing an application input that must be rejected rather than converted. State the validation rule, expected failure and test that distinguishes it from a silent zero or wrapped value. Use that counterexample to make the application’s validation contract inspectable.
Comments (0)