ConfluentCodingFree preview
Tail -n: Print the Last N Lines of a File
Implement `tail -n`: output the last N lines of a file. Follow-ups move to a low-level file API (read N bytes, move the file pointer ±N, get file size), require pseudocode that streams the result to stdout instead of buffering it, and probe buffer-vs-offset trade-offs.
SWE
io
filesystem
streaming
medium
Frequency
High
Last asked
2026-07-19
Stage
phone-screen · onsite-coding
Problem
- Implement the behavior of
tail -n N: given a file path and N, print the last N lines of the file. - The test input is a real text file. The interviewer has you save a
.txtfile first and read strings / characters from it as the test case, so the solution must actually open and read the file. - Second half (more theoretical): you are given a minimal file API and must write pseudocode on top of it:
- read N bytes from the current position;
- move the file pointer by +N or −N bytes;
- get the file size.
- The result must be streamed to stdout, not collected into memory in full before printing.
- Stdin-based form: the first input line holds the integer N, and every remaining line is one line of the file. Print the last N file lines in their original order.
- Rules:
- fewer than N lines in the file: print all of them;
- N = 0: print nothing;
- the file may or may not end with a newline, and both must be handled correctly;
- the file can be several GB, so do not load all of it into memory at once.
- Constraints:
0 <= N <= 10^7; line lengths vary and can be large.
Examples
- N = 2 with lines
alpha,beta,gammaprintsbeta,gamma. - N = 5 with lines
one,twoprintsone,two.
Hints
Pitfalls
- File I/O consumes real time. One candidate spent so long building the file-based test case in Java (buffered reader / writer APIs, no documentation available) that the follow-up was left unfinished. Python is the lower-friction choice for this round.
- Chunk-read edge cases cause bugs: a line split across two chunks, a file that does or does not end with a newline, a file with fewer than N lines, and
seeksemantics near the start of the file. One candidate hit a bug withfile.seekand chunk boundaries and had no time left for a byte-by-byte fallback. - Some interviewers give no hints or feedback after the initial direction and treat the round as a stress test — drive the edge-case discussion yourself.
list.pop(0)as the eviction step looks fine for small inputs but is quadratic when N is large.- Memory is
O(N)plus the longest single line; with unbounded line lengths that line is a hidden cost.
Talking Points
- State the memory constraint first: loading the whole file defeats the purpose of
tail, so stream line by line and keep only the last N. - Justify the deque:
appendplus automatic eviction isO(1), while a list withpop(0)costsO(N)per eviction. - Call out the edge cases explicitly: N = 0 prints nothing, and a file shorter than N prints everything. Both fall out of the deque naturally.
- Be precise about the trailing newline: strip as you read, and add one back after the last printed line so output is consistent either way.
- Quote complexity in lines, not bytes: time
O(L), spaceO(N)plus the largest line.
Follow-ups
Rewrite the solution as pseudocode on the restricted API (read N bytes, seek ±N, file size).
Stream the output to stdout instead of holding all N lines in memory.
Preparation
- In 20 minutes, implement
tail -nin Python on a real file: read fixed-size chunks backward from the end and print the last N lines. - Write tests for: empty file, N = 0, N larger than the line count, no trailing newline, a line longer than the chunk size, and CRLF line endings.
- Practice explaining the forward-buffer solution and the backward-seek solution in two minutes each, including memory usage for a multi-GB file.
- If you code in Java, rehearse file reading / writing APIs from memory beforehand.
- Write the deque solution from memory, then swap in a list with
pop(0)and explain why it degrades.
Was this article helpful?
Comments
Sign in to join the discussion
Loading...
