The Tenth Line interview question is a shell scripting task. You are given a text file file.txt and you need to print just the tenth line of the file. If the file has fewer than ten lines, you should output nothing. This problem tests your familiarity with basic Unix command-line tools like sed, awk, tail, and head.
Companies like Meta, Google, and Bloomberg ask this question to verify a candidate's comfort with the command line. While most software engineering is done in high-level languages, being able to quickly manipulate text files using standard utilities is a vital skill for debugging, log analysis, and automation. It tests your knowledge of common "one-liners" and your ability to choose the most efficient tool for a simple task.
There are several common patterns to solve this using shell commands:
sed: sed -n '10p' file.txt. The -n flag suppresses automatic printing, and 10p tells sed to print the tenth line.awk: awk 'NR == 10' file.txt. NR is the "Number of Records" (line number) in awk.tail and head: tail -n +10 file.txt | head -n 1. This takes the file starting from line 10 and then picks the first line of that result.File content: Line 1 ... Line 9 Line 10 Line 11
sed -n '10p' will find the 10th record and output "Line 10".tail -n +10 would output "Line 10" and "Line 11". Then head -n 1 picks "Line 10".
If the file only had 5 lines, sed and awk would reach the end without finding a 10th line, thus outputting nothing as required.One common mistake is using head -n 10 file.txt | tail -n 1. This works only if the file has at least 10 lines. If the file has 5 lines, head -n 10 returns all 5 lines, and tail -n 1 returns the 5th line—which is incorrect. Another mistake is not considering the performance on very large files; some commands read the entire file into memory, which should be avoided.
When prepping for the Tenth Line interview question, memorize at least two ways to do it (ideally sed and awk). These tools are the Swiss Army knives of the Linux command line. Understanding the Shell interview pattern—where you pipe small, specialized tools together—is a key part of becoming a productive developer.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Valid Phone Numbers | Easy | Solve | |
| Word Frequency | Medium | Solve | |
| Transpose File | Medium | Solve | |
| Is Object Empty | Easy | Solve | |
| Number of Senior Citizens | Easy | Solve |