Problem 195

Abstract / Summary

Given a text input, output only the tenth line, or the last line if there are fewer than ten.

Note

The full problem description, including helpful examples, can be found on LeetCode.

Bash 0195.bash

# Read from the file file.txt and output the tenth line to stdout.

head -10 file.txt | sed -n 10p
3 lines | 102 bytes | submitted August 17, 2023 | GitHub   100% comment ratio |

Difficulty

This problem is ranked as
easy medium hard

and has an acceptance rate of about 37% (400k+ submissions).

Typical patterns to solve this problem:
  • Shell
I solved 663 easy, 1250 medium and 309 hard problems

AI Disclaimer

Every single line of code was written by me, 100% from scratch, between August 2023 and August 2024.
However, two years later I set up this website and used various AI tools to generate most of the walkthrough / explanations below.

See this dedicated page for more details.
LeetCode profile | GitHub repo

Code Walkthrough

Overview

The script is a compact Bash pipeline that extracts the tenth line from a text file named file.txt. It combines two classic Unix utilities-head and sed-to limit the amount of data read from the file and then select the desired line. The pipeline is executed in a single command line, making it suitable for quick ad-hoc inspections or inclusion in larger shell scripts.

Key Steps

The first stage uses head to read only the initial segment of the file:

head -10 file.txt

head -10 stops reading after the tenth line, ensuring that the subsequent command receives at most ten lines regardless of the file’s total size.

The second stage pipes the output of head into sed, which is instructed to suppress automatic printing and to emit only the tenth line:

sed -n 10p

The -n flag disables the default behavior of printing every processed line, while 10p tells sed to print the line whose address is 10. Because the input stream contains at most ten lines, the address matches only the final line of the stream.

The complete pipeline connects these two stages:

head -10 file.txt | sed -n 10p

Edge Cases

When the source file contains fewer than ten lines, head -10 outputs all available lines without error. The subsequent sed -n 10p receives a stream that does not include a tenth line, so the address does not match any input line and nothing is printed. This behavior gracefully handles short files by producing no output rather than raising an exception.

If the file includes lines that are empty, contain whitespace, or embed special characters, both head and sed treat them as ordinary text. The pipeline therefore returns the exact tenth line content, preserving any leading or trailing spaces and non-printing characters that may be present.

When the file is a regular text file with standard line endings, the pipeline works as expected. In the case of a file that uses a different line-ending convention (for example, carriage-return-line-feed), head still counts logical lines based on newline characters, and sed will output the line as it appears in the input stream.

Performance Optimizations

The use of head -10 is a deliberate optimization that limits I/O. By stopping the read operation after ten lines, the command avoids scanning the remainder of a potentially large file. This reduces disk access time and memory consumption, especially when the file resides on slower storage media.

Passing a truncated stream to sed further improves efficiency. The -n option prevents sed from performing its default line-by-line output, which would otherwise generate unnecessary data. The explicit address 10p ensures that sed evaluates only the line of interest, avoiding any additional processing for earlier lines.

Together, these choices create a pipeline that performs minimal work: it reads just enough data to locate the target line and emits only that line, making the command fast and lightweight even on very large files.

written by openai gpt

ELI5 - Simplified Explanation

Imagine you have a really tall stack of ten giant picture books sitting on your desk. Each page in those books is like a line of text in a computer file. This code is a little robot assistant that helps you find exactly what is on the tenth page of your stack. First, the robot uses the head command to grab the top ten books from the pile, which is like holding onto the first ten pages. Then, it uses the sed command to look at that small pile and pick out just the last one, which is the tenth page, so it can show it to you on your screen. It is like a treasure hunt where the robot knows exactly which page you want to see.

written by google gemini

Code Quality

Several LLMs ranked my code quality on a scale from 1 (very poor) to 10 (outstanding):
n = 21 | median = 7 | μ = 6.10 | σ = 1.85

Complexity Analysis

not analyzed yet

Solved On The Same Day

13 medium 3 hard
17 solutions   August 17, 2023

Extra

There are a few extra pages giving you some insights into my setup:
  • Tech Stack - my development environment and tools
  • AI - how AI helped me create this website (NOT solving the problems)
  • Progress - progression over time
  • Quality - code quality