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
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess 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
Get started
PySpark Transformations and Windows
00:00
5 left

PySpark Transformations and Windows

MediumSQL · PostgreSQL

Problem

Describe how you would implement a solution using PySpark DataFrame transformations and SQL window functions.

Use the provided account and activity data to demonstrate the equivalent relational result. Include activity records from January 2025 and return one row per qualifying activity.

Output

  1. Return account_id, account_name, record_id, record_date, amount, record_number, previous_amount, and cumulative_amount.
  2. Number records per account chronologically, show the prior amount, and calculate the cumulative amount.
  3. Order by account_id, record_date, and record_id ascending.

Output details

  • Treat January as dates from 2025-01-01 inclusive through 2025-02-01 exclusive.
  • Treat NULL amounts as 0 when calculating cumulative_amount.
  • Preserve NULL as previous_amount when no prior activity exists.
  • Retain activities with zero or negative amounts, including 0.
  • Return only activities associated with an existing account.

Schema

accounts
ColumnTypeDescription
account_idPKINTUnique account identifier
account_nameVARCHAR(100)Display name for the account
activity_records
ColumnTypeDescription
record_idPKINTUnique activity record identifier
account_idINTAccount associated with the activity
record_dateDATEDate on which the activity occurred
amountDECIMAL(10,2)Activity amount, which may be NULL
Tablesaccountsactivity_records
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements and the two tables are in the Question tab.

You need to log in / sign up to run or submit.
CodePostgreSQL
Sign up free to run your codeLog inLn 1
Run your query to see results here.