Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Optimize High-Latency Query
00:00
5 left

Optimize High-Latency Query

MediumSQL · PostgreSQL

Problem

How would you optimize a database query that is experiencing high latency during peak usage?

Using the provided schema and representative data, rewrite the query to report active endpoints with completed requests during the specified peak interval. Keep the result focused on endpoints that recorded at least one slow request.

Output

  1. One row per qualifying endpoint with endpoint_id, endpoint_name, request_count, avg_latency_ms, and slow_request_count.
  2. Include only active endpoints with completed requests in the interval and at least one request at or above 500 ms.
  3. Order by slow_request_count descending, avg_latency_ms descending, then endpoint_id ascending.

Schema

endpoints
ColumnTypeDescription
endpoint_idPKINTUnique endpoint identifier
endpoint_nameVARCHAR(100)Endpoint name
is_activeBOOLEANWhether the endpoint is currently active
query_events
ColumnTypeDescription
event_idPKINTUnique query event identifier
endpoint_idINTEndpoint associated with the event
occurred_atTIMESTAMPTimestamp when the request occurred
statusVARCHAR(20)Request completion status
latency_msINTRequest latency in milliseconds
Tablesendpointsquery_events
Interviewer

Your question is Optimize High-Latency Query. Start with the requirements and the two tables in the Question tab.

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.
CodePostgreSQL
Sign up free to run your codeLog inLn 1
Run your query to see results here.