Given a text file file.txt, print just the 10th line of the file.
Example:
Assume that file.txt has the following content:
Line 1 Line 2 Line 3 Line 4 Line 5 Line 6 Line 7 Line 8 Line 9 Line 10
Your script should output the tenth line, which is:
Line 10
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The simplest way to find the tenth line is to read the file from the very beginning, one line at a time. We'll just keep a count of which line we are on, and when we get to the tenth one, that's our answer.
Here's how the algorithm would work step-by-step:
def get_tenth_line(file_path):
# Initialize a counter to track which line number we are currently processing.
current_line_number = 0
with open(file_path, 'r') as file_to_read:
# Iterating line-by-line is memory-efficient for large files, matching the step-by-step read.
for line_content in file_to_read:
current_line_number += 1
# This check is the core of the algorithm, stopping exactly when we've read the target line.
if current_line_number == 10:
return line_content
# This handles the edge case where the file has fewer than ten lines, returning an empty string.
return ""Instead of loading the entire file at once, which is inefficient for large files, the optimal approach is to read it one line at a time. We simply keep a running count of the lines and stop as soon as we find the tenth one.
Here's how the algorithm would work step-by-step:
line_counter = 0
with open('file.txt', 'r') as file_to_read:
# Reading line-by-line is memory-efficient, avoiding loading the entire file at once.
for current_line in file_to_read:
line_counter += 1
# This check is performed for every line to identify exactly when we reach the tenth one.
if line_counter == 10:
print(current_line.rstrip())
# Processing stops immediately after the target line is found for optimal performance.
break
| Case | How to Handle |
|---|---|
| File has fewer than ten lines | The program should produce no output or an empty string as the requested line does not exist. |
| File is extremely large and does not fit in memory | The solution must read the file line-by-line instead of loading the entire content at once to avoid memory exhaustion. |
| The tenth line itself is an empty line | The program must correctly output the empty line as valid content rather than treating it as the end of file. |
| File does not exist or user lacks read permissions | The program should handle the resulting I/O error gracefully, for instance by printing a message to standard error. |
| File uses different line endings (e.g., Windows CRLF) | The line-reading logic should correctly interpret any standard line ending format as a line separator. |
| The tenth line has no trailing newline character | The solution must return the line's content even if it is the last line and lacks a terminating newline character. |
| Input path is a directory, not a regular file | The file open operation will fail, and this specific error should be caught and handled cleanly without crashing. |
| File contains non-textual (binary) data | Reading a binary file as text may result in a decoding error or produce meaningless output. |