Shortest Shift Block to Reassign So On-Call Coverage Is Balanced

Last updated: November 29, 2026

Quick Overview

An on-call schedule string uses four team letters 'Q', 'W', 'E', 'R' and its length is a multiple of 4. Return the length of the shortest contiguous block you could reassign so every team ends up with exactly n/4 shifts.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


An on-call schedule string uses four team letters 'Q', 'W', 'E', 'R' and its length is a multiple of 4. Return the length of the shortest contiguous block you could reassign so every team ends up with exactly n/4 shifts.

A Google onsite sliding-window problem where the window represents what you change rather than what you keep. The interviewer checks that the candidate reasons about the letters outside the window and shrinks greedily.

What the Interviewer Expects
  • Realizes the outside of the window must have each count at most n/4
  • Counts all letters first, then removes window letters from the counts
  • Shrinks the left edge while the outside stays valid
  • Returns 0 for an already balanced schedule
  • States O(n) time
Key Topics to Cover
Sliding Window
Frequency Counting
Complement Reasoning
Two Pointers
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • What if there are k teams instead of four?
  • What if reassigning a shift to a particular team has a cost?
  • Return the block's start and end, not just its length.
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • Input: string s over {Q, W, E, R}, len(s) % 4 == 0.
  • Reassign the letters of one contiguous block to any teams.
  • Output: minimum block length so all four counts equal n / 4....
High-Level Design

Let target = n / 4. If we reassign window s[l..r], the letters outside it keep their teams, and we can make the inside anything. That works exactly when every team has at most target shifts outs...


Submit Your Answer
Markdown supported