KatInterview

Binary Search Over Date-Filtered Content Flags

Heaps, Sorting & SearchAuto-graded · 18 test cases
MediumAuto-gradedHeaps, Sorting & Searchbinary-searchsortingdata-manipulation

Last updated

Tests whether a candidate can isolate records by category and then apply binary search to efficiently locate all entries within an inclusive date window.

Content Flag Log Search

A moderation system stores a running log of flagged content items. Each entry records an item identifier, the flag category, and the date the flag was raised.

Each log entry is a dictionary with these keys:

  • pin_id — integer identifier of the flagged item
  • type — string describing the kind of flag (e.g. "spam", "privacy", "child")
  • violation_date — date string in YYYY-MM-DD format

Given the full log, a target flag type, and an inclusive date range, return the pin_id of every entry whose type matches the target and whose violation_date falls within [window_start, window_end] (both endpoints included).

Requirements

  • Use binary search to locate the date boundaries rather than scanning the filtered list linearly.
  • Results must be ordered by violation_date ascending. Among entries sharing the same date, preserve their original order in the input list (stable sort).
  • Do not deduplicate: if the same pin_id appears in multiple matching entries, include it once per matching entry.
  • Return an empty list when nothing matches, when the log is empty, or when window_start > window_end.

Constraints

  • 0 <= len(log) <= 10^5
  • pin_id is a non-negative integer.
  • All violation_date values are zero-padded ISO 8601 strings (YYYY-MM-DD). Lexicographic string comparison is equivalent to chronological comparison for this format.
  • window_start and window_end are also zero-padded ISO 8601 strings.
  • The input log is not guaranteed to be pre-sorted.

Examples

Example 1

Input: search_flag_log([{'pin_id': 0, 'type': 'spam', 'violation_date': '2022-01-03'}, {'pin_id': 1, 'type': 'spam', 'violation_date': '2022-01-03'}, {'pin_id': 0, 'type': 'privacy', 'violation_date': '2022-01-03'}, {'pin_id': 2, 'type': 'child', 'violation_date': '2022-02-06'}, {'pin_id': 3, 'type': 'spam', 'violation_date': '2022-02-10'}], 'spam', '2022-01-03', '2022-02-10')

Output: [0, 1, 3]

Example 2

Input: search_flag_log([{'pin_id': 0, 'type': 'spam', 'violation_date': '2022-01-03'}, {'pin_id': 1, 'type': 'spam', 'violation_date': '2022-01-03'}, {'pin_id': 0, 'type': 'privacy', 'violation_date': '2022-01-03'}, {'pin_id': 2, 'type': 'child', 'violation_date': '2022-02-06'}, {'pin_id': 3, 'type': 'spam', 'violation_date': '2022-02-10'}], 'privacy', '2022-01-01', '2022-01-31')

Output: [0]

Example 3

Input: search_flag_log([{'pin_id': 4, 'type': 'spam', 'violation_date': '2022-03-01'}, {'pin_id': 4, 'type': 'spam', 'violation_date': '2022-03-02'}, {'pin_id': 5, 'type': 'spam', 'violation_date': '2022-03-02'}, {'pin_id': 6, 'type': 'privacy', 'violation_date': '2022-03-03'}], 'spam', '2022-03-02', '2022-03-02')

Output: [4, 5]

Hints

Hint 1

Filter the log down to only the entries matching the target type before doing anything else — binary search requires the array to be sorted on the key you are searching, and mixing multiple types would break that ordering.

Hint 2

Build a parallel list of just the date strings from the filtered, sorted entries. Then use bisect_left on window_start to find the first included index and bisect_right on window_end to find the exclusive upper bound — this is the standard pattern for an inclusive [a, b] window.

Hint 3

Think carefully about why bisect_left and bisect_right are paired the way they are. Using bisect_right on the lower bound or bisect_left on the upper bound would silently exclude entries that fall exactly on a boundary date.