Evaluate Request Status from Rule Matches
Arrays & HashingAuto-graded · 21 test casesLast updated
Tests rule-based decision logic with subset matching and outcome priority resolution when multiple rules apply simultaneously.
You are building a system that determines the status of incoming requests by evaluating them against a collection of decision rules.
Each rule consists of:
- A set of required criteria (strings)
- An outcome status: one of
"APPROVED","REJECTED", or"PENDING"
Given a list of rules and the criteria present on a particular request, determine the final status:
- A rule applies when all of its required criteria appear in the request's criteria list
- Multiple rules may apply to the same request
- When multiple rules apply, the final status follows this priority order:
"REJECTED"(highest priority)"APPROVED"(medium priority)"PENDING"(lowest priority)
- If no rules apply, the default status is
"PENDING"
Write a function that takes a list of rules and a list of request criteria, then returns the final status string.
Input Format:
rules: A list of dictionaries, each containing:"conditions": A list of strings (the required criteria)"outcome": A string ("APPROVED","REJECTED", or"PENDING")
request_criteria: A list of strings representing the criteria present on this request
Output Format:
- Return a single string:
"APPROVED","REJECTED", or"PENDING"
Constraints
- Number of rules: 0 ≤ len(rules) ≤ 1000
- Number of request criteria: 0 ≤ len(request_criteria) ≤ 50
- Each criterion string is non-empty
- Request criteria may contain duplicates
- Rule conditions may be provided as lists, sets, or tuples
- Only the three specified outcome values will appear
Examples
Example 1
Input: evaluate_request_status([{'conditions': ['zoning'], 'outcome': 'APPROVED'}, {'conditions': ['violation'], 'outcome': 'REJECTED'}], ['zoning', 'violation'])
Output: 'REJECTED'
Example 2
Input: evaluate_request_status([{'conditions': ['paid'], 'outcome': 'APPROVED'}], ['submitted'])
Output: 'PENDING'
Example 3
Input: evaluate_request_status([{'conditions': ['submitted'], 'outcome': 'PENDING'}, {'conditions': ['submitted', 'paid'], 'outcome': 'APPROVED'}], ['submitted', 'paid'])
Output: 'APPROVED'
Hints
Hint 1
Convert the request criteria to a set for efficient membership checking
Hint 2
Use Python's set.issubset() to test whether all required criteria are present
Hint 3
Map each outcome to a numeric priority and track the maximum priority seen
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The problem requires two operations: identifying which rules match the request, then selecting the highest-priority outcome among matches.
Matching Logic:
A rule matches when every criterion it requires is present in the request. This is a subset relationship. Convert both the request criteria and each rule's conditions to sets, then use issubset() to test the relationship efficiently.
Priority Resolution:
Multiple rules may match simultaneously. Assign each outcome an integer priority (PENDING: 0, APPROVED: 1, REJECTED: 2) and track the maximum priority encountered. Start with PENDING at priority 0 as the baseline—this handles the "no rules match" case automatically without special logic.
Algorithm Steps:
- Convert
request_criteriato a set for O(1) membership tests - Initialize tracking variables:
final_status = "PENDING"andhighest_priority = 0 - For each rule:
- Convert its conditions to a set
- Test whether the conditions are a subset of the request criteria
- If matched, check if this outcome's priority exceeds the current highest
- If so, update both the status and priority trackers
- Return the final status
Why This Works:
The subset test correctly implements "all required criteria present." The linear scan with running maximum finds the highest-priority outcome, which the problem defines as the correct result. When no rules match, the loop never updates our trackers, leaving the initial PENDING value intact.
Edge Cases:
- Empty rules list: returns
PENDING - Empty request criteria: only rules with empty conditions can match
- Rules with empty conditions: match every request (empty set is subset of all sets)
- Duplicate criteria in request: absorbed during set conversion
Complexity
-
Time: O(C + R × M) where C is the number of request criteria, R is the number of rules, and M is the average number of conditions per rule. Set construction from the request is O(C), and each rule requires O(M) to build its condition set and test subset membership.
-
Space: O(C + M) for the request criteria set plus one temporary rule condition set at a time.
With the given constraints (R ≤ 1000, C ≤ 50), this runs efficiently.
Solution Code
from typing import Dict, List
def evaluate_request_status(rules: List[Dict[str, object]], request_criteria: List[str]) -> str:
criteria_set = set(request_criteria)
priority_map = {"PENDING": 0, "APPROVED": 1, "REJECTED": 2}
final_status = "PENDING"
highest_priority = 0
for rule in rules:
required = set(rule["conditions"])
if required.issubset(criteria_set):
outcome = rule["outcome"]
priority = priority_map[outcome]
if priority > highest_priority:
highest_priority = priority
final_status = outcome
return final_status