Minimize Boys Needed Beside Girls
Company: Guidewire
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This question evaluates string and array manipulation skills, the ability to model adjacency-based constraints, and optimization reasoning for minimizing resource placement.
Constraints
- 0 <= len(seats) <= 200000
- seats contains only 'G' and '-'
Examples
Input: "-G-GG--"
Expected Output: 2
Explanation: Place one boy in the gap between the first two girls to satisfy both, and one boy next to the last girl.
Input: "GGG"
Expected Output: -1
Explanation: The middle girl has no empty neighboring seat, so it is impossible.
Input: "G-G-G"
Expected Output: 2
Explanation: Only one of the two 'G-G' gaps can be shared for the middle girl, so two boys are required in total.
Input: "-GG-"
Expected Output: 2
Explanation: The two girls are adjacent, so they cannot share one boy. Each needs a different neighboring empty seat.
Input: ""
Expected Output: 0
Explanation: There are no girls, so no boys are needed.
Input: "--G--"
Expected Output: 1
Explanation: The single girl needs one boy in either neighboring empty seat.
Hints
- A single boy can satisfy two girls only when he sits in the middle of a 'G-G' pattern.
- Scan from left to right. If a girl cannot be paired with the next girl using one shared boy, then she must eventually need her own boy.