Count Individual Tags

PYSPARK coding challenge · Difficulty: medium · Topic: String Processing · +100 XP

Tags stores several numeric tags for a record inside a single string,

separated by #. The same tag can appear more than once in one row.

Tables

| column | type | description |
| --- | --- | --- |
| id | INT | identifies the record |
| tag | VARCHAR | numeric tags joined by `#` |

Split each string into its individual tags and count how many times each tag

occurs for each id. Return one row per unique id + tag pair with:

The string may begin or end with #, and two separators may sit next to each

other. Those produce empty pieces, which are not tags — ignore them.

Counting is per id: the same tag under two different ids is two separate rows.

Sort by id ascending, then by tag in ascending numeric order — so 90

comes before 100, which comes before 1000.

Example input

Tags

| id | tag |
| --- | --- |
| 1 | #200#100#100#300 |
| 2 | #200#200#300#400 |

Expected output

| id | tag | count |
| --- | --- | --- |
| 1 | 100 | 2 |
| 1 | 200 | 1 |
| 1 | 300 | 1 |
| 2 | 200 | 2 |
| 2 | 300 | 1 |
| 2 | 400 | 1 |

Row 1 splits into 200, 100, 100, 300, so 100 occurs twice and 200 and

300 once each. Row 2 splits into 200, 200, 300, 400.

What this PYSPARK challenge teaches you

“Count Individual Tags” is a medium-level PYSPARK challenge focused on String Processing. Working through it gives you hands-on practice with string-processing, explode, aggregation — 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. MySQL has no UNNEST. Rewrite the delimited string as a JSON array -- '#200#100' becomes '["200","100"]' -- and JSON_TABLE will give you one row per element.
  2. In PySpark this is split() to make an array, then explode() to turn each element into its own row.
  3. A leading '#' produces an empty first piece. Filter those out before grouping, or they become a bogus tag. And ORDER BY on a string sorts '1000' before '90' -- cast to a number.

Where this comes up

Variations of this problem have been reported in interviews at Adobe. Interviewers use 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 String Processing.

Solve this challenge free on PySpark.in