Data rotation and cleanup in Raspberry Pi - Time & Space Complexity
When we rotate and clean up data, we want to know how long it takes as the data grows.
We ask: how does the work increase when there is more data to handle?
Analyze the time complexity of the following code snippet.
# Assume logs is a list of log entries
max_logs = 100
if len(logs) > max_logs:
# Remove oldest logs to keep size
logs = logs[-max_logs:]
# Add new log entry
logs.append(new_log)
This code keeps only the newest 100 logs by removing older ones, then adds a new log.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Slicing the list to keep the last 100 logs.
- How many times: This happens once each time new data is added and the list is too long.
As the number of logs grows, the slicing operation always handles at most 100 items.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 (if over limit) |
| 100 | About 100 (at limit) |
| 1000 | Still about 100 (only last 100 kept) |
Pattern observation: The work stays roughly the same once the limit is reached, not growing with total data size.
Time Complexity: O(1)
This means the time to rotate and clean up data stays constant, no matter how big the total data is.
[X] Wrong: "Removing old logs takes longer as the total logs grow."
[OK] Correct: Because we only keep a fixed number of logs, the removal always handles the same small amount, so time does not grow with total logs.
Understanding how fixed-size data rotation keeps operations fast is a useful skill for managing data efficiently in real projects.
"What if we changed the max_logs limit to grow with input size? How would the time complexity change?"
Practice
Solution
Step 1: Understand data rotation and cleanup
Data rotation and cleanup help manage files by removing old or unnecessary data.Step 2: Identify the main goal
The goal is to keep storage organized and free space by deleting old files.Final Answer:
To keep storage organized and remove old files -> Option AQuick Check:
Data rotation = storage cleanup [OK]
- Confusing data cleanup with hardware upgrades
- Thinking it changes system performance directly
- Assuming it installs software automatically
Solution
Step 1: Understand the find command syntax
To delete files older than 7 days, use find with -type f (files), -mtime +7 (older than 7 days), and -delete.Step 2: Check each option
find /var/log -type f -mtime +7 -delete uses correct syntax. find /var/log -type d -mtime +7 -delete targets directories (-type d), not files. rm /var/log/* -mtime +7 uses rm incorrectly with -mtime. delete /var/log -older 7 is not a valid command.Final Answer:
find /var/log -type f -mtime +7 -delete -> Option BQuick Check:
find + -mtime +7 + -delete = correct [OK]
- Using -type d instead of -type f
- Trying to use rm with -mtime option
- Using invalid commands like delete
logfile="/var/log/syslog" backup="/var/log/syslog-$(date +%Y%m%d)" mv $logfile $backup > $logfile ls /var/log/syslog*
Solution
Step 1: Understand the script actions
The script renames syslog to syslog-YYYYMMDD (today's date), then creates a new empty syslog file.Step 2: Predict the ls output
After these steps, both /var/log/syslog (empty) and /var/log/syslog-YYYYMMDD exist, so ls lists both.Final Answer:
/var/log/syslog and /var/log/syslog-YYYYMMDD files listed -> Option CQuick Check:
mv + new empty file + ls = both files [OK]
- Thinking syslog file is deleted
- Assuming mv copies instead of moves
- Ignoring the empty file creation with >
logfile="/var/log/app.log" backup="/var/log/app.log-$(date +%Y%m%d)" mv $logfile $backup > $logfile find /var/log -type f -mtime 7 -delete
Solution
Step 1: Check the find command usage
The find command uses -mtime 7 which matches files exactly 7 days old, not older than 7 days.Step 2: Correct the find command
To delete files older than 7 days, use -mtime +7 instead of -mtime 7.Final Answer:
The find command should use -mtime +7 to delete files older than 7 days -> Option AQuick Check:
find -mtime +7 deletes older files [OK]
- Using -mtime 7 deletes only files exactly 7 days old
- Misplacing the > $logfile line
- Incorrect mv command syntax
/var/log/myapp.log is renamed with the date, a new empty log is created, and logs older than 14 days are deleted. Which script snippet correctly performs this task?Solution
Step 1: Rename current log and create new empty log
backup="/var/log/myapp.log-$(date +%Y%m%d)" mv /var/log/myapp.log $backup > /var/log/myapp.log find /var/log -type f -name 'myapp.log-*' -mtime +14 -delete correctly moves the log file to a dated backup and creates a new empty log with >.Step 2: Delete logs older than 14 days
backup="/var/log/myapp.log-$(date +%Y%m%d)" mv /var/log/myapp.log $backup > /var/log/myapp.log find /var/log -type f -name 'myapp.log-*' -mtime +14 -delete uses find with -name 'myapp.log-*' and -mtime +14 to delete backups older than 14 days, which is correct.Final Answer:
backup="/var/log/myapp.log-$(date +%Y%m%d)" mv /var/log/myapp.log $backup > /var/log/myapp.log find /var/log -type f -name 'myapp.log-*' -mtime +14 -delete -> Option DQuick Check:
mv + new file + find -mtime +14 = correct rotation [OK]
- Using rm instead of recreating empty log
- Using -mtime 14 instead of +14 in find
- Copying instead of moving log file
Ready to prove it?
Pass the test and this pattern moves unknown
