KatInterview

Tally Faulty and Clean Submissions per Company

SQL & DataAuto-graded · 17 test cases
MediumAuto-gradedSQL & Dataconditional-aggregationleft-joinhash-map

Last updated

Aggregate two conditional counts per group from two related row lists, keeping groups with no matching rows and handling both boolean and string status values.

Tally Faulty and Clean Submissions per Company

You are given two tables as lists of rows. Write a function that reports, for every company, how many of its submissions were faulty and how many were clean. This is the Python version of a LEFT JOIN + GROUP BY + conditional aggregation query.

Input

  • companies: a list of rows [company_id, name].
  • entries: a list of rows [entry_id, company_id, created_at, status].

The status field (the fourth element of an entry row) comes in one of two forms:

  • A boolean: True means faulty, False means clean.
  • A string: "buggy" means faulty, "non_buggy" means clean.

Output

Return a list of dictionaries, one per distinct company_id in companies, each with exactly these keys:

  • "employer_id": the company id
  • "buggy_count": the number of faulty entries for that company
  • "non_buggy_count": the number of clean entries for that company

The list must be sorted by employer_id in ascending order.

Rules

  • Every company in companies must appear in the output, even if it has no entries (both counts are then 0).
  • If the same company_id appears in more than one row of companies, it produces only one output row.
  • An entry whose company_id does not appear in companies is ignored.
  • A string status other than "buggy" or "non_buggy" (for example "pending" or "BUGGY"; matching is case-sensitive and exact) is counted in neither column.
  • A status that is neither a boolean nor a string (for example None) is counted in neither column.
  • The created_at and name values play no role in the result.

Constraints

  • 0 <= len(companies), len(entries) <= 10^5
  • Company ids and entry ids are integers.
  • Each company row has at least 2 elements; each entry row has 4 elements.
  • Your solution should run in roughly O((E + S) + E log E) time, where E is the number of companies and S the number of entries.

Examples

Example 1

Input: tally_faulty_clean([[1, 'Acme'], [2, 'Globex'], [3, 'Initech']], [[10, 1, '2025-08-20', True], [11, 1, '2025-08-21', False], [12, 1, '2025-08-22', True], [13, 2, '2025-08-23', False], [14, 2, '2025-08-24', False]])

Output: [{'employer_id': 1, 'buggy_count': 2, 'non_buggy_count': 1}, {'employer_id': 2, 'buggy_count': 0, 'non_buggy_count': 2}, {'employer_id': 3, 'buggy_count': 0, 'non_buggy_count': 0}]

Example 2

Input: tally_faulty_clean([[1, 'Acme'], [2, 'Globex'], [3, 'Initech']], [[10, 1, '2025-08-20', 'buggy'], [11, 1, '2025-08-21', 'non_buggy'], [12, 1, '2025-08-22', 'buggy'], [13, 2, '2025-08-23', 'non_buggy'], [14, 2, '2025-08-24', 'non_buggy']])

Output: [{'employer_id': 1, 'buggy_count': 2, 'non_buggy_count': 1}, {'employer_id': 2, 'buggy_count': 0, 'non_buggy_count': 2}, {'employer_id': 3, 'buggy_count': 0, 'non_buggy_count': 0}]

Hints

Hint 1

Think of the output as one bucket per company. Create every bucket up front with zero counts, so companies without entries are still reported.

Hint 2

Walk through the entries once. Skip any whose company is not a known bucket, and otherwise decide faulty versus clean from the status value.

Hint 3

In Python, `bool` is checked separately from `str`; handle each type explicitly, and let unrecognized strings or other types fall through without incrementing anything. Sort the company ids at the end.