Domain 1EasyCORE

Tenth Line

The problem requires printing the 10th line from a given text file named `file.txt`. The input is a text file with an unknown number of lines, and the output should be the content of the 10th line if it exists. If the file contains fewer than 10 lines, no output should be produced.

Domain: Easy•Updated: 2026-08-31
#Algorithms#LeetCode#qwen2.5-coder:14b#**FAANG asked:** 1

195. Tenth Line

Model: qwen2.5-coder:14b
Topics:
FAANG asked: 1
Acceptance rate: 32.8%


Problem Breakdown

The problem requires printing the 10th line from a given text file named file.txt. The input is a text file with an unknown number of lines, and the output should be the content of the 10th line if it exists. If the file contains fewer than 10 lines, no output should be produced.

Inputs:

  • A text file (file.txt) containing an arbitrary number of lines.

Outputs:

  • The content of the 10th line if it exists.
  • No output if the file has fewer than 10 lines.

Implicit Constraints/Edge Cases:

  • The file might be empty, resulting in no output.
  • The file could have more or fewer than 10 lines.
  • Lines are separated by newline characters (\n).

Intuition

A brute-force approach would involve reading the entire file line by line until the 10th line is reached. This method works but has a time complexity of O(n), where n is the number of lines in the file, which could be inefficient for large files.

The key insight to improve this is to use a more efficient method that directly accesses the 10th line without reading all previous lines. One such method is using sed, a stream editor command-line utility available on Unix-like systems, which can directly print specific lines from a file.

Approach

To solve this problem efficiently, we will use the sed command to extract the 10th line directly. The sed command is well-suited for this task because it allows us to specify a particular line number to output without needing to read through all preceding lines.

Algorithm Steps:

  1. Use the sed command with the -n option to suppress automatic printing.
  2. Use the 10p argument to print only the 10th line.
  3. Execute this command in a shell environment from within the Python script using the subprocess module.

Complexity Analysis

  • Time Complexity: O(1) for accessing the specific line, as sed directly jumps to the desired line without reading all previous lines.
  • Space Complexity: O(1), as no additional data structures are used beyond the command execution overhead.

Solution

import subprocess

def print_tenth_line():
    # Use sed to print the 10th line of the file
    result = subprocess.run(['sed', '-n', '10p', 'file.txt'], capture_output=True, text=True)
    
    # Check if the command was successful and output is not empty
    if result.returncode == 0 and result.stdout.strip():
        print(result.stdout.strip())

# Call the function to execute the solution
print_tenth_line()

Edge Cases Handled

  • Empty File: If file.txt is empty, sed will return an empty output, and no line will be printed.
  • File with Fewer than 10 Lines: If there are fewer than 10 lines, sed will also return an empty output, resulting in no print statement.
  • File with Exactly 10 Lines: The 10th line will be printed as expected.
  • File with More than 10 Lines: Only the 10th line will be printed, and all other lines will be ignored.

This solution is efficient and leverages the power of command-line tools to handle the task succinctly.