Bird
0
0

When executing head -n 5 file.txt, what is the expected time complexity relative to the file size?

medium📝 Command Output Q5 of 15
Linux CLI - Viewing and Editing Files
When executing head -n 5 file.txt, what is the expected time complexity relative to the file size?
ATime depends on reading the entire file, linear with file size
BTime depends only on reading the first 5 lines, roughly constant
CTime depends on the number of characters in the file, quadratic
DTime depends on the number of lines after line 5
Step-by-Step Solution
Solution:
  1. Step 1: Understand head behavior

    head -n 5 reads only the first 5 lines, ignoring the rest.
  2. Step 2: Time complexity

    Since it reads a fixed number of lines, time is roughly constant regardless of file size.
  3. Final Answer:

    Time depends only on reading the first 5 lines, roughly constant -> Option B
  4. Quick Check:

    head reads fixed lines [OK]
Quick Trick: Fixed line count means constant time [OK]
Common Mistakes:
MISTAKES
  • Assuming entire file is read
  • Confusing line count with file size
  • Thinking time grows with file length

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Linux CLI Quizzes