CSV library basics in Ruby - Time & Space Complexity
When working with CSV files in Ruby, it's important to understand how the time to process data grows as the file gets bigger.
We want to know how the program's work changes when the number of rows in the CSV increases.
Analyze the time complexity of the following code snippet.
require 'csv'
CSV.foreach('data.csv') do |row|
puts row[0]
end
This code reads a CSV file line by line and prints the first value of each row.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Reading each row from the CSV file one by one.
- How many times: Once for every row in the CSV file.
As the number of rows grows, the program reads and processes more lines.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 reads and prints |
| 100 | 100 reads and prints |
| 1000 | 1000 reads and prints |
Pattern observation: The work grows directly with the number of rows; doubling rows doubles work.
Time Complexity: O(n)
This means the time to read and process the CSV grows in a straight line with the number of rows.
[X] Wrong: "Reading a CSV file is always instant no matter the size."
[OK] Correct: The program must read each row one by one, so bigger files take more time.
Understanding how file reading scales helps you write efficient data processing code and answer questions about handling large files.
"What if we used CSV.read to load all rows at once instead of CSV.foreach? How would the time complexity change?"
Practice
CSV.foreach do?Solution
Step 1: Understand CSV.foreach purpose
The methodCSV.foreachis designed to read CSV files line by line.Step 2: Identify the output format
Each line is yielded as an array of strings representing the cells in that row.Final Answer:
Reads a CSV file line by line, yielding each row as an array. -> Option AQuick Check:
CSV.foreach reads lines as arrays [OK]
- Confusing reading with writing methods
- Thinking it returns a hash instead of an array
- Assuming it deletes files
Solution
Step 1: Identify mode for writing
To write to a CSV file, the mode should be 'w' (write mode).Step 2: Match syntax with CSV.open
The correct syntax isCSV.open('file.csv', 'w') do |csv| ... endto open for writing.Final Answer:
CSV.open('file.csv', 'w') do |csv| ... end -> Option DQuick Check:
Open with 'w' writes CSV [OK]
- Using 'r' mode which is for reading
- Using CSV.foreach for writing
- Using CSV.read with mode argument
require 'csv'
CSV.foreach('data.csv') do |row|
puts row.inspect
endAssuming
data.csv contains:name,age Alice,30 Bob,25
Solution
Step 1: Understand CSV.foreach behavior
It reads each line and yields an array of strings split by commas.Step 2: Analyze puts row.inspect output
Usingputs row.inspectprints the array representation of each row.Final Answer:
["name", "age"]\n["Alice", "30"]\n["Bob", "25"] -> Option BQuick Check:
CSV.foreach yields arrays, inspect shows arrays [OK]
- Expecting raw CSV string output
- Confusing puts with print
- Missing block for CSV.foreach
require 'csv'
CSV.open('output.csv', 'w') do |csv|
csv << 'name,age'
csv << ['Alice', 30]
endSolution
Step 1: Check CSV.open usage for writing rows
CSV expects each row as an array of fields, not a single string.Step 2: Identify the incorrect row format
The linecsv << 'name,age'should becsv << ['name', 'age']to write two columns.Final Answer:
The first csv << should be an array, not a string. -> Option AQuick Check:
CSV rows must be arrays [OK]
- Passing a string instead of array for a row
- Forgetting to require 'csv'
- Using wrong file mode
Solution
Step 1: Understand CSV.foreach usage for reading
CSV.foreach reads each row as an array, allowing manual hash construction.Step 2: Build hash from first and second columns
Assigninghash[row[0]] = row[1]correctly maps keys to values.Final Answer:
hash = {} CSV.foreach('file.csv') do |row| hash[row[0]] = row[1] end -> Option CQuick Check:
foreach + manual hash build = correct [OK]
- Using CSV.read.to_h which doesn't convert arrays to hash
- Using CSV.open with 'w' mode for reading
- Expecting row.to_h to work directly
Ready to prove it?
Pass the test and this pattern moves unknown
