IBM: Average VM Uptime Hours per Region (PySpark)

PYSPARK 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, with the PySpark DataFrame API (or Spark SQL).

Assign the result to df_result with the columns region and avg_uptime_hours, sorted by avg_uptime_hours descending, then region ascending, and finish with df_result.show().

Schema

DataFrame: vm_uptime

| Column | Type | Meaning |
| --- | --- | --- |
| vm_id | integer | The virtual machine |
| region | string | The VM's region |
| start_time | string | When the session started, `yyyy-MM-dd HH:mm:ss` |
| end_time | string | When it ended, `yyyy-MM-dd HH:mm:ss`; 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.5|
| Europe|             4.0|

+-------+----------------+

`

Explanation:

Constraints

What this PYSPARK challenge teaches you

“IBM: Average VM Uptime Hours per Region (PySpark)” is a medium-level PYSPARK challenge focused on Date Functions. Working through it gives you hands-on practice with unix_timestamp, to_timestamp, Date range, groupBy, 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. Convert with F.to_timestamp and filter start_time >= 2025-04-01 00:00:00 and < 2025-06-30 00:00:00.
  2. (F.unix_timestamp(end) - F.unix_timestamp(start)) / 3600.0 is the session's uptime in hours.
  3. Drop running sessions: F.col('end_time').isNotNull().
  4. groupBy('region').agg(F.round(F.avg(...), 2)) and orderBy(avg desc, 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 PySpark code 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 PYSPARK challenges

Helpful resources

Frequently asked questions

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

No. The PYSPARK 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