Design log queries and a buffered writer
Problem Description
This problem consists of two independent parts: designing a log store with time-range queries and implementing a buffered writer.
Part A: Log Store with Time-Range Queries
Design and implement a data structure capable of ingesting log entries. Each log entry consists of a unique identifier (ID) and an ISO-8601 timestamp (e.g., YYYY-MM-DD HH:MM:SS).
The data structure must support the following operations:
add(id, timestamp): Ingest a new log entry with the given unique ID and timestamp.query(start, end, granularity): Retrieve all unique IDs whose associated timestamps fall within the inclusive range[start, end]. Thegranularityparameter specifies the level of precision for the query, and can be one of:Year,Month,Day,Hour,Minute,Second.
For this part, you should describe:
- The chosen data structures.
- The time and space complexities for both
addandqueryoperations. - How ordering of log entries is maintained.
- Strategies for managing memory usage effectively.
Part B: Buffered Writer
Implement a BufferedWriter class that operates over an abstract sink.write(bytes) interface. The sink.write method is assumed to potentially accept partial writes (i.e., it might write fewer bytes than requested and return the number of bytes actually written).
The BufferedWriter must provide the following public methods:
write(data): Accepts a byte arraydatato be written to the sink.flush(): Forces any buffered data to be written to the sink.close(): Flushes any remaining data and releases resources.
Key requirements for the BufferedWriter:
- Preserve the original order of data written.
- Chunk large input data into fixed-size internal buffers before writing to the sink.
- Minimize the number of system calls to the underlying
sink.writemethod. - Robustly handle errors and partial writes returned by
sink.write.
For this part, you should discuss:
- Options for ensuring thread-safety.
- The complexity trade-offs involved in your design choices.
AI interview prep
Core dilemma
Designing scalable and robust systems for both time-series data querying and efficient, fault-tolerant I/O operations, each with distinct performance and resource management challenges.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Design log queries and a buffered writer
Problem Description
This problem consists of two independent parts: designing a log store with time-range queries and implementing a buffered writer.
Part A: Log Store with Time-Range Queries
Design and implement a data structure capable of ingesting log entries. Each log entry consists of a unique identifier (ID) and an ISO-8601 timestamp (e.g., YYYY-MM-DD HH:MM:SS).
The data structure must support the following operations:
add(id, timestamp): Ingest a new log entry with the given unique ID and timestamp.query(start, end, granularity): Retrieve all unique IDs whose associated timestamps fall within the inclusive range[start, end]. Thegranularityparameter specifies the level of precision for the query, and can be one of:Year,Month,Day,Hour,Minute,Second.
For this part, you should describe:
- The chosen data structures.
- The time and space complexities for both
addandqueryoperations. - How ordering of log entries is maintained.
- Strategies for managing memory usage effectively.
Part B: Buffered Writer
Implement a BufferedWriter class that operates over an abstract sink.write(bytes) interface. The sink.write method is assumed to potentially accept partial writes (i.e., it might write fewer bytes than requested and return the number of bytes actually written).
The BufferedWriter must provide the following public methods:
write(data): Accepts a byte arraydatato be written to the sink.flush(): Forces any buffered data to be written to the sink.close(): Flushes any remaining data and releases resources.
Key requirements for the BufferedWriter:
- Preserve the original order of data written.
- Chunk large input data into fixed-size internal buffers before writing to the sink.
- Minimize the number of system calls to the underlying
sink.writemethod. - Robustly handle errors and partial writes returned by
sink.write.
For this part, you should discuss:
- Options for ensuring thread-safety.
- The complexity trade-offs involved in your design choices.