Implement tail N lines
Problem: Implement tail -N for Large Files and Streams
Implement a function tail(path: string, N: int) that prints the last N lines of a given text file or stream.
Constraints and Assumptions
- The input file can be multi-gigabyte in size.
- The entire file must not be loaded into memory.
- Lines have variable lengths.
- The total number of lines in the file is unknown beforehand.
- Memory usage for the stream-based approach must be
O(N).
Required Approaches
The solution must cover two distinct scenarios:
- Random-Access Files: For files where seeking is possible (e.g., local disk files). Candidates should explain and implement approaches such as:
- Backward scanning from the end of the file.
- Using a fixed-size ring buffer.
- Unbounded Input Streams: For scenarios where seeking is impossible (e.g., network streams, pipes). The solution must maintain
O(N)memory usage.
Edge Cases and Considerations
The discussion and implementation should address the following:
- Character Encoding: Handling of UTF-8 multibyte characters.
- Trailing Newlines: Files that may or may not end with a newline character.
- Newline Conventions: Support for different newline conventions (e.g., LF, CRLF, CR).
- Extremely Long Lines: How to handle lines that are significantly longer than typical buffer sizes.
Deliverables
- Provide detailed explanations and implementations for both required approaches.
- Analyze and state the time and space complexities for each approach.
- Propose a set of key test cases to validate the implementation.
AI interview prep
Core dilemma
Efficiently finding the end of a variable-length sequence (lines) within a massive data source, under different access patterns (random vs. sequential), while strictly adhering to memory constraints and handling complex character encodings.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Implement tail N lines
Problem: Implement tail -N for Large Files and Streams
Implement a function tail(path: string, N: int) that prints the last N lines of a given text file or stream.
Constraints and Assumptions
- The input file can be multi-gigabyte in size.
- The entire file must not be loaded into memory.
- Lines have variable lengths.
- The total number of lines in the file is unknown beforehand.
- Memory usage for the stream-based approach must be
O(N).
Required Approaches
The solution must cover two distinct scenarios:
- Random-Access Files: For files where seeking is possible (e.g., local disk files). Candidates should explain and implement approaches such as:
- Backward scanning from the end of the file.
- Using a fixed-size ring buffer.
- Unbounded Input Streams: For scenarios where seeking is impossible (e.g., network streams, pipes). The solution must maintain
O(N)memory usage.
Edge Cases and Considerations
The discussion and implementation should address the following:
- Character Encoding: Handling of UTF-8 multibyte characters.
- Trailing Newlines: Files that may or may not end with a newline character.
- Newline Conventions: Support for different newline conventions (e.g., LF, CRLF, CR).
- Extremely Long Lines: How to handle lines that are significantly longer than typical buffer sizes.
Deliverables
- Provide detailed explanations and implementations for both required approaches.
- Analyze and state the time and space complexities for each approach.
- Propose a set of key test cases to validate the implementation.