FastPrepConditional Group Aggregation

Conditional Group Aggregation

Stripe logoStripeNEW GRADOA

Problem statement

Group rows, compute conditional counts and aggregate values, then apply filters to the grouped result.

FastPrep practice interpretation

For this exercise, assume the supplied samples table contains row_id, cohort, value and quality. The schema and rules below are authored practice assumptions.

  1. Keep only rows whose value >= 0 and whose quality is exactly ok or bad.
  2. Group the retained rows by cohort. Count all retained rows as row_count, count only retained ok rows as ok_count, and sum retained value as total_value. Identical values in different rows count separately.
  3. Return only cohorts with row_count >= 3, ok_count >= 2 and total_value >= 100.
  4. Return columns cohort, row_count, ok_count, total_value in that order, with rows sorted by cohort in ascending order. If no cohort qualifies, return these columns and no rows.

Table schema

MySQLPostgreSQLPandas

Use the same input data with any supported language. Open the Schema tab in the editor to see the generated SQL setup or Pandas DataFrames.

samples

ColumnTypeNullableDescription
row_idPKIntegerNo—
cohortTextNo—
valueIntegerNo—
qualityTextNo—

Expected result

Your query or function must return these columns.

ColumnTypeNullableDescription
cohortTextNo—
row_countIntegerNo—
ok_countIntegerNo—
total_valueIntegerNo—

Row order: must match exactly. Numeric tolerance: 0.

Constraints

  • There are 0 to 2,000 input rows. All fields are non-null, and row_id values are unique.
  • Each value is an integer between -1,000 and 1,000. Cohort values contain only lowercase English letters or only uppercase English letters per case; quality is case-sensitive text.
  • The input may contain groups that have no qualifying rows. Return one row per qualifying cohort.

More Stripe problems

See Stripe hiring insights