FastPrepDepartment Employee Counts and Running Total

Department Employee Counts and Running Total

Straive logoStraive● MediumFULLTIMENEW GRADOA

Problem statement

For this exercise, assume every declared department must appear in the result, including departments with no employees.

Given Department(dep_id, dep_name) and Employee(emp_id, dep_id, emp_name, salary), compute one row per department with these columns:

  • dep_id and dep_name: that department's ID and name.
  • employee_count: the number of employee rows assigned to that department.
  • cumulative_count: the sum of employee counts for all departments whose IDs are less than or equal to this department's ID.

Return rows in ascending numeric dep_id order. Department and employee IDs are unique primary keys. Every employee references one declared department. Names may repeat and do not identify employees or departments; salary does not affect either count. With no departments, return an empty result with the same four columns.

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.

Department

ColumnTypeNullableDescription
dep_idPKIntegerNo—
dep_nameTextNo—

Employee

ColumnTypeNullableDescription
emp_idPKIntegerNo—
dep_idIntegerNo—
emp_nameTextNo—
salaryIntegerNo—

Foreign key: dep_id → Department(dep_id)

Expected result

Your query or function must return these columns.

ColumnTypeNullableDescription
dep_idIntegerNo—
dep_nameTextNo—
employee_countIntegerNo—
cumulative_countIntegerNo—

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

Constraints

  • For this practice contract, Department contains 0 to 200 rows and Employee contains 0 to 1500 rows.
  • All columns are non-null. IDs are integers between 1 and 1000000000. Names contain 1 to 100 characters. Salary is an integer between 0 and 1000000000.
  • Primary keys are unique; Employee.dep_id references Department.dep_id. Input rows may arrive in any order.
  • Return exact integer counts; do not remove distinct IDs because their names or salaries match.

More Straive problems

See Straive hiring insights