Write a quarterly SQL report and a bash max-file script
OthersSoftware Engineer · Online AssessmentLast updated
This problem tests your ability to perform conditional aggregation in SQL for time-series reporting and your proficiency in recursive shell scripting for system administration tasks.
Part 1: SQL Reporting
Assume a table sales with columns sale_id (int), amount (decimal), and sale_date (datetime). Write a SQL query to generate a report showing the total sales amount for each quarter of the year 2023. The output should include columns: quarter (1-4) and total_revenue.
Example Output:
| quarter | total_revenue |
|---|---|
| 1 | 5000.00 |
| 2 | 7200.50 |
Part 2: Bash Scripting
Write a bash script that takes a directory path as an argument. The script should recursively traverse the directory and output the name and size of the single largest file found. If the directory is empty or does not exist, provide a descriptive error message to stderr and exit with a non-zero status code.
Constraints:
- For SQL, assume standard PostgreSQL or MySQL syntax.
- For Bash, use standard utilities like
find,du, orsort. - Ensure the bash script handles filenames with spaces correctly.
Hints
Hint 1
For the SQL part, use the `QUARTER()` function or `EXTRACT(QUARTER FROM ...)` combined with a `GROUP BY` clause.
Hint 2
For the bash script, use `find . -type f -exec du -h {} +` to list files and their sizes, then pipe the output to `sort -rh` to find the largest entry.
Hint 3
Remember to quote variables in bash (e.g., "$1") to handle paths or filenames that contain whitespace.
Follow-up questions
- How would you modify the SQL query to include quarters with zero sales?
- How could the bash script be optimized to handle directories with millions of files where `find` might consume too much memory?
- How would you extend the bash script to exclude specific file extensions, such as '.tmp' files, from the search?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.