KatInterview

Bracket Matching, Interval Union, and Expression Parsing

Stacks & QueuesAuto-graded · 22 test cases
MediumAuto-gradedStacks & Queuesstacksintervalsparsing

Last updated

Tests stack-based bracket validation, interval merging with endpoint semantics, and arithmetic evaluation with unary operators.

You will implement a single function that solves three independent subproblems and returns their results in a dictionary.

Subproblem 1: Bracket Validation

Given a string s containing only the characters (, ), [, ], {, }, determine whether the brackets are properly matched and nested.

  • Return True if every opening bracket has a corresponding closing bracket of the same type in the correct order.
  • Return False otherwise.
  • An empty string is considered valid.

Subproblem 2: Interval Union

Given a list of intervals where each interval is represented as [start, end, left_closed, right_closed]:

  • start and end are integers with start <= end
  • left_closed and right_closed are booleans indicating whether the interval includes its left and right endpoints

Compute the union of all intervals and return a minimal sorted list of non-overlapping intervals in the same format.

Merging rules:

  • Two intervals should merge if they overlap or touch
  • Intervals with prev_end > cur_start always merge
  • Intervals with prev_end == cur_start merge if and only if at least one endpoint at that position is closed (either prev is right-closed OR cur is left-closed)
  • An interval [a, a, False, False], [a, a, True, False], or [a, a, False, True] represents an empty set and should be removed
  • An interval [a, a, True, True] represents a single point and should be kept

Subproblem 3: Arithmetic Expression Evaluation

Given a string expr containing:

  • Non-negative integers
  • Binary operators: +, -, *, /
  • Parentheses: (, )
  • Unary minus (e.g., -3, 1+-2, 2*(-3+4))
  • Arbitrary whitespace

Compute and return the integer result of the expression.

Rules:

  • Division truncates toward zero (e.g., 7 / -2 equals -3)
  • A minus sign is unary when it appears at the start, after an operator, or after an opening parenthesis
  • You may assume the expression is valid and all intermediate results fit in a 32-bit signed integer

Return Value

Return a dictionary with three keys:

{
    'valid_brackets': bool,
    'merged_intervals': List[List],  # each inner list is [start, end, left_closed, right_closed]
    'expression_result': int
}

Constraints

  • 0 <= len(s) <= 10^4
  • 0 <= len(intervals) <= 10^4
  • -10^9 <= start, end <= 10^9 for each interval
  • 0 <= len(expr) <= 10^4
  • Expression contains only valid characters as described
  • All three subproblems should be solved efficiently:
    • Bracket validation: O(n) time, O(n) space
    • Interval merging: O(n log n) time, O(n) space
    • Expression evaluation: O(n) time, O(n) space

Examples

Example 1

Input: process_three_tasks('([]{})', [[1, 3, True, True], [3, 5, True, False]], '2*(-3+4)')

Output: {'valid_brackets': True, 'merged_intervals': [[1, 5, True, False]], 'expression_result': 2}

Example 2

Input: process_three_tasks('([)]', [[1, 3, True, False], [3, 5, False, True]], '-3+5')

Output: {'valid_brackets': False, 'merged_intervals': [[1, 3, True, False], [3, 5, False, True]], 'expression_result': 2}

Example 3

Input: process_three_tasks('', [], '1+-2')

Output: {'valid_brackets': True, 'merged_intervals': [], 'expression_result': -1}

Hints

Hint 1

For bracket validation, use a stack to track unmatched opening brackets. Each closing bracket must match the most recent unmatched opener.

Hint 2

When merging intervals, first remove empty sets (where start equals end but at least one side is open), then sort by start position with ties broken by whether the left side is closed.

Hint 3

For expression evaluation, handle unary minus by checking whether an operand precedes the minus sign. Use recursive descent or a two-stack approach to respect operator precedence.