Problem · Design
Concurrent Buffered File Logger
Learn this problemProblem statement
Model a lock-protected buffered file logger receiving writes from multiple logical threads. The input is the deterministic order in which operations acquire the logger lock, so process it from left to right.
["WRITE", threadId, message]appendsthreadId:messageto the in-memory buffer.["FLUSH"]appends every buffered line to the file in arrival order and empties the buffer.["CLOSE"]flushes the remaining buffer and closes the logger.
When a WRITE makes the buffer size equal to bufferCapacity, flush automatically. Each line must reach the file exactly once. Return the complete file contents as an array of lines.
Function
runConcurrentFileLogger(bufferCapacity: int, operations: String[][]) → String[]Examples
Example 1
bufferCapacity = 2operations = [["WRITE","t1","alpha"],["WRITE","t2","beta"],["WRITE","t1","gamma"],["FLUSH"],["WRITE","t3","delta"],["CLOSE"]]return = ["t1:alpha","t2:beta","t1:gamma","t3:delta"]The first two writes trigger an automatic flush. FLUSH writes gamma, and CLOSE writes the remaining delta line.
Constraints
1 <= bufferCapacity <= 2000001 <= operations.length <= 200000- The final operation is CLOSE, there is exactly one CLOSE, and no operation follows it.
- Thread identifiers and messages are nonempty strings of at most 100 characters and contain neither a colon nor a newline.