Neural Mastery
← Practice
Decompose a Compound Question Into Subquestionsmedium

Decompose a Compound Question Into Subquestions

medium RAG & Retrieval 15 min

NumPy allowed · plain Python earns 10 bonus points

RAG step 1/4

A query retrieves context

Embedding similarity ranks the documents before generation.
Open lesson →
01query
02embed
03retrieve
04context
What is RAG?→ embed →top context
retrieval.md44%
rag-systems.md33%
agents.md24%

Cosine-like similarity scores ranked before context assembly

Implement real query decomposition, splitting a compound multi-part question into independently-retrievable subquestions -- a real, standard technique for questions naive single-shot retrieval handles poorly.

Task

Implement decompose_question(question). Split on the literal string ' and ' (case-sensitive, surrounded by spaces). Strip whitespace from each part and drop any empty results. If there's no ' and ' in the question, return [question] unchanged (a single-part question).

Function Signature

decompose_question(question: str) -> list[str]

Examples

Example 1: two-part compound question
Input: question = 'what is the capital of France and what is its population'
Output: ['what is the capital of France', 'what is its population']
Example 2: single-part question unchanged
Input: question = 'what is the capital of France'
Output: ['what is the capital of France']
Example 3: three-part compound question
Input: question = 'who founded the company and when and where'
Output: ['who founded the company', 'when', 'where']

Constraints

  • question is a non-empty string
Python3Saved ✓
def decompose_question(question):
# Your implementation here
pass

Case 1: two-part compound question
Input: question = 'what is the capital of France and what is its population'
Expected: ['what is the capital of France', 'what is its population']
Case 2: single-part question unchanged
Input: question = 'what is the capital of France'
Expected: ['what is the capital of France']
Case 3: three-part compound question
Input: question = 'who founded the company and when and where'
Expected: ['who founded the company', 'when', 'where']