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
Problem Overview: The task is simple: given a file file.txt, print only the 10th line. If the file has fewer than ten lines, nothing should be printed. The challenge focuses on practical shell usage and efficient file I/O rather than complex algorithms.
Approach 1: Line-by-Line Reading (O(n) time, O(1) space)
This approach reads the file sequentially and keeps a counter for the current line number. Each time you read a new line, increment the counter. Once the counter reaches 10, print the line and stop processing. Since the file is processed one line at a time, the algorithm scans at most n lines where n is the total number of lines in the file. Memory usage stays constant because only a single line is stored at any moment. This technique is common when implementing the solution in general-purpose languages like Python, JavaScript, C++, or Java.
The key insight is that random access to a specific line in a text file is expensive, so sequential iteration is the natural solution. You avoid loading the entire file into memory and simply stop once the 10th line appears.
Approach 2: Using Shell Commands (O(n) time, O(1) space)
Shell utilities are designed for text processing, making this problem a one-liner in many environments. Tools like sed, awk, head, and tail can directly target specific lines. For example, sed -n '10p' file.txt prints only the 10th line, while awk 'NR==10' stops when the line number reaches ten. These tools internally iterate through the file until the requested line appears, which results in O(n) time complexity in the worst case and constant extra space.
This approach is ideal in a pure shell environment because it minimizes code and relies on optimized Unix text-processing utilities. It also reflects how real-world engineers manipulate logs and configuration files directly from the command line.
Recommended for interviews: Interviewers typically expect familiarity with standard shell utilities such as sed or awk. Demonstrating a command-line solution shows strong practical knowledge of shell scripting. If the interviewer asks for a general programming approach, the line-by-line iteration method clearly demonstrates control flow, file handling, and constant-space processing.
Solutions for this problem are being prepared.
Try solving it yourself| Approach | Time | Space | When to Use |
|---|---|---|---|
| Line-by-Line Reading | O(n) | O(1) | When implementing in general programming languages like Python, Java, or C++ with manual file iteration |
| Shell Utilities (sed / awk) | O(n) | O(1) | Best for shell scripting or command-line tasks where concise one-liners are preferred |
I HATE This Coding Question, but FAANG Loves it! | Majority Element - Leetcode 169 • Greg Hogg • 2,093,870 views views
Watch 9 more video solutions →Practice Tenth Line with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor