Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Key Alive in Time Range (SQL)

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

Your question is Key Alive in Time Range (SQL). Start with the requirements and the three 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

Confluent Cloud operations needs to determine which API keys were alive during a requested interval. An API key is considered alive during the half-open interval from an ACTIVE event until the next lifecycle event, including an open-ended interval when no later event exists.

Write a PostgreSQL query that returns every registered key with at least one active interval overlapping the requested range.

Requirements

  1. Use LEAD to determine when each lifecycle interval ends.
  2. Treat intervals as half-open, [start, end), so an activation exactly at the range end does not qualify.
  3. Return the overlapping portion of each active interval, including multiple qualifying intervals for one key.
  4. Order results by key_id and the beginning of the overlap.

Schema

api_keys
ColumnTypeDescription
key_idPKINTRegistered API key identifier
key_nameVARCHAR(100)Human-readable API key name
owner_teamVARCHAR(100)Owning Confluent team
key_events
ColumnTypeDescription
event_idPKINTLifecycle event identifier
key_idINTReferences api_keys.key_id
effective_atTIMESTAMPTZTime the resulting state became effective
state_afterVARCHAR(20)State after the event
event_noteTEXTOptional operational note
time_ranges
ColumnTypeDescription
range_idPKINTRequested range identifier
range_nameVARCHAR(100)Description of the requested range
range_startTIMESTAMPTZInclusive range start
range_endTIMESTAMPTZExclusive range end
Tablesapi_keyskey_eventstime_ranges
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results