Explode Array Column

PySpark data engineering interview problem. Difficulty: beginner. Pattern: Filtering. About 12 minutes. Free to practice.

Explode tags arrays into 1 row per tag. Treat this as a production helper: match the contracted return shape, including empty and duplicate inputs.

In this table each user has a list of tags in a single column, like ['sql', 'spark']. Many tools prefer one tag per row. Turning a list into several rows is called exploding. From df, explode the tags column into a new column called tag, so every tag gets its own row. Return the columns user_id and tag, sorted by user_id and then by tag. Save your final table in result.

Requirements

  • Create 1 row for each tag of each user.
  • Call the new column tag.
  • Return only user_id and tag.
  • Sort by user_id and then by tag.

Constraints

  • The input table is df with the columns user_id and tags. tags holds a list of text values.
  • Use F.explode on the tags column.
  • The result must not contain the original tags list column.

Examples

Input: user_id | tags Output: user_id | tag 1 | spark 1 | sql 2 | pandas 3 | flink 3 | kafka 3 | spark 4 | dbt Array grain becomes tag grain; other columns repeat.

Topics: lakebench, pyspark, explode.

More PySpark interview questions · All interview problems · Learn data engineering

beginner

Explode Array Column

Interview-style drill: Explode tags arrays into 1 row per tag.

From `df`, explode `tags` into a `tag` column. Return `user_id`, `tag` ordered by both. Assign `result`.