Concurrent Buffered File Logger
Updated
Problem statement
Model a buffered file logger that uses a lock and gets writes from several threads. The input lists the operations in the order they take 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.
More Databricks Onsite Interview questions
- Rolling Hit CounterPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Path Between Two Nodes of a Fibonacci TreeONSITE INTERVIEW · Seen Oct 2026
- Rectangular K-in-a-RowONSITE INTERVIEW · Seen Oct 2026
- Customer Revenue with Direct ReferralsPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- File Encryption Tree OptimizationPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Cipher Cover Interval EditingONSITE INTERVIEW · Seen Aug 2026
- Hybrid Run-Length and Bit-Packing EncoderONSITE INTERVIEW · Seen Aug 2026
- Key-Value Store with Hit CounterONSITE INTERVIEW · Seen Jul 2026
- Blocking Keyed Cache with Waiting ReadersONSITE INTERVIEW · Seen Jul 2026
- Snapshot Set IteratorPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2026
Similar questions at other companies
- Blocking Connection Pool Operation TraceGoogle · ONSITE INTERVIEWGoogle interview questions
- Modal Lock and Fair Modal LockOpenAI · PHONE SCREENOpenAI interview questions
- Priority Message BufferAmazon · ONSITE INTERVIEW
- Bounded Producer–Consumer QueueMakeMyTrip.com · ONSITE INTERVIEWMakeMyTrip.com interview questions
- Thread-Safe Task Storage and Worker DispatchNuro · ONSITE INTERVIEWNuro interview questions
- Circuit Breaker State MachineWise · ONSITE INTERVIEW
- Metric Counter with a Fake ClockSnap Inc. · ONSITE INTERVIEWSnap Inc. interview questions
- Per-Client Sliding-Window Rate LimiterNike, Inc. · ONSITE INTERVIEWNike, Inc. interview questions
- Article Vote TrackerRippling · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions
- Implement a Multithreaded Bounded QueueAMD · ONSITE INTERVIEWAMD interview questions