Cast Column Types

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

Cast string user_id to int and string amount to double. Treat this as a production helper: match the contracted return shape, including empty and duplicate inputs.

Both columns in this table were read as text, like '10' and '12.5'. To work with them as numbers, you need to change their type. This is called casting. From the table df, cast user_id to an integer and amount to a double (a decimal number). Sort by user_id. Both columns must be cast. Save your final table in result.

Requirements

  • Cast user_id to an integer.
  • Cast amount to a double (decimal number).
  • Both columns must be cast, not just one of them.
  • Sort by user_id.

Constraints

  • The input table is df. Both columns are text.
  • Use .cast('int') and .cast('double').
  • Sort after casting, so the ids sort as numbers.

Examples

Input: string user_id/amount Output: user_id | amount 10 | 12.5 20 | 7.0 30 | 100.25 40 | 0.5 Casts make arithmetic and numeric filters safe.

Topics: lakebench, pyspark, cast.

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

beginner

Cast Column Types

Interview-style drill: Cast string user_id to int and string amount to double.

From `df`, cast `user_id` to int and `amount` to double. Order by `user_id`. Assign `result`.