FastPrepImplement tail -n
Problems/Implement tail -n

Implement tail -n

Confluent logoConfluent● EasyFULLTIMEPHONE SCREENONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The judged core task matches the reported exercise at about 85%. Reports describe tail -n on a real file with limited RAM, supplied byte-read/pointer/size operations or actual language file APIs, backward reading from EOF, and streamed output in file order. One variant disallows an initial scan from the beginning. Pseudocode and runnable-code expectations differ between reports. The judged function receives file text and returns an array; real-file operations and memory limits are explicit follow-ups. LF byte handling, a fixed file, and buffer B define the worked practice implementation.

You are given an integer n and a string fileContents that holds a text file. Return its last n lines, in their original order.

Lines are separated by LF newline characters (\n). A newline at the very end of the file just ends the last line. It does not add an extra empty line. Blank lines that are really in the file are kept. If the file has fewer than n lines, return all of them. Return an empty array when n = 0 or the file is empty.

Complete tailLastNLines(int n, String fileContents). It returns a String[] with the chosen lines, without their LF characters. Here the file contents are passed in as a string.

Follow-up: actual file I/O

Now do the same thing for a real file that may be larger than the available RAM. Write the final n lines to a stream such as stdout, in file order. Do not load the whole file into memory. Do not hold the whole output in memory either.

  1. Work with byte reads, moving the file position, and the file size. You can use operations that are given to you or the real file APIs of your language.
  2. Compare two plans: read forward and keep the last n lines, or search backward from EOF. Explain how much memory the kept lines use. Do not assume that n lines always fit in memory.
  3. Handle the version where you may not first scan the file from the beginning. Read blocks backward from EOF to find where the needed lines start. Then stream those lines forward.
  4. Keep the original line order, and support output larger than your buffer. Explain the tradeoff between block size, number of read calls, and memory.
  5. If you are asked for working code, use real file APIs and test the output with assertions.

Worked file-I/O rules

For the worked follow-up, assume the file does not change, you can seek in it, it is binary, and its lines end with LF. You have a buffer of B bytes that you can choose. Use O(B) extra memory, even when one chosen line or the whole output is larger than B. First find where the last lines start. Then copy their original bytes to the output, including the final LF if there is one. A growing file and watching the file for changes are not part of this exercise.

The checks here only look at the returned lines. They do not check seeking, limited-size reads, or streamed output. The real-file code and tests in the editorial cover those.

Function

tailLastNLines(n: int, fileContents: String) → String[]

Examples

Example 1

n = 2fileContents = "a\nb\nc\n"return = ["b", "c"]

The last two lines are b and c.

Example 2

n = 0fileContents = "a\nb\nc"return = []

Keeping zero trailing lines returns an empty array.

Constraints

  • 0 <= n
  • fileContents may be large, so an efficient solution should avoid unnecessary copies.

More Confluent Onsite Interview questions

  • Sudoku Board ValidationPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Sudoku Puzzle SolverPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Sequential Phrase Search Across DocumentsPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
  • Bounded Combination Sum with Signed CandidatesONSITE INTERVIEW · Seen Oct 2026
  • Detect a Silent SensorONSITE INTERVIEW · Seen Sep 2026
  • Build an Inverted Document IndexONSITE INTERVIEW · Seen Sep 2026
  • Combination Sum with Unlimited ReuseONSITE INTERVIEW · Seen Sep 2026
  • Word Search Across DocumentsPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
  • Windowed Key-Value MapPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2025
  • Get Best PriceONSITE INTERVIEW · Seen Apr 2025

Similar questions at other companies

  • First Unique Character in a StreamAmazon · PHONE SCREEN · ONSITE INTERVIEW
  • Machine Message RouterOpenAI · PHONE SCREENOpenAI interview questions
  • Transaction Logs — Trigger / Resolve AlertsStripe · PHONE SCREENStripe interview questions
  • Select First or Last K Stream WordsGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
  • Earliest Unrepeated Letter in a StreamAdobe · PHONE SCREENAdobe interview questions
  • First Non-Repeating Character in a StreamBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
  • Extract Hashtags from a TweeteBay · ONSITE INTERVIEWeBay interview questions
  • Run-Length Encoding and DecodingAirbnb · PHONE SCREENAirbnb interview questions
  • Non-Repeating CharacterAutodesk · ONSITE INTERVIEWAutodesk interview questions
  • Container Ledger with TradesFlexport · ONSITE INTERVIEWFlexport interview questions

Interview reports that mention this question

  • Confluent Concurrency Interviews: 28 Experiences (Part 1 of 2)
  • Confluent File Manipulation Round (tail -n): 19 Experiences
  • Confluent Coding and System Design Questions: 33 Questions

Confluent interview experiences

  • Confluent Older Concurrency Rounds: 12 Experiences (Part 2 of 2)
  • Confluent SWE OA, Coding and Design Interviews: 19 Experiences
All Confluent interview questionsSee Confluent hiring insights
Saved
class Solution {
    public String[] tailLastNLines(int n, String fileContents) {
        // write your code here
    }

}
Checking account…