FastPrepMost Recent Active User Per Department
Problem · Database

Most Recent Active User Per Department

EasyLenskart logoLenskartFULLTIMEOA

Problem statement

The table activity_logs stores user-activity records in input order. For each department, return the user from the row with the largest last_seen timestamp.

  • If several rows in one department share its largest timestamp, keep the row with the smallest log_position.
  • Order departments by the smallest log_position at which each department appears.

Return department and user_id. The reported exercise formats these ordered pairs as a semicolon-delimited string; the tabular workspace represents the same pairs as ordered result rows.

Input table

activity_logs
ColumnMeaning
log_positionOne-based position of the record in the input
departmentDepartment name
user_idUser identifier
last_seenActivity timestamp

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.

activity_logs

One row per activity record, with explicit input order.

ColumnTypeNullableDescription
log_positionPKIntegerNo
departmentTextNo
user_idTextNo
last_seenIntegerNo

Expected result

Your query or function must return these columns.

ColumnTypeNullableDescription
departmentTextNo
user_idTextNo

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

Constraints

  • The activity_logs table has between 1 and 10^5 rows.
  • log_position is unique, non-null, and records the input order.
  • department and user_id are non-null strings of length from 1 through 50.
  • 0 <= last_seen <= 10^9.

More Lenskart problems