Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL: Timed Out Activity IDs

MediumSQL · PostgreSQL00:00
Practice interviewer
In session
5 left
00:00

Your question is SQL: Timed Out Activity IDs. Start with the requirements and the two tables on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Google Cloud Workflows records each execution of an activity, including retries. Write a PostgreSQL query that returns the IDs of activities whose latest execution has timed out as of 2025-02-01 12:00:00.

An execution is timed out when it is the latest execution for its activity, its status is running or timed_out, its completed_at is NULL, and the activity deadline is at or before the evaluation timestamp.

Requirements

  1. Use a CTE and ROW_NUMBER() to identify the latest execution for each activity by started_at.
  2. Join the latest executions to activities and apply the timeout conditions.
  3. Return only activity_id, ordered ascending.
  4. Exclude activities with no execution, completed executions, future deadlines, failed executions, and executions with a NULL deadline.

Schema

activities
ColumnTypeDescription
activity_idPKINTEGERUnique activity identifier
activity_nameVARCHAR(100)Human-readable activity name
timeout_atTIMESTAMPDeadline for the activity execution
activity_executions
ColumnTypeDescription
execution_idPKINTEGERUnique execution identifier
activity_idINTEGERReferenced activity
statusVARCHAR(20)Execution state
started_atTIMESTAMPExecution start timestamp
completed_atTIMESTAMPCompletion timestamp, if completed
Tablesactivitiesactivity_executions
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results