Sign in to track progress, unlock prep layers, and browse your pipeline.Sign in
Implement tail N lines
Problem: Implement tail -n
Design and implement a command-line utility similar to tail -n that prints the last N lines of a given input.
Core Requirements
- Input Flexibility: The utility must correctly process input from both:
- A regular, seekable file.
- A non-seekable stream (e.g., standard input or a pipe).
- Newline Character Support: It must correctly identify and handle various newline conventions:
- Unix-style Line Feed (
LF,\n). - Windows-style Carriage Return + Line Feed (
CRLF,\r\n). - Classic Mac-style Carriage Return (
CR,\r).
- Unix-style Line Feed (
- Memory Efficiency: The implementation must operate under strict memory constraints, avoiding scenarios where the entire input file or stream is loaded into memory.
- Arbitrarily Long Lines: The solution should gracefully handle lines that are significantly longer than typical read buffer sizes.
- UTF-8 Correctness: When identifying and splitting lines, the utility must preserve UTF-8 character encoding integrity, ensuring multi-byte characters are not split across buffer boundaries or line breaks.
Deliverables
- Detailed data structures.
- Algorithms for processing different input types and constraints.
- Analysis of time and space complexity.
- Consideration of relevant edge cases.
Follow-up Considerations
Candidates should be prepared to discuss extensions such as:
- Implementing
tail -ffunctionality to continuously monitor and output new lines as a file grows. - Strategies for handling log rotation, file truncation, and symbolic link changes when following files.
- Adding support for the
tail -coption, which prints the lastNbytes instead of lines.
AI interview prep
Core dilemma
Efficiently locating and buffering the last N lines of potentially massive, multi-format input, while respecting strict memory limits and handling both seekable and non-seekable sources, and ensuring character encoding integrity.
Mid-level signals3
Senior signals3
Red flags3
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
Design and implement a command-line utility similar to tail -n that prints the last N lines of a given input.
Core Requirements
- Input Flexibility: The utility must correctly process input from both:
- A regular, seekable file.
- A non-seekable stream (e.g., standard input or a pipe).
- Newline Character Support: It must correctly identify and handle various newline conventions:
- Unix-style Line Feed (
LF,\n). - Windows-style Carriage Return + Line Feed (
CRLF,\r\n). - Classic Mac-style Carriage Return (
CR,\r).
- Unix-style Line Feed (
- Memory Efficiency: The implementation must operate under strict memory constraints, avoiding scenarios where the entire input file or stream is loaded into memory.
- Arbitrarily Long Lines: The solution should gracefully handle lines that are significantly longer than typical read buffer sizes.
- UTF-8 Correctness: When identifying and splitting lines, the utility must preserve UTF-8 character encoding integrity, ensuring multi-byte characters are not split across buffer boundaries or line breaks.
Deliverables
- Detailed data structures.
- Algorithms for processing different input types and constraints.
- Analysis of time and space complexity.
- Consideration of relevant edge cases.
Follow-up Considerations
Candidates should be prepared to discuss extensions such as:
- Implementing
tail -ffunctionality to continuously monitor and output new lines as a file grows. - Strategies for handling log rotation, file truncation, and symbolic link changes when following files.
- Adding support for the
tail -coption, which prints the lastNbytes instead of lines.