Home › Interview Questions › Need PySpark code to compute acceptance rate per d…

Need PySpark code to compute acceptance rate per date.

🟔 Medium Coding Junior level
1Times asked
Apr 2026Last seen
Apr 2026First seen

šŸ’” Model Answer

Here is a PySpark snippet that calculates the acceptance rate per date:

python
from pyspark.sql import functions as F

# Filter and aggregate sent actions
sent = df.filter(F.col("action") == "sent")
    .groupBy("date")
    .agg(F.count("*" ).alias("sent_cnt"))

# Filter and aggregate accepted actions
accepted = df.filter(F.col("action") == "accepted")
    .groupBy("date")
    .agg(F.count("*" ).alias("accepted_cnt"))

# Join aggregates and compute rate
rate_df = sent.join(accepted, "date", "inner")
    .withColumn("acceptance_rate", (F.col("accepted_cnt") / F.col("sent_cnt")) * 100)
    .select("date", "acceptance_rate")

rate_df.show()

This code performs two groupBy operations, joins the results on date, and calculates the acceptance percentage. The overall time complexity is linear in the number of rows, with an additional cost for the join proportional to the number of distinct dates.

This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.

šŸŽ¤ Get questions like this answered in real-time

Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers — invisible to screen sharing.

Get Assisting AI — Starts at ₹500