SQL for GPU Minutes Billed per Deployment From Replica Events
Last updated: November 29, 2026
Quick Overview
Given a table of replica lifecycle events (started, stopped) per deployment with GPU type and per-minute price, write SQL that reports billed GPU minutes and cost per deployment for one day, clipping replicas that span midnight.
Baseten
November 29, 20260
0
0 solved
Given a table of replica lifecycle events (started, stopped) per deployment with GPU type and per-minute price, write SQL that reports billed GPU minutes and cost per deployment for one day, clipping replicas that span midnight.
Dedicated deployments on Baseten are billed by the time replicas are up on a given GPU type, so usage reporting starts from replica start and stop events. The interviewer checks pairing events, clipping to a reporting window and handling replicas that are still running.
What the Interviewer Expects
- Pairs each start with its stop using the replica id, not row order
- Clips intervals to the reporting day on both ends
- Treats replicas with no stop event as running until the end of the window
- Joins prices by GPU type and multiplies by minutes
- Groups by deployment and orders the output meaningfully
Key Topics to Cover
How to Approach This
- Clarify the schema and expected output format before writing queries.
- Use CTEs (WITH clauses) to break complex queries into readable steps.
- Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
- Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
- For pandas, prefer vectorized operations over row-by-row iteration.
Possible Follow-up Questions
- Billing is per second with a one-minute minimum per replica start. How does the query change?
- How would you catch duplicate or out-of-order events from the controller?
- How would you build this as an incremental hourly job instead of a daily scan?
- How would you reconcile these numbers with the cloud provider's invoice?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice SQL ProblemsSample Answer
Clarify the Data
replica_events(replica_id, deployment_id, gpu_type, event_type, event_ts) with event_type in ('started','stopped'), one of each per replica at most. gpu_prices(gpu_type, price_per_minute). Repor...
Approach
Pivot events into one row per replica with started_at and stopped_at (null if still running). Keep replicas whose interval overlaps the window, clip with GREATEST and LEAST, convert to minutes, jo...