KatInterview

Conditional Form Visibility with Numeric Checks and Nested Groups

Data Structure DesignAuto-graded · 19 test cases
MediumAuto-gradedData Structure Designparsingvalidationrecursion

Last updated

Decide which questions of a form are visible by parsing typed answers and evaluating validated, nestable all/any conditions with numeric comparisons.

Form Visibility Rules

You are given a form definition and the raw answers a user typed in. Return the IDs of the questions that should be shown, in form order.

Inputs

  • spec: a dictionary with a key "questions", an ordered list of question objects. Each question has:
    • id: a unique string.
    • type: either "text" or "number".
    • prompt: a string that you can ignore.
    • condition (optional): a rule that decides whether the question is shown.
  • answers: a dictionary mapping a question ID to the raw string the user entered. Some questions may have no entry. Entries whose ID matches no question are ignored.

Answer parsing

  • Text: the parsed answer is the raw string with leading and trailing whitespace removed.
  • Number: strip the raw string. The result must be non-empty, must be accepted by decimal.Decimal, and must be finite (reject NaN and infinities). The parsed answer is that Decimal. For example, "10000.01", " -3 " and "1e3" are valid, while "", "1,000" and "abc" are not.
  • Every answer present in answers for a question in the form is parsed, whether or not that question ends up visible. If any such answer is invalid (including a non-string raw value for a number question), raise ValueError.
  • A question's answer counts in conditions even if that question is itself hidden.

Conditions

A condition is either a leaf or a group.

Leaf: a dictionary with exactly the keys question_id, operator and value.

  • question_id must name a question that appears earlier in the list than the question carrying the condition.
  • If the referenced question is text, the operator must be equals or not_equals. The answer is compared with value as an exact, case-sensitive string comparison, with value used as given.
  • If the referenced question is number, the operator must be one of equals, not_equals, greater_than, greater_than_or_equal, less_than, less_than_or_equal. The value is converted with str(value).strip() and must be a valid finite Decimal under the rules above (so JSON numbers and numeric strings both work). Comparison is numeric, not lexical, and Decimal("5") == Decimal("5.0").
  • If the referenced question has no answer, the leaf is False, for every operator including not_equals.

Group: a dictionary with exactly one key, either "all" or "any", and no other keys. Its value must be a non-empty list of conditions (leaves or groups, nested to any depth). all is true when every child is true, and any is true when at least one child is true.

Validation

  • A question without a condition key, or with condition set to None, is always visible.
  • Every other condition is validated completely, including all nested children, before it is evaluated. Any violation of the rules above raises ValueError. This covers:
    • a condition that is not a dictionary;
    • a group with both all and any, or with extra keys;
    • an empty or non-list group;
    • a leaf with missing or extra keys;
    • a reference to an unknown or non-earlier question;
    • an operator not allowed for the referenced type;
    • an invalid numeric value.
  • Duplicate question IDs and question types other than text or number also raise ValueError.
  • If any error is raised, the function raises, and no partial list is returned.

Return the IDs of visible questions in their original order.

Constraints

  • 1 ≤ number of questions ≤ 1000.
  • Condition nesting depth is at most 50.
  • Numeric values are decimal strings or JSON numbers. Use exact decimal arithmetic, not binary floats.
  • Every question has id and type fields.

Examples

Example 1

Input: visible_question_ids({'questions': [{'id': 'request_type', 'type': 'text', 'prompt': 'What are you requesting?'}, {'id': 'justification', 'type': 'text', 'prompt': 'Why is it needed?', 'condition': {'question_id': 'request_type', 'operator': 'equals', 'value': 'software'}}]}, {'request_type': ' software '})

Output: ['request_type', 'justification']

Example 2

Input: visible_question_ids({'questions': [{'id': 'amount', 'type': 'number', 'prompt': 'Amount'}, {'id': 'approval', 'type': 'text', 'prompt': 'Approval required?', 'condition': {'question_id': 'amount', 'operator': 'greater_than', 'value': '10000'}}]}, {'amount': '10000.01'})

Output: ['amount', 'approval']

Example 3

Input: visible_question_ids({'questions': [{'id': 'request_type', 'type': 'text', 'prompt': 'Type'}, {'id': 'amount', 'type': 'number', 'prompt': 'Amount'}, {'id': 'approver', 'type': 'text', 'prompt': 'Approver', 'condition': {'any': [{'all': [{'question_id': 'request_type', 'operator': 'equals', 'value': 'software'}, {'question_id': 'amount', 'operator': 'greater_than', 'value': '10000'}]}, {'question_id': 'amount', 'operator': 'greater_than_or_equal', 'value': '50000'}]}}]}, {'request_type': 'software', 'amount': '12000'})

Output: ['request_type', 'amount', 'approver']

Hints

Hint 1

Parse all provided answers up front into typed values (stripped string or Decimal), raising ValueError on bad numeric input, and keep a lookup from question id to its type and position.

Hint 2

Write a recursive validator for conditions: groups check their key and non-empty list and recurse, leaves check keys, earlier-reference, and operator-versus-type rules, including that numeric values parse as finite Decimals.

Hint 3

Write a recursive evaluator mirroring the validator: all/any over children, and leaves that return False when the referenced answer is missing, otherwise compare with the right operator using Decimal for numbers.