Fix the result comparison for a finance query

Review the AI's codeEasy10 minDatabricks and Snowflake style

Your task

An AI agent rewrote a slow finance query, replacing a correlated subquery with a window function, and says it "returns identical results, just faster". A spot check on staging matched. Before the new query feeds the finance dashboards, you compare the full result sets of both queries on a production snapshot.

An assistant wrote diff_result_sets(old_rows, new_rows). It is in the editor. It has a bug. Find it and fix it.

Input and output:

  • old_rows: the old query's rows. new_rows: the new query's rows. Each row is one string.
  • Return the differences, a list of strings. An empty list means the results are identical.

Rules the tests check:

  • Compare rows as whole strings. Row order does not matter, but how many times a row appears does.
  • For each distinct row, take the number of times it appears in old_rows minus the number of times it appears in new_rows:
    • positive N: -<row> xN
    • negative, N more in new_rows: +<row> xN
    • zero: nothing
  • Sort the output by the row text, using plain string comparison.

Examples

diff_result_sets(old_rows: list[str], new_rows: list[str]) -> list[str]
Example 1
Inputdiff_result_sets(["a|10", "b|20"], ["a|10", "b|20"])
Output[]
Example 2
Inputdiff_result_sets(["m|1", "m|1", "z|9"], ["m|1", "z|9"])
Output["-m|1 x1"]
Example 3
Inputdiff_result_sets(["a"], ["a", "b"])
Output["+b x1"]