← Questions

Data-parallel and fully sharded matrix multiply over simulated devices

0
SpaceXAICodingLast asked Apr 2026

A coding interview question asked in SpaceXAI's technical phone screen, last asked Apr 2026. It tests distributed training, parallelism and python.

You are given a small simulator of P devices that run in parallel (one thread each) and can exchange arrays only through point-to-point send and recv. Compute Y = X @ W for an input X of shape (B, D) and weights W of shape (D, H). First finish a data-parallel version, where every device has the full weights and a slice of the batch. Then write a fully sharded (FSDP-style) version, where each device stores only its slice of the weights and gathers the rest from the others just before it needs them. Say what each version costs in memory and communication.

Interface

Unless you agree on a different interface with the interviewer, assume:

class Device:
    rank: int            # 0 .. world_size - 1
    world_size: int
    def send(self, dst: int, array: np.ndarray) -> None: ...
    def recv(self, src: int) -> np.ndarray: ...   # blocks until a message from src arrives

def run_on_devices(world_size: int, fn) -> list:   # runs fn(device) on every device, returns their results
  • data_parallel_matmul(dev, x_shard, w) -> np.ndarray: x_shard is this device's B / P rows of X; w is the full W. Returns this device's rows of Y.
  • fsdp_matmul(dev, x_shard, w_shard) -> np.ndarray: w_shard is this device's D / P rows of W. Gather the full W from all devices with send/recv, compute, then drop the gathered copy. Returns this device's rows of Y.
  • all_gather(dev, piece) -> list[np.ndarray]: the helper you write for the fully sharded version; returns every device's piece in rank order.

Example

P = 2, X: (4, 6), W: (6, 4)
data parallel:  device r holds X rows 2r .. 2r+1 and all 24 numbers of W
                returns a (2, 4) block, no communication
fully sharded:  device r holds X rows 2r .. 2r+1 and W rows 3r .. 3r+2 (12 numbers)
                receives the other device's 12 numbers, rebuilds W, returns a (2, 4) block
stacking both blocks in rank order equals X @ W

Constraints

  • B and D are divisible by P unless you choose to handle the remainder.
  • send may block until the receiver calls recv, so the order of sends and receives matters.
  • Use NumPy for the local multiply; the communication is what is being tested.

Clarifying questions

  • Is send buffered or does it block until matched? Can a device send to itself?
  • Must the full Y end up on one device, on every device, or stay sharded by rows?
  • Should the weights be sharded by rows or by columns of W?
  • Is there a backward pass and gradient sync, or only the forward multiply?

What a strong answer covers

  • A correct all-gather built from send/recv that cannot deadlock: a ring, or ordering sends and receives by rank parity.
  • Data parallel: no communication in the forward pass, full W in memory on every device.
  • Fully sharded: each device stores |W| / P, receives (P - 1) / P · |W| per forward, and briefly holds the full W; when that trade is worth it.
  • Results checked against X @ W on one device for several P, including P = 1.
  • The ring all-gather taking P - 1 steps, and why its cost per device does not grow with P.

Follow-ups

  • Add the backward pass: gradients of W need an all-reduce for data parallel and a reduce-scatter for fully sharded.
  • Overlap communication with compute, for example by prefetching the next layer's shards.
  • Shard W by columns instead (tensor parallelism): what has to be communicated now?
  • How would you test for deadlocks and for messages delivered out of order?

Where it's asked

What it tests

distributed training · parallelism · python

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding