Problem · Design

Durable Data Writer

Learn this problem
MediumDatabricks logoDatabricksFULLTIMEONSITE INTERVIEW

Problem statement

Simulate a durable record writer by processing a finite operation sequence. The writer starts in the running state with empty pending and durable storage.

Each operation is one of:

  • ["WRITE", record]: append record to the in-memory pending buffer.
  • ["FLUSH"]: atomically append every pending record to durable storage, preserving order, then clear the pending buffer. Flushing an empty buffer does nothing.
  • ["CRASH"]: discard only pending records and enter the crashed state. Durable records remain unchanged.
  • ["RECOVER"]: leave the crashed state with the durable records intact and an empty pending buffer.
  • ["READ"]: return a snapshot of the durable records. Pending records are not visible.

After CRASH, the next operation is RECOVER. Return one string array for every READ operation, in operation order. Duplicate records are preserved.

Function

simulateDurableWriter(operations: String[][]) → String[][]

Examples

Example 1

operations = [["WRITE","A"],["WRITE","B"],["READ"],["FLUSH"],["READ"]]return = [[],["A","B"]]

The first read sees no durable records because both writes are pending. After the flush, both records are durable and visible in order.

Example 2

operations = [["WRITE","a"],["FLUSH"],["WRITE","b"],["CRASH"],["RECOVER"],["READ"],["WRITE","c"],["FLUSH"],["READ"]]return = [["a"],["a","c"]]

Record a survives because it was flushed. Pending record b is lost in the crash. After recovery, c is written and flushed after a.

Constraints

  • 1 <= operations.length <= 10000
  • Every operation has a valid name and arity.
  • Every record is a non-empty string of at most 100 characters.
  • The total number of WRITE operations is at most 5000.
  • After each CRASH, the next operation is RECOVER.

More Databricks problems

drafts saved locally
public String[][] simulateDurableWriter(String[][] operations) {
    // Write your code here.
}
operations[["WRITE","A"],["WRITE","B"],["READ"],["FLUSH"],["READ"]]
expected[["", "A", "B"]]
checking account