Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Unit Test Spark Transformation Output
00:00
5 left

Unit Test Spark Transformation Output

MediumPython

Problem

In Databricks, unit-testing a Spark transformation often means verifying that an output collection of rows matches the expected result even when row order is not guaranteed. Implement a Python function that compares two datasets and reports whether they are equivalent after applying deterministic normalization rules.

Formal Specification

Write a function assert_spark_rows_equal(actual_rows, expected_rows, key_columns).

  • actual_rows: list of dictionaries representing transformed output rows
  • expected_rows: list of dictionaries representing expected rows
  • key_columns: list of column names used to sort rows deterministically before comparison

The function should return True if:

  1. Both datasets contain the same number of rows
  2. Every row has the same set of columns after normalization
  3. Rows are equal after sorting by key_columns

Normalization rules:

  • Missing keys should be treated as None
  • String values should be compared exactly
  • Numeric and boolean values should be compared exactly
  • Row order in the input lists should not affect the result

Constraints

  • 0 <= len(actual_rows), len(expected_rows) <= 10^4
  • Each row contains at most 50 columns
  • 0 <= len(key_columns) <= 10
  • Missing keys should be treated as None
  • Input row order should not affect the result

Function Signature

def assert_spark_rows_equal(actual_rows, expected_rows, key_columns):
Interviewer

Your question is Unit Test Spark Transformation Output. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.