KatInterview

Slot-Aware Feed Deduplication Across Ordered Sections

Arrays & HashingAuto-graded · 22 test cases
MediumAuto-gradedArrays & Hashinghashingsimulationgreedy

Last updated

Tests whether candidates can implement a two-phase, cross-section deduplication policy with per-section slot limits, within-section uniqueness, and optional exempt sections.

Problem

You are building the content feed for a streaming platform. The feed is divided into ordered sections (think genre shelves or curated lists). Each section is filled by pulling from a ranked candidate list, and the system enforces the following policy.

Filling Rules

Guarded slots. Every section i has a guarded slot count guard_sizes[i]. The first guard_sizes[i] items placed into section i form its guarded block. During the guarded phase — while fewer than guard_sizes[i] items have been accepted into the current section — a candidate is accepted only if both of the following hold:

  1. The candidate has not already been placed into the current section.
  2. If the section is non-exempt: the candidate has not appeared in the guarded block of any earlier non-exempt section.

If a candidate fails either check during the guarded phase, it is skipped permanently — it is not buffered or reconsidered for the trailing block of the same section.

Trailing slots. The guarded phase ends as soon as exactly guard_sizes[i] items have been accepted into section i. Every subsequent candidate (up to the max_sizes[i] total item limit) is in the trailing phase. A candidate is accepted in the trailing phase if and only if it has not already been placed into the current section. No cross-section check is applied during the trailing phase.

Within-section uniqueness. No item may appear more than once in a single section, regardless of which phase placed it.

Exempt sections. Sections whose indices appear in skip_indices are exempt. An exempt section:

  • Does not check incoming candidates against the shared guarded history.
  • Does not add its own guarded items to the shared guarded history.
  • Still enforces within-section uniqueness exactly as a non-exempt section does.
  • Still counts guarded slots internally (i.e., the first guard_sizes[i] accepted items are its guarded block, but that block is invisible to all other sections).

Shortfall. If a section's candidate list is exhausted before the section reaches max_sizes[i] items, output however many valid items were collected — no error and no padding.

Processing order. Sections are processed from index 0 to index R − 1. "Earlier non-exempt sections" means non-exempt sections with a smaller index.

Task

Given the sections' candidate lists, guard sizes, maximum sizes, and the set of exempt section indices, return the filled sections in the same order as the input.

Constraints

  • 0 <= R <= 1000, where R is the number of sections; candidate_sections, guard_sizes, and max_sizes each have length R.
  • 0 <= guard_sizes[i] <= max_sizes[i] for all valid i.
  • 0 <= max_sizes[i] <= 500 for all valid i.
  • skip_indices is a set of valid section indices in [0, R), or None (treat as an empty set).
  • Total number of candidate strings across all sections is at most 200 000.
  • Candidate strings are non-empty and may contain any printable characters.

Examples

Example 1

Input: fill_feed_sections([['A', 'B', 'C', 'D'], ['B', 'A', 'E', 'B', 'C']], [2, 2], [3, 3], [])

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

Example 2

Input: fill_feed_sections([['X', 'Y', 'Z'], ['X', 'W', 'V']], [1, 1], [2, 2], [1])

Output: [['X', 'Y'], ['X', 'W']]

Hints

Hint 1

Track two separate sets: one shared set for items already placed into the guarded blocks of non-exempt sections, and one per-section set for items already placed anywhere in the current section. The per-section set is reset for every new section.

Hint 2

A candidate rejected during the guarded phase is discarded for good — do not buffer it to reconsider once the section moves into its trailing phase. The single forward pass over the candidate list is intentional and determines the output.

Hint 3

Exempt sections sit completely outside the shared guarded history: they neither read from it nor write to it. They still enforce within-section uniqueness using their own per-section set, and they still count their own guarded slots internally.