cswork / libraryOpen practice workspace

Coding questions · Walmart Labs

Shortest Talent-Covering Team Starting At Each Index

Practice a sliding-window coding question where each start index needs the shortest contiguous team containing every talent type. This prompt targets hash map counts, two-pointer window management, and O(n) reasoning for large arrays.

There are n students in a line. Each student has exactly one talent represented by an integer from 1 to talentsCount. A valid team is a contiguous subarray that contains every talent from 1 to talentsCount at least once. For every starting index, return the length of the shortest valid team that starts at that index, or -1 if no such team exists.

Function signature:

def shortest_complete_teams(talents_count: int, talent: list[int]) -> list[int]:
    pass

Constraints:

  • 1 <= talents_count <= n <= 100000.
  • Each talent[i] is between 1 and talents_count.
  • The solution should be O(n) or close to it.
  • Return one answer per starting index.

Examples:

talents_count = 3, talent = [1, 2, 3, 2, 1]
Output: [3, 4, 3, -1, -1]

Shortest Talent-Covering Team From Each Start

Hints

  1. Use one right pointer shared across all left starts.
  2. Shrink from the left after recording the answer.