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
LeetCode Hard SQL
00:00
5 left

LeetCode Hard SQL

HardSQL · PostgreSQL

Problem

KPIT's engineering knowledge search surface stores indexed text and a required set of equal-length keywords. Write a PostgreSQL query that finds every zero-based character position where the text contains all required keywords exactly once, in any order and including duplicate keyword requirements.

Requirements

  1. Return case_id and the zero-based start_position for every valid match.
  2. Treat the required keywords as a multiset, so duplicate words must occur the required number of times.
  3. Require the matching substring to contain exactly word_count * word_length characters.
  4. Return results ordered by case_id and start_position.
  5. Ignore cases with no usable keyword rows or a NULL search text.

All keywords within a case have the same length.

Schema

search_cases
ColumnTypeDescription
case_idPKINTSearch case identifier
search_textTEXTText scanned for concatenated keywords
case_labelVARCHAR(80)Short description of the search case
required_words
ColumnTypeDescription
word_idPKINTRequired-word identifier
case_idINTReferences search_cases.case_id
word_positionINTPosition in the input requirement list
word_textVARCHAR(40)Keyword that must occur in the candidate substring
Tablessearch_casesrequired_words
Interviewer

Your question is LeetCode Hard SQL. 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.