Maximum Gap Between Consecutive Workers' Stations

Quick Overview

Maximize the largest station-index gap between consecutive workers while matching every skill in order to distinct stations, with a valid assignment guaranteed.

Maximum Gap Between Consecutive Workers' Stations

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

# Maximum Gap Between Consecutive Workers' Stations You are given strings `skill` and `station`. Worker `i` requires the skill `skill[i]`, and station `j` supports `station[j]`. Assign every worker to a different station so that: - The station's supported skill matches the worker's required skill. - Assigned station indices strictly increase in worker order. If worker `i` is assigned index `j[i]`, the assignment's gap is the largest difference `j[i] - j[i - 1]` between consecutive workers. With only one worker, the gap is `0`. Return the maximum possible assignment gap. At least one valid assignment is guaranteed to exist. ### Function `int maximumGap(string skill, string station)` ### Example 1 **Input:** `skill = "aba", station = "aabxxbaa"` **Output:** `5` **Explanation:** Assign the workers to station indices `[0, 5, 7]`. Their skills are `a`, `b`, `a`, and their consecutive gaps are `5` and `2`. A gap larger than `5` is impossible: the `b` stations are at indices `2` and `5`, with matching `a` stations before and after them. ### Example 2 **Input:** `skill = "z", station = "azzzb"` **Output:** `0` **Explanation:** The single worker can use any matching `z` station. There is no pair of consecutive workers, so the assignment gap is `0`. ### Constraints - `1 <= skill.length <= station.length <= 100000` - Both strings contain only lowercase English letters. - A matching, strictly increasing assignment exists for all workers. ### Clarifications - Station indices are zero-based, and unused stations are allowed. - A gap is an index difference, not the number of unused stations strictly between two workers. - Maximize the largest consecutive-worker gap, not the smallest gap. - All workers must be assigned, including those before and after the pair producing the largest gap. ### What a Strong Answer Covers - Respecting skill matches, distinct stations and worker order simultaneously. - Widening a consecutive pair's gap without preventing a complete assignment. - Handling repeated skills, one worker and the full string-length limit.

Overview: Maximize the largest station-index gap between consecutive workers while matching every skill in order to distinct stations, with a valid assignment guaranteed.

Amazon logo
Amazon
Source window: 0–6 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Maximum Gap Between Consecutive Workers' Stations

You are given strings skill and station. Worker i requires the skill skill[i], and station j supports station[j].

Assign every worker to a different station so that:

  • The station's supported skill matches the worker's required skill.
  • Assigned station indices strictly increase in worker order.

If worker i is assigned index j[i], the assignment's gap is the largest difference j[i] - j[i - 1] between consecutive workers. With only one worker, the gap is 0.

Return the maximum possible assignment gap. At least one valid assignment is guaranteed to exist.

Function

int maximumGap(string skill, string station)

Example 1

Input: skill = "aba", station = "aabxxbaa"

Output: 5

Explanation: Assign the workers to station indices [0, 5, 7]. Their skills are a, b, a, and their consecutive gaps are 5 and 2. A gap larger than 5 is impossible: the b stations are at indices 2 and 5, with matching a stations before and after them.

Example 2

Input: skill = "z", station = "azzzb"

Output: 0

Explanation: The single worker can use any matching z station. There is no pair of consecutive workers, so the assignment gap is 0.

Constraints

  • 1 <= skill.length <= station.length <= 100000
  • Both strings contain only lowercase English letters.
  • A matching, strictly increasing assignment exists for all workers.

Clarifications

  • Station indices are zero-based, and unused stations are allowed.
  • A gap is an index difference, not the number of unused stations strictly between two workers.
  • Maximize the largest consecutive-worker gap, not the smallest gap.
  • All workers must be assigned, including those before and after the pair producing the largest gap.

What a Strong Answer Covers Guidance

  • Respecting skill matches, distinct stations and worker order simultaneously.
  • Widening a consecutive pair's gap without preventing a complete assignment.
  • Handling repeated skills, one worker and the full string-length limit.