Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
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 feedbackSuccess 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 Slow Client Query
00:00
5 left

Optimize Slow Client Query

MediumSQL · PostgreSQL

Problem

How would you optimize a database query that is performing slowly for a client with millions of records?

Using the provided PostgreSQL schema, return the latest completed attempt for each active candidate and active assessment since 2025-01-01. Include prior score information and recorded event metrics so the query can be evaluated for correctness and performance.

Output

  1. One row per candidate and assessment combination with a qualifying completed attempt.
  2. Columns: candidate_name, assessment_name, completed_at, score, previous_score, score_change, event_count, and total_duration_seconds.
  3. Include candidates with no matching events, treating their event count and duration as zero.
  4. Sort by completed_at descending, then candidate_name, then assessment_name.

Schema

candidates
ColumnTypeDescription
candidate_idPKINTUnique candidate identifier
candidate_nameVARCHAR(100)Candidate display name
statusVARCHAR(20)Candidate lifecycle status
assessments
ColumnTypeDescription
assessment_idPKINTUnique assessment identifier
assessment_nameVARCHAR(150)Assessment title
statusVARCHAR(20)Assessment lifecycle status
attempts
ColumnTypeDescription
attempt_idPKINTUnique attempt identifier
candidate_idINTCandidate who made the attempt
assessment_idINTAssessment being attempted
statusVARCHAR(20)Attempt status
completed_atTIMESTAMPCompletion timestamp
scoreINTAssessment score
attempt_events
ColumnTypeDescription
event_idPKINTUnique event identifier
attempt_idINTAttempt associated with the event
event_typeVARCHAR(30)Type of activity event
duration_secondsINTEvent duration in seconds
Tablescandidatesassessmentsattemptsattempt_events
Interviewer

Your question is Optimize Slow Client Query. Start with the requirements and the four 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.