Design a buffered file writer that splits large writes into capacity-sized flushes
Company: Datadog
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design a `BufferedFileWriter` that wraps a file-like sink and buffers `write` operations in memory up to a fixed buffer capacity, measured in characters.
Buffered data is only flushed to the underlying sink when the buffer becomes full or when `flush` is explicitly invoked. If a single `write` call's input exceeds the remaining buffer space, the data must be split across multiple buffer fills and flushes until all characters are accepted.
To make the behavior observable for testing, the writer maintains an internal log of flushed chunks (each chunk equals the data that was flushed in one call to the underlying sink). The `get_flushed` method returns this log, and `get_buffer` returns the data currently held in the buffer but not yet flushed.
Implement the following methods:
- `BufferedFileWriter(capacity)`: Create a writer with an empty buffer and an empty flush log.
- `write(data: str)`: Append `data` to the in-memory buffer. If `data` does not fit in the remaining buffer capacity, fill the buffer, flush it to the underlying sink, then continue with the remaining characters, repeating until all of `data` is consumed.
- `flush()`: Flush the current buffer contents (if any) to the underlying sink and clear the buffer. Has no effect if the buffer is empty.
- `close()`: Flush any remaining buffered data to the underlying sink. After close, further writes are not expected.
- `get_buffer() -> str`: Return the current contents of the in-memory buffer that have not yet been flushed.
- `get_flushed() -> List[str]`: Return the list of chunks that have been flushed to the underlying sink, in flush order.
Example 1, with capacity 5:
```python
writer = BufferedFileWriter(5)
writer.write("abc") # buffer: "abc"
writer.get_buffer() # returns "abc"
writer.write("defgh") # fills buffer to "abcde", flushes, buffer: "fgh"
writer.get_flushed() # returns ["abcde"]
writer.get_buffer() # returns "fgh"
writer.flush() # flushes "fgh"
writer.get_flushed() # returns ["abcde", "fgh"]
writer.get_buffer() # returns ""
```
Only 2 characters of `"defgh"` fit after `"abc"`, so `"de"` completes the first chunk and `"fgh"` starts a new buffer.
Example 2, with capacity 3:
```python
writer = BufferedFileWriter(3)
writer.write("abcdefgh") # splits into "abc", "def", then buffers "gh"
writer.get_flushed() # returns ["abc", "def"]
writer.get_buffer() # returns "gh"
writer.close() # flushes remaining "gh"
writer.get_flushed() # returns ["abc", "def", "gh"]
writer.get_buffer() # returns ""
```
The 8-character write fills and flushes the 3-character buffer twice, and the last 2 characters wait in the buffer until `close`.
```hint Consuming the input
Walk through `data` with an index. At each step, two quantities limit how many characters you can take; take the smaller one.
```
```hint When to flush
Decide what the buffer should look like right after a write fills it exactly, and check what an explicit flush or close on an empty buffer adds to the log.
```
### Constraints & Assumptions
- `1 <= capacity <= 10^6`
- `0 <= len(data) <= 10^5` per `write` call
- At most `10^4` total operations
- `data` consists of printable ASCII characters, so one character is one unit of capacity.
- A write that leaves the buffer exactly full flushes it right away, so the buffer never sits full between calls.
- The flush log stands in for the sink in tests: each flush adds one chunk to the list returned by `get_flushed`.
### Clarifying Questions to Ask
- Should a `write` after `close` raise an error or be ignored?
- Can `close` be called more than once?
- Should `get_flushed` return a copy, so callers cannot change the internal log?
- If the real sink fails during a flush, should the data stay in the buffer for a retry?
### What a Strong Answer Covers
- The loop that splits one write across several buffer fills, and why it always makes progress.
- Exactly when a flush happens: a full buffer, an explicit flush, or close, with no empty chunks logged for an empty buffer.
- How the buffer is stored so total work stays linear in the number of characters written.
- Behavior around close, writes after close, and what the two getters expose.
### Follow-up Questions
- How would you make the writer safe when several threads write at once, keeping each write's characters contiguous in the output?
- When the buffer is empty and the remaining input is at least `capacity` long, how could you skip copying it through the buffer, and would the flush log change?
- How would you add a time-based flush so buffered data reaches the sink within a set interval?
- What changes if capacity is measured in bytes and `data` can contain multi-byte UTF-8 characters?
Overview: Design a BufferedFileWriter that holds writes in a fixed-capacity memory buffer, splits any write that does not fit across several flushes, and logs each chunk sent to the sink. It tests careful index and boundary handling, flush and close semantics, and keeping total work linear in the characters written.