Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

LeetCode Hard SQL

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

Your question is LeetCode Hard SQL. Start with the requirements and the two 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

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
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results