Design a buffered file wrapper that flushes to disk at capacity
Quick Overview
Design a BufferedFile class that wraps a file dependency, collects writes in an in-memory buffer, and sends them to disk in one call when the buffer reaches capacity or on an explicit flush. It tests low-level class design, handling of the capacity boundary, write ordering, and the cost of building the flushed string.
Design a buffered file wrapper that flushes to disk at capacity
Company: Datadog
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: easy
Interview Round: Other
Design a `BufferedFile` class that wraps a standard `File` (a dependency with a `write(data)` method that persists data to disk) and reduces the number of disk writes by buffering data in memory.
The class is constructed with a `File` instance and a positive integer `capacity` representing the maximum buffer size (in characters). Calling `write(data)` appends `data` to an in-memory buffer instead of writing directly to disk. Calling `flush()` writes all buffered data to the underlying file in a single call and clears the buffer.
Whenever the buffered content reaches or exceeds `capacity` after a `write`, the buffer must be automatically flushed to the underlying file. The class must guarantee that no data is lost across automatic and explicit flushes, and that data is written to disk in the original order.
Implement the following methods:
- `BufferedFile(file, capacity)`: Store the underlying file and the capacity, starting with an empty buffer.
- `write(data: str)`: Append `data` to the in-memory buffer. If the buffer length reaches or exceeds capacity, automatically flush to the underlying file.
- `flush()`: Write all buffered data to the underlying file in a single call and clear the buffer. No-op if the buffer is empty.
- `get_disk_contents() -> str`: Return the concatenation of all data that has been persisted to the underlying file so far (used to verify flush behavior).
Example 1, with capacity 10:
```python
f = File()
bf = BufferedFile(f, 10)
bf.write("hello") # buffer: "hello" (len 5)
bf.write("world") # buffer: "helloworld" (len 10), auto-flushes
bf.get_disk_contents() # returns "helloworld" (auto-flushed at capacity)
bf.flush() # buffer empty, no-op
bf.get_disk_contents() # returns "helloworld"
```
The second write brings the buffer to exactly 10 characters, which reaches capacity, so it is flushed in one call. The explicit flush afterwards has nothing to write.
Example 2, with capacity 100:
```python
f = File()
bf = BufferedFile(f, 100)
bf.write("abc") # buffer: "abc"
bf.get_disk_contents() # returns "" (nothing flushed yet)
bf.write("def") # buffer: "abcdef"
bf.flush() # writes "abcdef" to disk, buffer cleared
bf.get_disk_contents() # returns "abcdef"
bf.write("ghi") # buffer: "ghi"
bf.get_disk_contents() # returns "abcdef" ("ghi" is still buffered)
```
```hint Tracking the size
Keep a running count of buffered characters so the capacity check never rescans the buffer, and think about how to build the flushed string without copying the whole buffer on every write.
```
```hint The boundary
Check which comparison matches "reaches or exceeds", and decide when it is safe to clear the buffer relative to the call on the underlying file.
```
### Constraints & Assumptions
- `1 <= capacity <= 10^6`
- `1 <= len(data) <= 10^4` per write
- At most `10^4` total operations
- `data` consists of printable ASCII characters.
- The `File` used in tests records each call to `write`. It also exposes `read_all()`, which returns everything written to it so far, in order.
### Clarifying Questions to Ask
- If `File.write` raises an error during a flush, should the buffered data stay in memory so a later flush can retry?
- Will `write` and `flush` be called from more than one thread?
- Is there a `close()` step at which any data still in the buffer must be flushed?
### What a Strong Answer Covers
- How the buffer is stored and how its length is tracked, with the cost of each `write` and `flush`.
- The auto-flush condition, including a write that lands exactly on capacity and one that overshoots it.
- How ordering and the no-data-loss guarantee hold across automatic flushes, explicit flushes and a flush on an empty buffer.
- The memory bound on the buffer, and what happens to buffered data when a disk write fails or the program stops without a flush.
### Follow-up Questions
- How would you make `BufferedFile` safe when several threads call `write` and `flush` at once, while keeping the on-disk order equal to the call order?
- How would you add a time-based flush so data never sits in memory longer than a set interval?
- Should a single write larger than `capacity` skip the buffer and go straight to the file? What changes in the ordering logic?
- How would a test prove that the buffering actually reduces the number of physical writes?
Overview: Design a BufferedFile class that wraps a file dependency, collects writes in an in-memory buffer, and sends them to disk in one call when the buffer reaches capacity or on an explicit flush. It tests low-level class design, handling of the capacity boundary, write ordering, and the cost of building the flushed string.
Design a BufferedFile class that wraps a standard File (a dependency with a write(data) method that persists data to disk) and reduces the number of disk writes by buffering data in memory.
The class is constructed with a File instance and a positive integer capacity representing the maximum buffer size (in characters). Calling write(data) appends data to an in-memory buffer instead of writing directly to disk. Calling flush() writes all buffered data to the underlying file in a single call and clears the buffer.
Whenever the buffered content reaches or exceeds capacity after a write, the buffer must be automatically flushed to the underlying file. The class must guarantee that no data is lost across automatic and explicit flushes, and that data is written to disk in the original order.
Implement the following methods:
BufferedFile(file, capacity)
: Store the underlying file and the capacity, starting with an empty buffer.
write(data: str)
: Append
data
to the in-memory buffer. If the buffer length reaches or exceeds capacity, automatically flush to the underlying file.
flush()
: Write all buffered data to the underlying file in a single call and clear the buffer. No-op if the buffer is empty.
get_disk_contents() -> str
: Return the concatenation of all data that has been persisted to the underlying file so far (used to verify flush behavior).
The second write brings the buffer to exactly 10 characters, which reaches capacity, so it is flushed in one call. The explicit flush afterwards has nothing to write.