IBM: Average VM Uptime Hours per Region

SQL coding challenge · Difficulty: medium · Topic: Date Functions · +100 XP

Problem

*IBM-style interview practice.*

Topic: Date Functions · timestamp differences · date-range filter · GROUP BY / AVG / ROUND

IBM's cloud operations team wants to compare regions: compute the average uptime in hours of the virtual-machine sessions in each region over the last 90 days.

Return region and avg_uptime_hours, highest average first (ties by region); the grader checks the rows, not their order.

Schema

Table: vm_uptime

| Column | Type | Meaning |
| --- | --- | --- |
| vm_id | INT | The virtual machine |
| region | VARCHAR | The VM's region |
| start_time | DATETIME | When the session started |
| end_time | DATETIME | When it ended; NULL while still running |

Example 1

Input: vm_uptime

| vm_id | region | start_time | end_time |
| --- | --- | --- | --- |
| 1 | US-East | 2025-06-01 08:00:00 | 2025-06-01 16:00:00 |
| 2 | US-East | 2025-06-10 09:00:00 | 2025-06-10 16:00:00 |
| 1 | US-East | 2025-05-20 22:00:00 | 2025-05-21 05:30:00 |
| 2 | US-East | 2025-03-15 06:00:00 | 2025-03-16 02:00:00 |
| 1 | US-East | 2025-06-29 10:00:00 | NULL |
| 3 | Europe | 2025-06-05 10:00:00 | 2025-06-05 12:30:00 |
| 4 | Europe | 2025-04-01 00:00:00 | 2025-04-01 05:30:00 |
| 4 | Europe | 2025-06-30 01:00:00 | 2025-06-30 03:00:00 |
| 5 | APAC | 2025-03-31 23:00:00 | 2025-04-01 02:00:00 |

Output:

| region | avg_uptime_hours |
| --- | --- |
| US-East | 7.50 |
| Europe | 4.00 |

Explanation:

Constraints

What this SQL challenge teaches you

“IBM: Average VM Uptime Hours per Region” is a medium-level SQL challenge focused on Date Functions. Working through it gives you hands-on practice with TIMESTAMPDIFF, Date range, AVG, ROUND, Cloud operations — the kind of transformation you are asked to write in real data engineering work and in technical interviews. You can solve it directly in the browser: the dataset is pre-loaded, so you write the query or DataFrame code, run it, and compare your output against the expected result immediately.

Concepts covered

How to approach it

If you get stuck, work through these steps in order before looking at a full solution — each one narrows the problem down:

  1. Keep the window by start_time: >= '2025-04-01 00:00:00' and < '2025-06-30 00:00:00'.
  2. TIMESTAMPDIFF(SECOND, start_time, end_time) / 3600.0 keeps minutes; TIMESTAMPDIFF(HOUR, ...) drops them.
  3. Exclude sessions that are still running: end_time IS NOT NULL.
  4. GROUP BY region with ROUND(AVG(...), 2), ORDER BY the average DESC, then region.

Interview practice

This problem is good practice for data engineering interviews at companies such as IBM. Interviewers use problems like it to check whether you can express the logic cleanly and reason about correctness on edge cases such as ties, nulls and empty groups.

How to practise it on PySpark.in

Open the challenge, write your SQL query in the editor and press Run to execute it against the sample dataset. Submitting checks your output against every test case, including hidden ones, so you find out straight away whether your logic holds up. You can retry as often as you like, and each solved challenge adds to your XP.

Related SQL challenges

Helpful resources

Frequently asked questions

Do I need to install Spark or a database to solve this?

No. The SQL environment runs in your browser with the sample data already loaded, so there is nothing to install or configure.

Is this challenge free?

Yes - the problem, the sample dataset, the hints and unlimited test runs are free.

What level is it?

It is rated medium and covers Date Functions.

Solve this challenge free on PySpark.in