Implement staircase printing and distributed mode/median
AnthropicSoftware Engineer · OnsiteLast updated
This set of problems evaluates implementation correctness and attention to formatting for a staircase-printing routine alongside distributed-algorithms reasoning for computing a global mode/median, testing algorithmic thinking, I/O handling, synchronization, and communication-cost trade-offs in the Coding & Algorithms domain.
Problem A: Print a “staircase” pattern
Implement a function that prints a staircase with n rows.
Input
- An integer
n (n >= 1)
Output
- Print
nlines. - Line
i(1-indexed) containsiitems separated by a single space. - Formatting requirement: there must be no trailing space at the end of any line (i.e., the last item in a line should be printed without an extra delimiter).
- You must also write a small set of test cases to verify correctness (including edge cases like
n = 1).
Follow-up: modify the staircase so that it prints consecutive integers across the entire output (e.g., 1, then 2, then 3, continuing across rows), still respecting the “no trailing delimiter” formatting requirement.
Problem B: Compute mode/median in a constrained distributed setting
You have 10 worker nodes. Each node stores a partition of a dataset of integers locally.
You are given only these primitives:
read_local()to read bytes from local storage at 10 bytes/second.send(node_id, bytes)which transmits data at 1 byte/second.recv(node_id, bytes)which receives data at 1 byte/second.barrier()for synchronization.
Task
- Design and implement a workable approach to compute the global mode (most frequent value) and/or the global median.
- The interviewer will iterate with you: after you present a correct baseline, you should propose improvements to reduce end-to-end time and communication.
What to discuss while implementing
- What you choose to transmit (raw data vs summaries).
- Coordination strategy (central aggregator vs hierarchical reduction).
- How your design changes if the value range is small/known vs large/unknown.
- How you would validate correctness and measure end-to-end time under the given byte/second cost model.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.