How to filter solutions by difficulty, topics, and companies
Quest’s retriever supports metadata-based filtering to find solutions by difficulty level, topics, or companies. This is useful for targeted practice or interview preparation.
def filter_by_metadata( self, companies: List[str] = None, difficulty: str = None, topics: List[str] = None) -> List[Solution]: """Filter solutions based on metadata.""" filtered_solutions = self.solutions # Filter by companies if companies: filtered_solutions = [ sol for sol in filtered_solutions if any(company.lower() in sol.companies.lower() for company in companies) ] # Filter by difficulty if difficulty: filtered_solutions = [ sol for sol in filtered_solutions if sol.difficulty.lower() == difficulty.lower() ] # Filter by topics if topics: filtered_solutions = [ sol for sol in filtered_solutions if any(topic.lower() in sol.topics.lower() for topic in topics) ] return filtered_solutions
# All of these work the same:retriever.filter_by_metadata(difficulty="Easy")retriever.filter_by_metadata(difficulty="easy")retriever.filter_by_metadata(difficulty="EASY")
Implementation:
retriever2.py
if difficulty: filtered_solutions = [ sol for sol in filtered_solutions if sol.difficulty.lower() == difficulty.lower() ]
Start with Easy problems to build confidence, then progress to Medium and Hard.
retriever = LeetCodeRetriever()# Problems involving EITHER DFS OR BFStree_traversal = retriever.filter_by_metadata(topics=["DFS", "BFS"])for sol in tree_traversal[:5]: print(f"{sol.title}") print(f"Topics: {sol.topics}\n")
How it works:
retriever2.py
if topics: filtered_solutions = [ sol for sol in filtered_solutions if any(topic.lower() in sol.topics.lower() for topic in topics) ]
The any() function means a solution matches if it contains at least one of the specified topics.
When filtering by multiple topics, you get problems that have ANY of those topics, not ALL of them.
retriever = LeetCodeRetriever()# Problems asked by Amazon OR Googlefaang_sols = retriever.filter_by_metadata(companies=["Amazon", "Google"])for sol in faang_sols[:10]: print(f"{sol.title} - {sol.difficulty}") print(f"Companies: {sol.companies}\n")
def filter_by_metadata(self, companies=None, difficulty=None, topics=None): filtered_solutions = self.solutions # Start with all solutions # Apply company filter (if specified) if companies: filtered_solutions = [sol for sol in filtered_solutions ...] # Apply difficulty filter to remaining solutions if difficulty: filtered_solutions = [sol for sol in filtered_solutions ...] # Apply topic filter to remaining solutions if topics: filtered_solutions = [sol for sol in filtered_solutions ...] return filtered_solutions
# Identify weak area (e.g., Graph algorithms)# Start with easier problemsgraph_practice = retriever.filter_by_metadata( topics=["Graph", "DFS", "BFS"], difficulty="Easy")print(f"Found {len(graph_practice)} graph problems to practice")# Work through them systematicallyfor idx, sol in enumerate(graph_practice, 1): print(f"{idx}. {sol.title}") print(f" Topics: {sol.topics}\n")
import random# Get medium problems from multiple companiespractice_set = retriever.filter_by_metadata( companies=["Amazon", "Google", "Microsoft", "Facebook"], difficulty="Medium")# Randomly sample 20 problemsrandom_practice = random.sample(practice_set, min(20, len(practice_set)))print("Today's practice set (20 random medium problems):")for idx, sol in enumerate(random_practice, 1): print(f"{idx}. {sol.title}") print(f" Company: {sol.companies.split(',')[0]}") print(f" Topics: {sol.topics}\n")
from src.DSAAssistant.components.retriever2 import LeetCodeRetrieverfrom rag_engine3 import RAGEngine# Initializeretriever = LeetCodeRetriever()rag_engine = RAGEngine(retriever)# Get filtered solutionsamazon_dp = retriever.filter_by_metadata( companies=["Amazon"], topics=["Dynamic Programming"])# Query about themfor sol in amazon_dp[:5]: print(f"\n{'='*60}") print(f"Problem: {sol.title}") print(f"{'='*60}") # Ask for explanation response = rag_engine.answer_question( f"Explain the {sol.title} problem and provide the optimal solution" ) print(response)
Limitation: The filter_by_metadata() method returns a list, not a searchable index.Workaround: If you need semantic search on filtered results, create a custom index:
# Filter firstfiltered = retriever.filter_by_metadata(difficulty="Medium")# Then search manuallyfor sol in filtered: if "binary search" in sol.title.lower(): print(sol.title)
Limitation: Multiple topics use OR logic (any match), not AND logic (all match).Workaround: Filter in two steps:
# Want problems with BOTH Array AND Hash Tablearray_problems = retriever.filter_by_metadata(topics=["Array"])# Manually filter for Hash Tableboth_topics = [ sol for sol in array_problems if "hash table" in sol.topics.lower()]
Metadata filtering is exact string matching. Make sure to use the exact topic/company names as stored in the database.