KatInterview

Consecutive-Range Handoff Pairs for Interval Coverage

Greedy & IntervalsAuto-graded · 18 test cases
MediumAuto-gradedGreedy & Intervalsintervalsbinary-searchhashing

Last updated

Tests interval reasoning and efficient pairing: find all unordered listing pairs whose consecutive availability windows together cover a full date range.

Consecutive-Range Handoff Pairs

You are given a collection of named listings. Each listing has an availability schedule: a list of individual day numbers on which it is open. You are also given a target window defined by a first day s and a last day e, both inclusive.

A handoff pair is an unordered pair of two distinct listings (X, Y) such that some split point t exists with s ≤ t < e where:

  • Listing X is available on every day from s through t with no gaps, and
  • Listing Y is available on every day from t + 1 through e with no gaps.

Note that either listing may cover more than its required segment — what matters is that the required days are all present and contiguous. Each valid unordered pair must appear in the output exactly once, with the two listing names sorted alphabetically within the pair.

Return the list of all such pairs, sorted lexicographically.

What counts as a valid consecutive block

A listing covers a segment [a, b] consecutively if every integer from a to b inclusive appears in its availability list. Gaps anywhere in that range disqualify it for that role.

Constraints

  • 1 ≤ number of listings ≤ 500
  • Each availability list contains between 0 and 10 000 integers.
  • Day numbers are positive integers; duplicates within a single listing's availability list are allowed in the input and should be treated as a single occurrence.
  • 1 ≤ s ≤ e ≤ 10 000
  • If s == e or s > e, no valid split point exists — return an empty list.
  • A listing may qualify as both the first and the second segment partner in different pairs.
  • A listing cannot be paired with itself.
  • If no valid pair exists, return an empty list.

Examples

Example 1

Input: find_handoff_pairs({'A': [1, 2, 3, 6, 7, 10, 11], 'B': [3, 4, 5, 6, 8, 9, 10, 13], 'C': [7, 8, 9, 10, 11]}, 3, 11)

Output: [['B', 'C']]

Example 2

Input: find_handoff_pairs({'A': [1, 2, 3, 4], 'B': [5, 6, 7, 8], 'C': [2, 3, 4, 5, 6, 7, 8]}, 1, 8)

Output: [['A', 'B'], ['A', 'C']]

Example 3

Input: find_handoff_pairs({'A': [1, 2, 3], 'B': [1, 2, 3], 'C': [4, 5, 6]}, 1, 6)

Output: [['A', 'C'], ['B', 'C']]

Hints

Hint 1

For each listing, you only need two numbers: how far its consecutive run from day `s` reaches (its 'prefix reach'), and where its consecutive run back from day `e` begins (its 'suffix start'). Everything else about the listing is irrelevant.

Hint 2

A pair (X, Y) is valid if X's prefix reach plus one is at least Y's suffix start — in other words, X hands off no later than Y can pick up. Check both orderings (X first, then Y first) since either listing can play either role.

Hint 3

To avoid checking every pair against every possible split day, reduce the problem to comparing two integers per pair. Sorting candidates by their suffix-start value and using binary search lets you find all matching partners for a given prefix-reach in O(log N) rather than O(N).