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
SQL Concurrency and Locks
00:00
5 left

SQL Concurrency and Locks

HardSQL · PostgreSQL

Problem

They asked me about concurrency/lock problems, how I would work with those requirements.

Using the provided lock request and lock compatibility data, write a SQL query to identify currently waiting requests blocked by granted requests on the same relation. Exclude requests from the same transaction.

Output

  1. One row per waiting and blocking request pair
  2. Columns: waiting_request_id, blocking_request_id, relation_name, waiting_mode, and blocking_mode
  3. Include only incompatible lock pairs, ordered by waiting_request_id, then blocking_request_id

Schema

lock_requests
ColumnTypeDescription
request_idPKINTUnique lock request identifier
transaction_idINTTransaction that owns or requests the lock
relation_nameVARCHAR(100)Database relation being locked
requested_modeVARCHAR(50)Requested PostgreSQL lock mode
grantedBOOLEANWhether the requested lock has been granted
requested_atTIMESTAMPTime at which the lock was requested
lock_mode_compatibility
ColumnTypeDescription
mode_aPKVARCHAR(50)First mode in canonical lexical order
mode_bPKVARCHAR(50)Second mode in canonical lexical order
is_compatibleBOOLEANWhether the two lock modes can coexist
Tableslock_requestslock_mode_compatibility
Interviewer

Your question is SQL Concurrency and Locks. 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.