Buffered File Writer

Asked at Datadog· Screen

Reported by the problem's creator, not verified

Import another question

A file accepts writes, and every write to it is slow. You implement a writer that sits in front of the file and collects characters in an in-memory buffer, so the file receives fewer, larger writes. The buffer holds at most capacity characters, and every character counts as one byte. When the buffer holds exactly capacity characters, its contents go to the file as a single write and the buffer is emptied. Characters reach the file in the order they were written; none are lost or sent twice. Each test creates one writer and checks the return value of every operation.

Examples

  1. Example 1

    Input: init = [8], operations = [["write",["abc"]],["write",["defgh"]],["write",["ij"]],["flush",[]],["flush",[]]]

    Output: [[],["abcdefgh"],[],["ij"],[]]

    Explanation: abc leaves 5 free slots, so the next write fills the buffer exactly and abcdefgh goes out as one write. ij waits in the buffer until flush sends it; the second flush finds the buffer empty and sends nothing.

Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question

Code

Loading editor…

Ln 1, Col 1

Start an attempt to use Coach.