Back to problems

Tail N Lines

Algorithm · Confluent · Medium

Design and build a function that mimics the operation of the Unix tail -n command. You are given a potentially enormous text file modeled as a sequence of strings and must efficiently extract the last n lines from it. If the total number of lines is less than n, simply hand back every line available. The file can contain millions of entries, so your implementation should minimize memory usage. Constraints: The number of lines in the file can be up to 10 7 . 1 6 Example 1:…

Checking your access…