KatInterview

Quarterly Revenue Query and Largest-File Shell Script

SQL & DataModel answer
MediumModel answerSQL & Datasqlaggregationgroup-by

Last updated

Write a SQL query that totals 2023 sales by quarter, and a robust bash script that finds the largest file under a directory, handling odd filenames and error cases.

This exercise has two small tasks that cover everyday engineering tooling.

Part 1: Revenue per quarter (SQL)

You have a table sales with these columns:

  • sale_id (integer)
  • amount (decimal)
  • sale_date (datetime)

Write a query that returns the total revenue for each calendar quarter of 2023. The result must have two columns:

  • quarter: a number from 1 to 4
  • total_revenue: the sum of amount for that quarter

Example of the expected shape:

quartertotal_revenue
15000.00
27200.50

Use standard PostgreSQL or MySQL syntax. Explain which dialect you chose.

Part 2: Find the biggest file (Bash)

Write a bash script that accepts one argument, a directory path. It must:

  • Search the directory and all its subdirectories.
  • Print the name (path) and size of the single largest file.
  • If the directory does not exist, or contains no files, print a clear error message to stderr and exit with a non-zero status.

Requirements:

  • You may use utilities such as find, du or sort.
  • Filenames containing spaces must work correctly.

Be ready to discuss how your solution behaves with very large inputs, ties, and unusual filenames.

Hints

Hint 1

For the SQL part, filter the year with a half-open range (`>= '2023-01-01' AND < '2024-01-01'`) rather than wrapping the column in a function or using BETWEEN with a date-only upper bound. Think about which one keeps indexes usable and does not lose late-December timestamps.

Hint 2

Derive the quarter with EXTRACT(QUARTER FROM ...) in PostgreSQL or QUARTER(...) in MySQL, then GROUP BY it and ORDER BY it so the output order is deterministic.

Hint 3

For the shell part, `du -h | sort -rh` is approximate and fragile. Let `find -type f -printf '%s\t%p\0'` emit exact byte sizes with NUL-terminated records, and keep a running maximum in a `while read -d ''` loop fed by process substitution.

Hint 4

Validate the argument first: wrong argument count, missing path, not a directory, unreadable. After the loop, treat 'no file seen' as the empty-directory error. Send all errors to stderr with `>&2`.