Skip to content
CurriculumLesson 32 ยท Files
Intermediate~16 min

Line-Sequential & Hashed Files

Line-sequential files handle variable-length text; hashed files turn a key into a bucket address. One is flexible, the other is fast for exact matches.

In plain English

Hashing is a locker number computed from the key, so you go straight to the right locker.

By the end you will be able to

  • Explain line-sequential records and delimiters.
  • Explain hashing, buckets, and collisions.
  • Analyze point lookup and range query costs.
  • Know when hashing is the wrong choice.
01

Line-sequential organization

A line-sequential file stores variable-length records, often lines of text. Each record needs a delimiter or a length prefix so the reader knows where it ends. Reading is sequential; finding a random line requires scanning from the beginning or using a separate index. Logs and configuration files commonly use this layout.

lesson-32.osOS
length-prefixed:
[5]hello[5]world

delimited:
hello\nworld\n
02

Hashed organization

A hash function maps a key to a bucket number. The bucket holds the record or a pointer to it. A point lookup computes the hash and reads one bucket, giving near-constant time when the distribution is good. Collisions happen when two keys map to the same bucket; they are handled by chaining or open addressing.

lesson-32.osOS
bucket = hash(key) % bucket_count
read bucket
if collision: follow overflow chain
03

The range-query problem

Hashing destroys key order. A query for all keys between 10 and 20 cannot jump to a range; it may have to scan many buckets. Hashed files are excellent for exact-match lookups and poor for range queries and ordered scans. An index or a tree is the better choice when order matters.

Key takeaways

  • Line-sequential files store variable-length records with delimiters or lengths.
  • Hashing maps a key to a bucket for fast point lookups.
  • Collisions require chaining or open addressing.
  • Hashing is weak for range queries and ordered scans.
  • Choose the organization based on the query pattern.

Common pitfalls

  • Forgetting to handle hash collisions.
  • Using hashing for range queries.
  • Assuming a line-sequential file supports fast random access without an index.

Kernel notes

  • Explain length-prefixed versus delimited records.
  • Compute a bucket index with a hash function.
  • State the main weakness of hashing.

Quick check

0 / 2 answered

1. What is the main weakness of a hashed file?

2. How does a line-sequential file know where a record ends?